2011-10-07 57 views
0

我有模型AccountLicense,当我执行方法getExpiringLicenes由于某种原因我与父模型(AccountUser)的关联AccountUser的主键没有被使用,而是使用默认主键'id'。我不知道为什么会发生这种情况。这是插件的一部分。相关模型使用错误的主键

任何帮助,这是非常感谢。

这是我收到的例外:

Warning (512): SQL Error: 1054: Unknown column 'AccountUser.id' in 'on clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684]

这是正在执行的查询:

SELECT `AccountLicense`.`license_id`, `AccountLicense`.`user_id`, 
`AccountLicense`.`board_id`, `AccountLicense`.`license_number`, 
`AccountLicense`.`license_state`, `AccountLicense`.`license_designation_id`, 
`AccountLicense`.`active_date`, `AccountLicense`.`expire_date`, 
`AccountLicense`.`is_active`, `AccountLicense`.`is_confirmed`, 
`AccountLicense`.`is_primary`, `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` 
FROM `account_licenses` AS `AccountLicense` 
LEFT JOIN `account_users` AS `AccountUser` 
ON (`AccountLicense`.`user_id` = `AccountUser`.`id`) 
WHERE `AccountLicense`.`expire_date` BETWEEN '2011-10-05' and '2011-11-04' 

这是我AccountLicenses型号:

<?php 
class AccountLicense extends AppModel { 
    var $name = 'AccountLicense'; 
    var $primaryKey = 'license_id'; 
    var $plugin = 'AccountModule'; 

    var $belongsTo = array(
     'AccountUser' => array(
      'className' => 'AccountUser', 
      'foreignKey' => 'user_id' 
     ) 
    ); 


    public function getExpiringLicenses($date = null) 
    { 
     if(is_null($date)) 
      $date = date('Y-m-d',strtotime("+30 days")); 
     return $this->find(
      'all', 
      array(
       'conditions' => array(
        $this->name . '.expire_date BETWEEN ? and ?' =>array(date('Y-m-d'),$date) 
       ) 
      ) 
     );  
    } 
} 
?> 

这是我的帐户用户型号:

<?php 
class AccountUser extends AppModel { 
var $name = 'AccountUser'; 
var $primaryKey = 'user_id'; 
var $actsAs = array('Containable'); 

var $validate = array(
    'user_name'=>array(
     'rule'=>'isUnique', 
     'message'=>'This username has already been taken. Please try again' 
), 
    'user_pass' => array(
     'rule' => array('between', 8, 16), 
     'message' => 'Passwords must be between 8 and 16 characters long.') 

); 

var $hasMany = array(
    'AccountLicense' => array(
     'className' => 'AccountLicense', 
     'foreignKey' => 'user_id' 
    ) 
); 
?> 

回答

1

既然是一个插件的关联应该有插件名称

例子: 如果你的插件名称是AccountModule你的公会应该像

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

而且是没有错的,但正确的方法在插件中声明模型类是

<?php 
class AccountLicense extends AccountModuleAppModel { 

如果它在您的文件夹结构中的正确位置并正确地声明你不会需要这条线

var $plugin = 'AccountModule'; 
+0

即使做出这些更改,我仍然会得到相同的错误。 – rottmanj

+0

您是否也为帐户用户使用AccountModuleAppModel?你是否创建了从appModel扩展的AccountModuleAppModel?此外控制器AccountModuleAppController?是plugins文件夹内的文件夹内的所有内容?它没有找到模型,因此它使用默认参数作为表和id创建默认模型,问题在于为什么:S有很多可能性。你读过关于插件的书吗? – api55