开发者

CakePHP Select Query

开发者 https://www.devze.com 2023-04-08 23:51 出处:网络
Is there a way to select all fields from a specific table in CakePHP? So something like: $this->Model1->find(\'first\',

Is there a way to select all fields from a specific table in CakePHP? So something like:

$this->Model1->find('first',
                    array('fields' => 'Model2.*',
                          'conditions' => 'Model1.id = Model2.Model1_id'),
                          'contain' => array());

I've been l开发者_运维百科ooking all over the place and can't find anything on this. I'm kind of hoping that I don't have to type out all of the fields for Model2 :(

Forgive me for my nubishness, I just started learning Cake. Many thanks in advance!


You don't. The syntax you have (meaning the Model2.* part) will work, as will defining no fields at all. By default, they're all returned.

I don't know, though, whether the find call as you have it will work. It seems awkward at best to be executing a find on Model1 in order to get data from Model2. As Henri mentioned in his comment, better to do the find on Model2.


For some driver, wildcard won't work as expected. So you need the whole list of fields.

$ds = $this->Model1->getDataSource();

$this->Model1->find('first', array(
    'fields' => $ds->fields($this->Model1),
    'conditions' => 'Model1.id = Model2.Model1_id',
    'contain' => array()
));


you can try this

$this->Model1->find('first',
array(
  'fields'=>'Model2.*',

  'joins' => array(
      array(
        'table'=>'table',
        'alias'=>'Model2',
        //'type'=>'LEFT',
        'conditions'=>array(
          "Model2.model1_id = Model1.id",          
        ),
      ),
  ),

  'conditions' => array('Model1.somefields' =>'1'
  )

) );

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号