$user = $this->User->find('first',array( 'recursive' => -1,'conditions' => array( 'User.full_name' => 'Bruce Thomas' ) ));
上面的查询将成功返回名为Bruce Thomas的用户的数据.但是当我尝试通过另一个模型使用我的模型用户时,问题就出现了,如下所示的Containable行为:
$user = $this->MyOtherModel->find('first',array( 'contain' => array('User'),'conditions' => array( 'MyOtherModel.id' => $my_other_model_id 'User.full_name' => 'Bruce Thomas' ) ));
(上面的这个例子假设MyOtherModel与我的模型MyOtherModel有belongsTo关系)
Warning (512): sql Error: 1054: Unknown column ‘User.full_name’ in ‘on clause’ [CORE\cake\libs\model\datasources\dbo_source.PHP,line 681]
有关我如何能做到这一点的任何帮助吗?谢谢
The implementation of virtualFields has a few limitations. First you cannot use virtualFields on associated models for conditions,order,or fields arrays. Doing so will generally result in an sql error as the fields are not replaced by the ORM. This is because it difficult to estimate the depth at which an associated model might be found. A common workaround for this implementation issue is to copy virtualFields from one model to another at runtime when you need to access them:
$this->virtualFields['name'] = $this->Author->virtualFields['name'];
要么
$this->virtualFields += $this->Author->virtualFields;
更多详细信息:http://book.cakephp.org/2.0/en/models/virtual-fields.html – 如果您计划实施他们建议的选项(对我而言看起来很不错),您应该查看“虚拟字段和模型别名”部分以避免字段名称冲突(即,如果您有在两个模型中称为full_name的字段并尝试发出使用这两个模型的查询,您将得到一个模糊的字段sql错误;使用别名可以避免这种情况.