Asociaciones de modelos y modelado de datos

I am developing a web app for an art gallery, and I want to check I have set up the data model correctly.

Tengo un people mesa y un artists table. Some of the people son artists and some are not.

También tengo una products table and each product pertenece a uno artist.

Below is a simplified version of the tables:

products
********
id
title
artist_id

artists
*******
id
profile
rating
person_id

people
******
id
first_name
last_name

I need to be able to retrieve the name of the artist when performing the find() método en el Product modelo.

Have I set up the data model correctly and if so what is the best way to retrieve the artist's name without getting lots of unwanted data?

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

What's the purpose of the "people" table? What's the role of the people who are not artists in your app? -

2 Respuestas

It's possible to use CakePHP's bindModel & unbindModel to build up relations that don't rely on Cake's automagic goodness.

Since you were interested in doing the find on the Producto model, I'll set out an example that can be called from the Productos controlador:

// unbind Artist
$this->Product->unbindModel(array(
  'belongsTo' => array('Artist')
));

// bind Artist & Person using explicit conditions
$this->Product->bindModel(array(
  'hasOne' => array(
    'Artist' => array(
      'foreignKey' => false,
      'conditions' => array('Artist.id = Product.artist_id')
    ),
    'Person' => array(
      'foreignKey' => false,
      'conditions' => array('Person.id = Artist.person_id')
    ),
  )
));

// return the person
$person = $this->Product->find('first', array(
  'conditions' => array(
    'Product.id' => 1 // or any other condition
  ),
  'fields' => array(
    'Person.first_name',
    'Person.last_name'
  )
));

¿Que esta pasando aqui?

Firstly, we unbind the Producto model's relationship with the Artist modelo.

Secondly, we bind the Artist y Persona models by explicitly defining the relationships and disabling Cake's automatic forigenKey connections.

Lastly, we do a 'first' find on the Producto model and only request the 'nombre de pila'&'apellido' del Persona .

respondido 09 nov., 11:04

Each 'Product' belongs to an 'Artist' and each 'Artist' hasAndBelongsToMany 'Product' You will also need the following table:

artists_products
****************
id
product_id
artist_id

http://book.cakephp.org/#!/view/1044/hasAndBelongsToMany-HABTM

respondido 08 nov., 11:20

Artist 'hasMany' Product. A 'hasAndBelongsToMany' relationship is not required nor the table - Leo

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