【发布时间】:2011-12-02 07:54:39
【问题描述】:
我希望有人能对这个问题有所了解。我已经阅读了大量的 stackoverflow 文档、cake 文档、作品,但我似乎无法解决这个问题。我的模型关系如下:
- 马属于供应商
- 马属于洛特
- 供应商有很多马
- Lot hasMany Sale
- Lot hasMany Withdrawal
然后,我列出所有批次并显示一个包含马匹、供应商、批次号等的表格。这工作正常,实际上使用 Paginator::Sort 方法我什至可以排序为 Vendor.name ASC 或 DESC ,这告诉我关联是正确的。
但是,我似乎无法根据与属于供应商的任何字段的匹配来操纵搜索结果。例如,如果在我的分页设置中我执行以下操作:
'conditions' => array('Vendor.name' => 'Simon')...
我收到一个 SQL 错误,指出列 Vendor.name 不存在,但如果我删除此条件约束然后评估 SQL,它肯定存在。
还应该说,我正在使用 unbindModel() 和 bindModel() 方法动态创建 hasOne 关系以获得最佳 MySQL 开销,这并没有像以前那样导致手头的问题。
这是我在 var_dump() 分页结果时返回的第一行:
'Vendor' =>
array
'id' => string '2' (length=1)
'name' => string 'ALDORA STUD' (length=11)
'surname' => string '' (length=0)
'address' => string '' (length=0)
'tel' => string '' (length=0)
'fax' => string '' (length=0)
'email' => string '' (length=0)
'url' => string '' (length=0)
'comments' => string '' (length=0)
'created' => string '0000-00-00 00:00:00' (length=19)
'modified' => string '0000-00-00 00:00:00' (length=19)
'modified_by' => string '0' (length=1)
'Withdrawal' =>
array
'id' => null
'Sale' =>
array
'id' => null
'catalogue_id' => null
'lot_id' => null
'purchaser' => null
'amount' => null
'notes' => null
'date' => null
我省略了很多行,但您可以清楚地看到 Vendor 作为与我正在工作的控制器 Lot 的 hasOne 关系被送回。
如果有帮助,我正在使用此代码取消绑定和重新绑定模型:
$this->Lot->unbindModel(array(
'hasOne' => array('Horse'),
'hasMany' => array('Withdrawal', 'Update', 'Sale'),
'belongsTo' => array('Catalogue')
));
$this->Lot->Horse->unbindModel(array(
'belongsTo' => array('Vendor', 'Lot')
));
$this->Lot->Horse->Vendor->unbindModel(array(
'hasMany' => array('Horse')
));
$this->Lot->bindModel(array(
'hasOne' => array(
'Catalogue' => array(
'foreignKey' => false,
'conditions' => array('Catalogue.id = Lot.catalogue_id')
),
'Horse' => array(
'foreignKey' => false,
'conditions' => array('Horse.id = Lot.horse_id')
),
'Vendor' => array(
'foreignKey' => false,
'conditions' => array('Vendor.id = Horse.vendor_id'),
),
'Withdrawal' => array(
'foreignKey' => false,
'conditions' => array('Withdrawal.lot_id = Lot.id'),
'fields' => array('id')
),
'Sale' => array(
'foreignKey' => false,
'conditions' => array('Sale.lot_id = Lot.id')
)
)
));
$this->Lot->contain(array('Sale', 'Catalogue', 'Withdrawal', 'Horse', 'Vendor'));
我的分页器调用如下所示:
$this->paginate = array(
'contain' => array('Sale', 'Catalogue', 'Withdrawal', 'Horse', 'Vendor'),
'conditions' => array('Vendor.name' => 'Somerset Stud'),
'maxLimit' => ($this->params['ext'] == 'csv' ? 1000 : 25),
'limit' => ($this->params['ext'] == 'csv' ? 1000 : 25),
);
那是我收到列 Vendor.name 不存在的错误。 如果有人能分享一些技巧和想法,我将不胜感激。
亲切的问候, 西蒙
【问题讨论】:
-
Simon,绑定后需要
$this->Lot->contain(array('Sale', 'Catalogue', 'Withdrawal', 'Horse', 'Vendor'));吗? -
@Moz Morris - 通常我不需要这个,尤其是当我可以在 Paginate 类中使用 contains 属性时。但是我在我的 AppModel 类中将 Recursive 设置为 -1,所以我需要重新声明所有关系,即使在上面动态创建它们时也是如此。
-
当前你在哪个控制器
-
我目前在LotsController工作
标签: model pagination cakephp-2.0