使用CakePHP,如何根据相关表上的条件选择记录

使用CakePHP,如何根据相关表上的条件选择记录,cakephp,cakephp-1.2,Cakephp,Cakephp 1.2,另一个noob问题——使用CakePHP的v1.2.1.8004,我认为 我有3个表,broker(B)、quote_site(QS)和broker_quote_site(BQS)将它们链接在一起 B有许多BQ(它属于B) QS有很多BQ(属于QS) 我试图检索链接到特定代理的quote站点,但CakePHP并没有在幕后进行表的连接 我的问题是: $quote_sites = $this->QuoteSite->find('all', array( 'cond

另一个noob问题——使用CakePHP的v1.2.1.8004,我认为

我有3个表,broker(B)、quote_site(QS)和broker_quote_site(BQS)将它们链接在一起

B有许多BQ(它属于B) QS有很多BQ(属于QS)

我试图检索链接到特定代理的quote站点,但CakePHP并没有在幕后进行表的连接

我的问题是:

    $quote_sites = $this->QuoteSite->find('all', array(
        'conditions' => array(
            'Broker.company_id' => $company_id,
            'BrokerQuoteSite.is_active' => true
        ),
        'contain' => array(
            'BrokerQuoteSite' => array(
                'Broker'
            )
        )
    ));
以下是相关模型:

<?php
class QuoteSite extends AppModel
{
    var $name = 'QuoteSite';
    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasMany = array(
        'BrokerQuoteSite' => array(
            'className' => 'BrokerQuoteSite',
            'foreignKey' => 'quote_site_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}
?>

经纪人:

<?php
class Broker extends AppModel
{
    var $name = 'Broker';
    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasMany = array(
        'BrokerQuoteSite' => array(
            'className' => 'BrokerQuoteSite',
            'foreignKey' => 'broker_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
}
?>

最后一点:

<?php
class BrokerQuoteSite extends AppModel
{
    var $name = 'BrokerQuoteSite';
    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'Broker' => array(
            'className' => 'Broker',
            'foreignKey' => 'broker_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        ) ,
        'QuoteSite' => array(
            'className' => 'QuoteSite',
            'foreignKey' => 'quote_site_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
        )
    );
}
?>


提前感谢您提供的任何提示/窍门。

这是一个很好的问题,我认为应该分两步进行

$this->Broke = ClassRegistry::init("Broke");
$brokeids = $this->Broke->find("list",array("conditions"=>array('Broker.company_id' => $company_id)));     //get all B ids

$this->QuoteSite->Behaviors->attach('Containable');  //use containable behavior
$quote_sites = $this->QuoteSite->find('all',array(
                                                     'contain'=>array(
                                           'BrokerQuoteSite'=>array(
              'conditions'=>array(
                                  "BrokerQuoteSite.broke_id"=>$brokeids,
                                  "BrokerQuoteSite.is_active" => true
                                 )
                                                                   )
                                                                      )

                                                  )

                                      );     
代码还没有经过测试,可能有一些语法错误。希望能有所帮助

更新

$this->Broke = ClassRegistry::init("Broke");
$this->Broke->recursive=2;
$brokes = $this->Broke->find("all",array("conditions"=>array("Broke.company_id"=>$comany_id)));
如果您使用
debug($brokes)查看结果,您需要的QS信息将被找到。您需要做的是从数组中提取它们


干杯

Chris为什么不将Broker定义为一种HABTM关系,然后通过简单的查找就可以得到想要的结果

class Broker extends AppModel {
    var $name = 'Broker';   
    var $hasAndBelongsToMany = array(
        'QuoteSite' =>
            array(
                'className'              => 'QuoteSite',
                'joinTable'              => 'broker_quote_sites',
                'foreignKey'             => 'broker_id',
                'associationForeignKey'  => 'quote_site_id'                    
            )
    );
}

非常感谢-看起来不错,稍后将试用。我担心这需要分步骤完成,这将我的cakePHP推得太远了。@Chris Kimpton,嗯,我认为还有另一种更简单的方法。请参阅我的更新plz。我看到了这一点,但看到它提到了联接表的特定命名约定以及联接表上的哪些字段-我不希望更改这些。但再看看,我认为它可能是灵活的,足以与我们所拥有的。。。谢谢。虽然默认行为依赖于各种约定,但正如Leo所建议的,您可以随意绕过约定。