Vinculación de modelos

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?

preguntado el 08 de noviembre de 11 a las 15:11

2 Respuestas

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

Por ejemplo, la directriz Artist Modelo:

<?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

respondido 08 nov., 11:19

This is the association I have setup, but want to access the artist's first name in my Product model. Here is a related pregunta. Is this the correct way to setup the tables? - más actualizado

I guess it is something like:

Product has an Artist:

<?php
class Product extends AppModel {
    var $hasOne = array('Artist');
}

Artist belongs to a Person and has many Products.

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

    var $belongsTo = array('Person');

    var $hasMany = array('Product');
}

Person has an Artist.

<?php
class Person extends AppModel {

    var $hasOne = array('Artist');

}

respondido 08 nov., 11:20

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.