如何从链接的模型中检索字段 [英] How to Retrieve fields from linked Models

查看:60
本文介绍了如何从链接的模型中检索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个数据库表:

I have the following three database tables:

Products
########
id
title
artist_id

Arists
######
id
profile
person_id

People
######
id
first_name
last_name

在我的产品模型中,如何创建一种方法来返回产品标题和艺术家的 first_name

In my Product model how do I create a method to return the product title along with the artist's first_name?

我建立了以下模型关联:

I have set up the following model associations:

Product belongs to Artist
Artist belongs to Person


推荐答案

包含绝对是用于过滤相关记录的方法。确保在模型或app_model中添加$ actsAs = array('Containable')。

Containable is definitely the way to go for filtering related records. Make sure to add $actsAs = array('Containable') into your model or app_model.

然后您可以执行以下操作:

Then you can do things like:

$this->Product->find('all', array(
    'contain' => array(
        'Artist' => array(
            'Person' => array(
                'id',
                'first_name'
            )
        )
    )
));

这篇关于如何从链接的模型中检索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆