将模型链接在一起 [英] Linking Models Together

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

问题描述

我在数据库中有以下三个表:

I have the following three tables in a database:

Products
********
id
title
artist_id

Artists
*******
id
profile
rating
person_id

People
******
id
first_name
last_name

每位艺术家可以有多个产品。

Each artist can have many products.

每位艺术家只有一个人。

Each artist has only one person.

每个产品只有一个艺术家。

Each product has only one artist.

这三个表的模型之间设置的正确关联是什么?

What are the correct associations to set up between the models for these three tables?

推荐答案

艺术家对产品:一对多

对艺术家的产品:一对一

艺术家到人:一对一

Artist to Products: One to Many
Product to Artist: One to One
Artist to Person: One to One

例如, Artist Model:

<?php
class Artist extends AppModel {
    var $name = 'Artist';

    var $belongsTo = array(
        'Person' => array(
            'className' => 'Person',
            'foreignKey' => 'person_id'
        )
    );

    var $hasMany = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'product_id'
        )
    );
}
?>

DOC

DOC

这篇关于将模型链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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