We can implement it in the following ways
{Query all the information containing the keyword keyword in the Note of the specified User}
1.SQL statement
SELECT * FROM Users AS User LEFT JOIN Notes AS Note ON User.id = Note.user_id WHERE User.id = {$user_id} AND Note.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 instructions on these two methods, please refer to
http://api.cakephp.org/class/model
Our approach is
//重新綁定關聯(lián)指定查詢條件 $this->User->unbindModel('Note'); $this->User->bindModel( 'hasMany' => array( 'Note' => array( 'conditions' => array( 'Note.subject LIKE' => '%'.$keyword.'%' ) ) ) ); //指定主表條件獲取數(shù)據(jù) $data = $this->User->find('all',array( 'conditions' => array( 'User.id' => $user_id ) )); //或者 $data = $this->User->read(null,$user_id);
3. Using Cakephp’s core behavior (Behavior) Containable
We first create our own AppModel class and create the file /app/app_model.php
class AppModel extends Model { //加載核心行為 var $actsAs = array('Containable'); }
Then we can query through such code in the controller
$this->User->contain('Note.subject LIKE' => '%'.$keyword.'%'); $data = $this->User->find('all',array( 'conditions' => array( 'User.id' => $user_id ) ));
You can also write it directly into 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.'%' ) ) ) ));
Notes:
If you want to query {User.name or Note.subject all records containing the keyword keyword}
At this time, Cakephp's find method cannot be implemented This query must use the custom SQL statement introduced above, as follows:
SELECT * FROM users AS User LEFT JOIN notes AS Note ON User.id = Note.user_id WHERE User.name LIKE '%keyword%' OR Note.subject LIKE '%keyword%'
The above is a summary of the method of querying the association table in Cakephp. For more related content, please pay attention to the PHP Chinese website (miracleart.cn)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Validator can be created by adding the following two lines in the controller.

This chapter deals with the information about the authentication process available in CakePHP.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

To work on file upload we are going to use the form helper. Here, is an example for file upload.
