繁体   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