I want to convert following mysql statement into cakephp query syntax.Please help me. Here is my query.SELECT * FROM post WHERE (user_id == $id OR awarded_to = $id ) AND id = $id
Here is my cakephp query. is it wrong.
$this->Post->find('all',
array('conditions' => array('OR' =>
array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'AND' =>
array('Post.id' => $id)
))
This one's on the house, but next time read the docs.
$this->Post->find('all', array(
'conditions' => array(
'OR' => array(
array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'Post.id' => $id
)
));
The 2 OR arrays need to be inside an array themself, and there's no need for AND.
Just add Post.id = $id below the OR condition without you give "and" it's automatic read as and condition...
$this->Post->find('all', array(
'conditions' => array(
'OR' => array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'Post.id' => $id
));
You are very close with your query, just you need little modification like to remove 'AND' in conditions array after 'OR' as:
$this->Post->find('all', array(
'conditions' => array(
'OR' => array(
array('Post.user_id' => $id),
array('Post.awarded_to' => $id)
),
'Post.id' => $id
)
));
Or
$this->Post->find('all', array(
'conditions' => array(
'Post.id' => $id
)
));
The second option will be the best for you if you are passing tables primary key 'id' in AND conditions.
Related
How can this Cakephp query be made to count distinct?
$matches = $this->Product->find('count', array(
'conditions' => array(
"Product.brand_id" => $brand['Brand']['id'],
'Product.active' => 1
));
Something like this?
$matches = $this->Product->find('count', array(
'fields' => 'DISTINCT Product.brand_id',
'conditions' => array("Product.brand_id" => $brand['Brand']['id'],
'Product.active' => 1)
));
How can I group by fields and Order by DESC in cakephp?
$this->set('messages', $this->Message->find('all',array('conditions' => array('Message.receiver_id'=>$this->Session->read('Auth.User.id')),'group' => array('Message.user_id'),'order' => array('max(Message.created) ASC'))));
Records are fetched in cakePhp depend on a model is
$records = $this->Model->find('all', array(
'conditions' => array(conditons),
'fields' => array(fieldlist),
'group' => array('Model.column'),
'order' => array('Model.column', 'Model.column2 DESC')
));
From your code, you can write as
$data = $this->Message->find('all', array(
'conditions' => array( 'Message.receiver_id'=>$this->Session->read('Auth.User.id')),
'group' => array('Message.user_id'),
'order' => array('max(Message.created) ASC')
));
$this->set('messages', $data);
$records = $this->Model->find('all', array(
'conditions' => array(conditons),
'recursive' => -1,
'fields' => array(fieldlist),
'group' => array('Model.column'),
'order' => array('Model.column', 'Model.column2 DESC')
));
If you want to order by latest Message inserted then instead of sort by created use sort by id like this Model.id DESC'
I am using cakephp 2.1 and defined 3 tables as follows.
`industries(id, name);`
`movies(id, name, industry_id),`
`trailers(id, name, movie_id);`
I want to paginate the trailers for particular industry. So the code I have written is below:
$this->paginate = array(
'Industry' => array(
'contain' => array(
'Movie' => array(
'order' => 'Movie.release DESC',
'Trailer' => array(
'conditions' => array(
'Trailer.id !=' => $trailer_id
)
)
)
),
'conditions' => array(
'Industry.id' => $id
)
)
);
If I would specify 'limit' for 'Trailer', I get correct number of results but some null array for a movie which doesn't contain any trailers. Please help me to get the number of specified limit of trailers for particular industry. The work will be most appreciated.
It looks to me like you've got your contain key in the wrong place. It should be defined before the models.
<?php
$this->paginate = array(
'contain' => array(
'Industry' => array(
'Movie' => array(
'order' => 'Movie.release DESC',
'Trailer' => array(
'conditions' => array(
'Trailer.id !=' => $trailer_id
)
)
)
)
),
'conditions' => array(
'Industry.id' => $id
)
);
I search for all albums by id using:
$this->Album->find('all', array(
'conditions' => array(
'Album.id' => $albums_ids,
'Album.galleries_id' => $id
)
));
But I also would like to find all non-private albums (private == 0) as well. I tried:
$this->Album->find('all', array(
'conditions' => array(
'Album.id' => $albums_ids,
'Album.galleries_id' => $id,
'OR'=> array(
array('Album.private' => 0),
array('Album.galleries_id' => $id)
)
)
));
but no success...
it should be gallery_id
$this->Album->find('all', array(
'conditions' => array(
'Album.galleries_id' => $id,
'OR'=> array(
'Album.private' => 0,
'Album.id' => $albums_ids
)
)
));
Would you not simply be looking for the following logic instead?
$this->Album->find('all', array(
'conditions' => array(
'Album.id' => $albums_ids,
'Album.galleries_id' => $id,
'Album.private' => 0,
)
));
I am trying to do a find with conditions in two models. Is this possible ?
$offices = $this->User->Org->find('first', array(
'conditions' => array(
"or" => array(
'Org.website LIKE' => $match,
'Domain.domain' => $match
)
)
));
The relationship looks like this
'Domain' => array(
'className' => 'Domain',
'foreignKey' => 'org_id',
),
As a containable search
$this->User->Org->Behaviors->attach('Containable');
$offices = $this->User->Org->find('first', array(
'contain' => array(
'Domain' => array(
'conditions' => array(
'Or' => array(
'Domain.domain' => $match
)
)
),
'Office' => array(
'fields' => array('Office.id', 'Office.city')
)
),
'conditions' => array(
"or" => array(
'Org.website' => $match
)
)
));
Thanks
Alex
It's possible as long as your level of recursion is set appropriately, but I highly recommend using the Containable Behavior for something like this. It makes this things trivial, readable and surgical (you get what you need and only what you need).