2014-02-13 50 views
0

我有一個應用程序,它有幾個表,但我想連接他們3。所以我有項目,類型和照片。照片有一個參考item或typologytable的FK item_subitem_id。照片表是這樣的:cakephp協會

public $photos = array(
    'photo_id' => array('type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'), 
    'photo_item' => array('type' => 'boolean', 'null' => false, 'default' => null), 
    'photo_typology' => array('type' => 'boolean', 'null' => false, 'default' => null), 
    'photo_item_typology_id' => array('type' => 'integer', 'null' => false, 'default' => null), 
    'photo_pic_path' => array('type' => 'text', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'), 
    'photo_type' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 20, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'), 
    'indexes' => array(
     'PRIMARY' => array('column' => 'photo_id', 'unique' => 1) 
    ), 
    'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB') 
); 

所以,如果你看到有照片表已經photo_item和photo_typology這是布爾。因此if photo_item=1這意味着phoyo_item_typology_id中的字段來自項目表,photo_typology的字段相同。

我想知道的是:這可能是第一次嗎?如果是的話,如何做呢?如果沒有,有沒有另一種方法,更方便的方法?

回答

1

我認爲你想要照片模型PhotoItem相關如果photo_item = 1,這是真的嗎?如果是的話,你可以添加condiiotns到模型中你的關聯定義。

普萊舍看到:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

類似:

public $hasMany = array(
     'Recipe' => array(
      'className' => 'Recipe', 
      'conditions' => array('Recipe.approved' => '1'), 
      'order' => 'Recipe.created DESC' 
     ) 
    );