2011-10-09 113 views
0

我有一个拥有2个关联的模型(AccountAgentDetail)。一个是belongsTo(AccountUser),另一个是hasOne(AccountProfile)。 AccountAgent的表格只与AccountUser具有FK关系。该模型和相关模型是插件的一部分。未使用外键的关联

我看到的问题是,当执行查询时,从AccountProfile到AccountAgentDetail的连接正在使用错误的关联。它使用AccountAgentDetail表的id字段而不是我在AccountAgentDetail模型中定义的fk字段。

这是我与工作模式:

<?php 
class AccountAgentDetail extends AccountModuleAppModel { 
var $name = 'AccountAgentDetail'; 
var $primaryKey = 'agent_detail_id'; 

var $belongsTo = array(
    'AccountUser' => array(
     'className' => 'AccountModule.AccountUser', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

var $hasOne = array(
    'AccountProfile' => array(
     'className' => 'AccountModule.AccountProfile', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

public function getProspectiveAgents($count = 10) 
{ 
    return $this->find('all', 
       array(
         'conditions'=>array('AccountAgentDetail.is_prospect'=>1), 
         'order'=>array('AccountAgentDetail.created_date DESC') 
       ) 
      ); 
    } 
} 
?> 

这是当我调用该方法getProspectiveAgents时执行的查询。我看到的问题是在第二次左连接中使用了AccountAgentDetailagent_detail_id而不是AccountAgentDetailuser_id

SELECT 
`AccountAgentDetail`.`agent_detail_id`, 
`AccountAgentDetail`.`user_id`, 
`AccountAgentDetail`.`is_prospect`, 
`AccountAgentDetail`.`mls_id`, 
`AccountAgentDetail`.`primary_office`, 
`AccountAgentDetail`.`primary_board`, 
`AccountAgentDetail`.`commission_plan`, 
`AccountAgentDetail`.`referred_by`, 
`AccountAgentDetail`.`referral_source`, 
`AccountAgentDetail`.`previous_brokerage`, 
`AccountAgentDetail`.`created_date`, 
`AccountAgentDetail`.`last_modify_date`, 
`AccountAgentDetail`.`created_by`, 
`AccountAgentDetail`.`last_modifed_by`, 
`AccountUser`.`user_id`, 
`AccountUser`.`user_name`, 
`AccountUser`.`user_pass`, 
`AccountUser`.`user_status`, 
`AccountUser`.`user_group`, 
`AccountUser`.`instance_id`, 
`AccountUser`.`is_logged_in`, 
`AccountUser`.`is_visible`, 
`AccountUser`.`created_by`, 
`AccountUser`.`last_modified_by`, 
`AccountUser`.`created_date`, 
`AccountUser`.`last_modified_date`, 
`AccountProfile`.`profile_id`, 
`AccountProfile`.`user_id`, 
`AccountProfile`.`first_name`, 
`AccountProfile`.`middle_name`, 
`AccountProfile`.`last_name`, 
`AccountProfile`.`birth_date`, 
`AccountProfile`.`ssn`, 
`AccountProfile`.`employee_id`, 
`AccountProfile`.`hire_date`, 
`AccountProfile`.`sever_date`, 
`AccountProfile`.`rehire_date`, 
`AccountProfile`.`created_by`, 
`AccountProfile`.`last_modified_by`, 
`AccountProfile`.`created_date`, 
`AccountProfile`.`last_modify_date` 
FROM 
`account_agent_details` AS `AccountAgentDetail` 
LEFT JOIN `account_users` AS `AccountUser` ON(
`AccountAgentDetail`.`user_id` = `AccountUser`.`user_id` 
) 
LEFT JOIN `account_profiles` AS `AccountProfile` ON(
`AccountProfile`.`user_id` = `AccountAgentDetail`.`agent_detail_id` 
) 
WHERE 
`AccountAgentDetail`.`is_prospect` = 1 
ORDER BY 
`AccountAgentDetail`.`created_date` DESC 

回答

0

你decalared“agent_detail_id”主键,在我看来,合乎逻辑的选择联接,而不是user_ID的是外键将此作为主键!虽然我不确切知道这个课程在后台如何工作,但我认为你应该使用你的模型。

在你的地方,我会在mvc-class-model上下文中考虑它,只考虑在E-Relational“框架”下。

对此没有把握......也许您的业务需求是这样的,您可以共享主键user_id,实际上将它作为外键和主键在AccountAgentDetail中(如果它是一对一的话)。

+0

这没有任何意义。根据你的逻辑,我所有的主键都需要是user_id。 – rottmanj

+0

有共享主键可能发生的完全有效的情况。你来过这里两次,仍然没有给出一个好的描述,特别是没有一些指示性数据的几张表并不是一个很大的帮助,因为不能保证可以安全地提取案例需求描述! – Melsi