2012-03-12 37 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