2012-03-12 73 views
0

这是动作URL如何在蛋糕的PHP取从两个表中的数据

http://localhost/carsdirectory/cars/home

cars_controller.php(控制器)

public function home(){ 

    $this->set('latest_cars', $this->Car->find('all', array(
     'order' => array(
      'Car.modified DESC', 
      'Car.created Desc'    
     ), 
     'limit' => '3' 
    ))); 

    $this->set('galleries', $this->Gallery->find('all')); 


} 

car.php(模型)

public $hasMany = array(
    'Gallery' => array(
     'className' => 'Gallery', 
     'foreignKey' => 'car_id', 
     'dependent' => true 
    ) 
); 
(模特)
var $belongsTo = array(
     'Car' => array(
      'className' => 'Car', 
      'foreignKey' => 'car_id', 
) 
    ); 

home.ctp(视图)

<?php foreach($latest_cars as $latest_car){ ?> 

    <img src="img/car-listings.jpg" />  // now it's static 

    <h4><?php echo $latest_car['Car']['car_name']; ?></h4> // it's dynamic it's coming car table 

    <span>$<?php echo $latest_car['Car']['car_price']; ?></span> // it's dynamic it's coming car table 


<?php } ?> 

我有替换线

<img src="img/car-listings.jpg" /> 

与线

<?php $this->Html->image('/media/filter/small/'.$latest_cars['Gallery']['dirname'].'/'.$latest_cars['Gallery']['basename']);?> 

但即时得到该错误

未定义指数:胆ERY [APP \视图\汽车\ home.ctp,线226]

<img src="img/car-listings.jpg" /> this line i want to make dynamic , so my question how to use join in cars_controller or any other idea and i want to fetch data from galleries table 

这是画廊表结构

ID - 1

基名 - chrysanthemum_10.jpg

car_id - 1

在此先感谢

回答

2

所以,你有一个Car模型和Gallery模型。由于Gallerycar_id属性,它可以用来形成这些CakePHP的Associations

画廊属于关联租车

汽车hasOne画廊

你可以选择您实际需要的关联并在模型中定义它们。在你的情况,你想查询汽车的时候,所以,以显示该车的画廊:

// in car.php 

var $hasOne = 'Gallery'; 

然后你可以选择是否要使用Containable来控制协会得到包括在查询中,或者只是使用recursive包括所有这些:

// in cars_controller.php 

$this->set('latest_cars', $this->Car->find('all', array(
    'recursive' => 1, 
    'order' => array(
     'Car.modified DESC', 
     'Car.created Desc'    
    ), 
    'limit' => '3' 
))); 

然后在您的视图中,使用$latest_car['Car']访问汽车性能和$latest_car['Gallery']访问库属性

编辑

如果Car的hasMany Gallery,那么你就应该想到这个结构:

[0] => 
    Car => (first car) 
    Gallery => 
     [0] => (first gallery of first car) 
     [1] => (second gallery of first car) 
[1] => 
    Car => (second car) 
    Gallery => 
     [0] => (first gallery of second car) 

etc. 

所以要访问它在您的观点:

<?php 
    foreach($latest_cars as $latest_car){ 
     foreach ($latest_car['Gallery'] as $gallery) 
      echo $this->Html->image('/media/filter/small/'.$gallery['dirname'].'/'.$gallery['basename']); 
?> 
+0

我已经加入car.php public $ hasMany = array( \t'Gallery'=> array( \t'class名称” => '廊', \t 'FOREIGNKEY'=> 'car_id', \t '依赖'=>真 \t \t) \t);和gallery.php变量$属于关联=阵列( \t \t \t '汽车'=>数组( \t \t \t \t '的className'=> '汽车', \t \t \t \t 'FOREIGNKEY'=> 'car_id', \t) );但我得到那味精未定义的索引:画廊[APP \ views \ cars \ home.ctp,第226行],可以帮助我更多PLZ – 2012-03-12 07:44:47

+0

您使用'递归'或可容纳? – ori 2012-03-12 08:11:00

+0

先生ori,我没有关于递归或包含,即时新的蛋糕PHP,但我已编辑我的问题,所以PLZ你可以检查现在 – 2012-03-12 08:17:09

0

添加此行在您的控制器文件

$this->loadModel('Table');//table is your model name in singular 
    $this->set('table', $this->Table->find('all')); 

就可以直接使用$表视图,如果你有关系,与表,你可以使用CakePHP的提供的默认关系

感谢

+0

我做它已经 – 2012-03-12 07:59:57

1

使用捧场控制器发现,指定领域需要

$results= $this->Car->find('all', 
array('fields'=> array('Car.*','galleries.*'), 
'joins'=> array( 
array('table'=>'galleries', 'type'=>'inner', 
'conditions'=>array('Car.car_id=galleries.car_id')) 
)) 
) 
+0

感谢回复我会尝试用 – 2012-03-12 07:25:05