【问题标题】:how to fetch data from two table in cake php如何从cake php中的两个表中获取数据
【发布时间】:2012-03-12 07:09:04
【问题描述】:

这是操作网址

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

basename - chrysanthemum_10.jpg

car_id - 1

提前致谢

【问题讨论】:

    标签: php cakephp cakephp-1.3


    【解决方案1】:

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

    图库属于汽车

    汽车有一个画廊

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

    // 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 有很多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']);
    ?>
    

    【讨论】:

    • 我已经添加了 car.php public $hasMany = array( 'Gallery' => array( 'className' => 'Gallery', 'foreignKey' => 'car_id', 'dependent' = > 真的));和gallery.php var $belongsTo = array('Car' => array('className' => 'Car', 'foreignKey' => 'car_id', ));但我得到了那个味精未定义的索引:Gallery [APP\views\cars\home.ctp, line 226],你能帮我更多吗
    • 你是用recursive还是containable?
    • ori 先生,我不关心递归或可包含,我是 cake php 的新手,但我已经编辑了我的问题,所以请你现在检查
    • ori 先生,我需要你的帮助,我想在显示图像的同时计算图像(第一辆车的第一个画廊),你能告诉我该怎么做吗?
    • 对不起,我这几天真的很忙,不明白你想做什么。你为什么不发布一个新问题?有足够的人可以提供帮助。
    【解决方案2】:

    在Controller find中使用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'))
    ))
    )
    

    【讨论】:

      【解决方案3】:

      在你的控制器文件中添加这一行

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

      你可以直接在视图中使用$table,如果你和table有关系你可以使用CakePHP提供的默认关系

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 2017-08-23
        • 1970-01-01
        • 2018-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多