簡體   English   中英

如何從蛋糕php中的兩個表中獲取數據

[英]how to fetch data from two table in cake 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
    )
);

gallery.php(模型)

    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']);?> 

但我收到那個錯誤

未定義索引:圖庫[APP \\ views \\ cars \\ 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

提前致謝

因此,您有一個Car模型和一個Gallery模型。 由於Gallery具有car_id屬性,因此可以用來形成以下CakePHP 關聯

畫廊屬於汽車

汽車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 Many 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']);
?>

在Controller查找中使用join,根據需要指定字段

$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'))
))
)

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

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

您可以直接在視圖中使用$ table,如果您與table有關系,則可以使用CakePHP提供的默認關系

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM