2011-10-09 57 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