We can use the following methods to {query all the information of the Note containing the keyword in the specified User}
We can implement the following methods:
{Query all information about the keyword contained in the Note of the specified User}
1. SQL statements
SELECT * FROM Users AS UserLEFT JOIN Notes AS Note ON User.id = Note.user_idWHEREUser.id = {$user_id}ANDNote.subject LIKE '%{keyword}%'
Then we execute this SQL statement and use the query method of the model.
$data = $this->User->query($sql);
2. use the bindModel () and unbindModel () methods of the model
For more information about the two methods, see
Http://api.cakephp.org/class/model
Our approach is
// Re-bind the association to specify the query condition $ this-> User-> unbindModel ('note '); $ this-> User-> bindModel ('hasances' => array ('note' => array ('conditions' => array ('Note. subject LIKE '=>' % '. $ keyword. '%'); // specifies the primary table condition to obtain data $ data = $ this-> User-> find ('all ', array ('conditions' => array ('user. ID' => $ user_id); // or $ data = $ this-> User-> read (null, $ user_id );
3. use Cakephp's core Behavior (Behavior) Containable
First, create your own AppModel class, and create a file/app/app_model.php.
Class AppModel extends Model {// load core behavior var $ actsAs = array ('containerable ');}
Then, in the controller, we can use this code to query
$this->User->contain('Note.subject LIKE' => '%'.$keyword.'%');$data = $this->User->find('all',array( 'conditions' => array( 'User.id' => $user_id )));
You can also directly write it to the find statement, similar to the following
$data = $this->User->find('all',array( 'conditions' => array( 'User.id' => $user_id ), 'contain' => array( 'Note' => array( 'conditions' => array( 'Note.subject LIKE' => '%'.$keyword.'%' ) ) )));
Note:
If you want to query all records that {User. name or Note. subject contains the keyword}
In this case, the find method of Cakephp cannot implement this query. you must use the custom SQL statement described above, as shown below:
SELECT *FROM users AS UserLEFT JOIN notes AS Note ON User.id = Note.user_idWHEREUser.name LIKE '%keyword%'ORNote.subject LIKE '%keyword%'