【问题标题】:Relationships not working correctly - cakePHP关系无法正常工作 - cakePHP
【发布时间】:2014-03-17 14:39:48
【问题描述】:

我通过内置的 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 find 语句。

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

【问题讨论】:

  • 你能发布你的find声明吗?还是这样的?
  • @PauloRodrigues 更新了我的问题。

标签: cakephp cakephp-2.4


【解决方案1】:

编辑

将此行添加到您的资产模型中

public $primaryKey = 'id';

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

【讨论】:

  • 将此行添加到您的资产模型 public $primaryKey = 'id';
猜你喜欢
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多