另一个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' => '',
)
);
}
?>
在此先感谢您的任何提示/技巧。
我曾见过这个,但看到它提到了连接表的特定命名约定,以及哪些字段位于连接表上 - 我不希望更改该连接表。但再次看,我认为它可能足够灵活,可以与我们所拥有的一起工作......谢谢。 – 2011-03-21 13:10:33
尽管默认行为依赖于各种惯例,但您可以自由地绕过狮子会建议的约定。 – 2011-03-21 13:26:56