0

我在某个关联对象上有一个方法,我想使用它,但是关联对象需要设置它的 id 以便该方法可以工作

A->B->method()

而不是做

$a = A->find('first');
A->B->id = $a['B'][id'];
A->B->method();

有简单的方法吗?因为既然A属于B,为什么我不能简单的调用

A->B->method();

让模型关系弄清楚

4

1 回答 1

0

从您的评论来看,您似乎正在尝试从 A 中找到 B 的特定字段,如果您Containable在模型中正确设置,您可以执行以下操作:

A->contain('B.id'); // change B.id to B.whateverfield you are trying to fetch
$a = A->find('first');
echo $a['B']['id']; // change id to whateverfield

http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html# contains-deeper-associations

于 2012-08-17T03:19:19.257 回答