CakePHP retrieves data from two models

Source: Internet
Author: User
Tags php foreach
Today I learned how to query complex conditions in cakephp and summarized some successful and failed code! In the manual "3.7.3.10 complex condition query", only data is obtained from a model, so we try to get data from both models! Today I learned how to query complex conditions in cakephp and summarized some successful and failed code! In the manual "3.7.3.10 complex condition query", only data is obtained from a model, so we try to get data from both models!

First, let's briefly describe the model relationship: User and Note models! For details about the relationship, see the Note project data structure notes in Cakephp. The data structure is as follows:

 

Purpose: to query a user based on the search keyword "an" and query all notes containing the keyword "! Okay. let's look at the View code:


 

MyAdvsearch
 Create ("User", array ('action' => 'Search'); echo $ form-> input ("q", array ('label' => 'Keywords: '); echo $ form-> end ("search");?>

The users controller requires a search method: function search () {}; write the code to implement the query function in this method! The following shows the SQL statements used for searching!

SELECT *FROM usersLEFT JOIN notes ON users.id = notes.user_idWHERE users.name LIKE '%an%'OR notes.subject LIKE '%an%'LIMIT 0 , 30

The following code is used to achieve our goal! Analyze the following code:

$results = $this->User->find('all',array('conditions' => array('OR' => array('User.name LIKE' => '%'.$this->data['User']['q'].'%','Note.subject LIKE' => '%'.$this->data['User']['q'].'%'))));

The following code is not successful!

$conditions = array('User.name LIKE'=>'%'.$this->data['User']['q'].'%');$results = $this->User->find('all',array('conditions' => $conditions,'contain' => array('Note' => array('conditions' => array('Note.subject LIKE'=>'%'.$this->data['User']['q'].'%',)))));

Finally, we use SQL statement queries to achieve our goal:

function search(){if($this->data['User']['q']){$sql = "SELECT * FROM users AS User " ."LEFT JOIN notes AS Note On User.id = Note.user_id " ."WHERE User.name LIKE '%an%' " ."OR " ."Note.subject LIKE '%an%' ";$results = $this->User->query($sql);$this->set('results',$results);}else{$this->set('results',null);}}

Note that the above error codes are written in the if statement block to determine the correctness!

Name Subject Created

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.