K-RAN
K-RAN

Reputation: 893

CakePHP Select Query

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 looking 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!

Upvotes: 1

Views: 2290

Answers (3)

iwat
iwat

Reputation: 3821

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()
));

Upvotes: 1

sukinsan
sukinsan

Reputation: 513

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

) );

Upvotes: 0

Rob Wilkerson
Rob Wilkerson

Reputation: 41236

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.

Upvotes: 1

Related Questions