0
votes

I have the model AccountLicense, when I execute the method getExpiringLicenes for some reason the association I have with the parent model (AccountUser) the primary key of AccountUser is not being used, it is instead using the default primary key 'id'. I am not sure why this is happening. This is all part of a plugin.

Any help with this is greatly appreciated.

This is the exception I am getting:

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

This is the query that is being executed:

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'

This is my AccountLicenses model:

<?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)
                 )
            )
        );      
    }
}
?>

This is my AccountUser model:

<?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

1 Answers

1
votes

Since is a plugin your association should have the plugin name

Example: If your plugin name is AccountModule your association should look like

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

Also is not wrong, but the correct way to declare a model class inside a plugin is

<?php
class AccountLicense extends AccountModuleAppModel {

If it is in the correct place in your folder structure and correctly declared you won't need this line

var $plugin = 'AccountModule';