0

私はアートギャラリーのWebアプリケーションを開発しており、データモデルを正しく設定したことを確認したいと思います。モデルの関連付けとデータモデリング

私はpeopleテーブルとartistsテーブルを持っています。 peopleの一部はartistsであり、一部はそうではありません。

また、私はproductsテーブルを持っていて、それぞれproductartistに属しています。

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

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

people 
****** 
id 
first_name 
last_name 

Productモデルにfind()メソッドを実行するとき、私は、アーティストの名前を取得できるようにする必要があります。

は以下の表の簡易版です。

私はデータモデルを正しく設定していますか?もしそうなら、不要なデータを大量に取り込まずにアーティスト名を取得する最良の方法は何ですか?

+0

"人"テーブルの目的は何ですか?あなたのアプリでアーティストではない人々の役割は何ですか? – dhofstet

答えて

3

それはCakePHPのbindModelを使用することが可能です& unbindModelは、Cakeのオートマティックな良さに依存しないリレーションシップを構築します。

あなたは製品モデルに検索をすることに興味を持っていたので、私はコントローラ製品から呼び出すことができます例着手します:ここで何が起こっている

// 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' 
) 
)); 

を?

製品モデルの関係アーティストモデルとの関係をまずアンバインドします。

は第二に、我々は明示的に関係を定義し、ケーキの自動forigenKey接続を無効にすることでアーティストモデルをバインドします。

最後に、我々は製品モデルに見つけるだけの「first_nameの」&「last_nameの」を要求する「最初」を行います。

-1

各「製品が」「アーティスト」と各「アーティスト」hasAndBelongsToManyの「製品」に属する あなたはまた、次の表が必要になります。

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

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

+0

アーティスト 'hasMany'プロダクト。 'hasAndBelongsToMany'関係は必須ではなく、テーブルも必要ありません – Leo

関連する問題