2011-03-20 60 views
1

另一个noob问题的条件选择记录 - 使用CakePHP的v1.2.1.8004,我想......使用CakePHP,如何基于相关表

我有3个表,券商(B),quote_site( QS)和将它们链接在一起的broker_quote_site(BQS)。

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

我试图找回报价网站,链接到一个特定的经纪人,但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' => '', 
     ) 
    ); 
} 
?> 

在此先感谢您的任何提示/技巧。

回答

2

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'      
      ) 
    ); 
} 

http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

+0

我曾见过这个,但看到它提到了连接表的特定命名约定,以及哪些字段位于连接表上 - 我不希望更改该连接表。但再次看,我认为它可能足够灵活,可以与我们所拥有的一起工作......谢谢。 – 2011-03-21 13:10:33

+0

尽管默认行为依赖于各种惯例,但您可以自由地绕过狮子会建议的约定。 – 2011-03-21 13:26:56

0

尼斯的问题,我的想法是让两个步骤

$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 
           ) 
                    ) 
                    ) 

               ) 

            );  

的代码还没有测试过,也许有些语法错误了there.Hope它帮助。

更新

$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的信息。

干杯。

+0

许多Thanks-看起来不错,稍后会尝试一下。害怕它需要逐步完成,这推动我的蛋糕PHP太远了。 – 2011-03-21 07:51:25

+0

@Chris Kimpton,嗯,还有另一种更简单的方式,我认为。看到我的更新PLZ。 – Young 2011-03-21 09:22:22