Summary of how Cakephp queries joined tables

Source: Internet
Author: User
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%'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.