开发者

Accessing Has Many Through

Hi I'm a Rails programmer on my first foray in to CakePHP but seem to have hit a bit of a wall:

I have the following relationships:

BookStore has_many Books
Book belongs_to BookStore
Book has_and_belongs_to_many Authors
Author has_and_belongs_to_many Books

If I have a BookStore instance how can I get at t开发者_如何转开发he list of Authors for that BookStore so I can iterate over it in my view?

Many thanks in advance, Ash


Provided you have the relationships set up correctly in the models, CakePHP will do most of the work for you. A good guide on doing this can be found at http://book.cakephp.org/view/1000/Models.

$authors = $this->Author->find('all', array(
    'conditions' => array(
        'Bookstore.id' => $id,
    ),
));

$this->set('authors', $authors);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜