JOIN과 함께 CakePHP의 find 메소드를 사용하여 여러 테이블에서 데이터를 검색하는 방법은 무엇입니까?

Mary-Kate Olsen
풀어 주다: 2024-10-17 22:34:02
원래의
266명이 탐색했습니다.

How to Retrieve Data from Multiple Tables Using CakePHP\'s find Method with JOIN?

Retrieve Data from Multiple Tables Using CakePHP's find Method with JOIN

To execute the specified SQL query using CakePHP's find method, you can employ two approaches:

Method 1: CakePHP Standard Approach

  1. Establish relationships between your models:
<code class="php">class User extends AppModel {
    public $actsAs = array('Containable');
    public $hasMany = array('Message');
}

class Message extends AppModel {
    public $actsAs = array('Containable');
    public $belongsTo = array('User');
}</code>
로그인 후 복사
  1. Define relationships properly:
  2. Rename messages.from column to messages.user_id for auto-association.
  3. Execute the query:
<code class="php">$this->Message->find('all', array(
    'contain' =&gt; array('User')
    'conditions' =&gt; array(
        'Message.to' =&gt; 4
    ),
    'order' =&gt; 'Message.datetime DESC'
));</code>
로그인 후 복사

Method 2: Custom Join

  1. Define custom join parameters:
<code class="php">$this->Message->find('all', array(
    'joins' =&gt; array(
        array(
            'table' =&gt; 'users',
            'alias' =&gt; 'UserJoin',
            'type' =&gt; 'INNER',
            'conditions' =&gt; array(
                'UserJoin.id = Message.from'
            )
        )
    ),
    'conditions' =&gt; array(
        'Message.to' =&gt; 4
    ),
    'fields' =&gt; array('UserJoin.*', 'Message.*'),
    'order' =&gt; 'Message.datetime DESC'
));</code>
로그인 후 복사

Using Multiple Relationships to the Same Model

You can establish two relationships to the same model:

<code class="php">class User extends AppModel {
    public $actsAs = array('Containable');
    public $hasMany = array(
        'MessagesSent' =&gt; array(
            'className'  =&gt; 'Message',
            'foreignKey' =&gt; 'from'
         )
    );
    public $belongsTo = array(
        'MessagesReceived' =&gt; array(
            'className'  =&gt; 'Message',
            'foreignKey' =&gt; 'to'
         )
    );
}

class Message extends AppModel {
    public $actsAs = array('Containable');
    public $belongsTo = array(
        'UserFrom' =&gt; array(
            'className'  =&gt; 'User',
            'foreignKey' =&gt; 'from'
        )
    );
    public $hasMany = array(
        'UserTo' =&gt; array(
            'className'  =&gt; 'User',
            'foreignKey' =&gt; 'to'
        )
    );
}</code>
로그인 후 복사

Example query:

<code class="php">$this->Message->find('all', array(
    'contain' =&gt; array('UserFrom')
    'conditions' =&gt; array(
        'Message.to' =&gt; 4
    ),
    'order' =&gt; 'Message.datetime DESC'
));</code>
로그인 후 복사

위 내용은 JOIN과 함께 CakePHP의 find 메소드를 사용하여 여러 테이블에서 데이터를 검색하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!