使用错误主键的关联模型

时间:2011-10-07 13:37:16

标签: cakephp

我有模型AccountLicense,当我执行方法getExpiringLicenes由于某种原因我与父模型(AccountUser)的关联没有使用AccountUser的主键时,它使用默认的主键&#39 ; ID&#39 ;.我不确定为什么会这样。这都是插件的一部分。

非常感谢任何帮助。

这是我得到的例外:

  

警告(512):SQL错误:1054:未知列' AccountUser.id'在' on条款' [CORE / cake / libs / model / datasources / dbo_source.php,第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)
                 )
            )
        );      
    }
}
?>

这是我的AccountUser模型:

<?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 个答案:

答案 0 :(得分:1)

由于是一个插件,您的关联应该具有插件名称

实施例: 如果您的插件名称是AccountModule,则您的关联应该类似于

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

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

<?php
class AccountLicense extends AccountModuleAppModel {

如果它位于文件夹结构中的正确位置并且正确声明,则不需要此行

var $plugin = 'AccountModule';
相关问题