关系不正常 - cakePHP

时间:2014-03-17 14:39:48

标签: cakephp cakephp-2.4

我通过内置的CakePHP构建函数建立了关系。首先,我的表格:

assets
id | company_id | group_id | location_id | item_id | invoice_id | item_number | description | purchase_value | purchase_date | barcode | colour | picture | created | modified

repairs
id | company_id | asset_id | invoice_id | created | modified

我在asset模型中的关系如下:

public $hasMany = array(
    'Repair' => array(
        'className' => 'Repair',
        'foreignKey' => 'asset_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

在我的repair模型中,像这样:

public $belongsTo = array(
    'Asset' => array(
        'className' => 'Asset',
        'foreignKey' => 'asset_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

但是,当我尝试查看资产时,会显示以下错误:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Asset.asset_id' in 'field list'

SQL Query: SELECT `Asset`.`id`, `Asset`.`company_id`, `Asset`.`group_id`, `Asset`.`location_id`, `Asset`.`item_id`, `Asset`.`invoice_id`, `Asset`.`item_number`, `Asset`.`description`, `Asset`.`purchase_value`, `Asset`.`purchase_date`, `Asset`.`barcode`, `Asset`.`colour`, `Asset`.`picture`, `Asset`.`created`, `Asset`.`modified`, `Asset`.`asset_id` FROM `asset_register`.`assets` AS `Asset` WHERE `Asset`.`company_id` = 1 AND `Asset`.`asset_id` = (4)

任何人都知道这是什么问题?

编辑更新

我的find语句是普通的CakePHP查找语句。

$options = array('conditions' => array('Asset.' . $this->Asset->primaryKey => $id));
$this->Asset->recursive = 1;
$this->set('asset', $this->Asset->find('first', $options));

1 个答案:

答案 0 :(得分:1)

修改

将此行添加到资产模型

public $primaryKey = 'id';

并确保$ primaryKey未在模型中的其他位置定义。