erlandmuchasaj
erlandmuchasaj

Reputation: 232

Association in cakephp

I have an App, which has several tables, but i want to connect 3 of them. So i have item, typologyand photo. Photo has one FK item_subitem_id which referest to both item or typologytable. photo table is this:

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

So if you see there is photo table has photo_item and photo_typology which are boolean. so if photo_item=1 it means the field in phoyo_item_typology_id is from item table, the same for photo_typology.

What i wanna know is: is this possible first? and second if yes, how can it be done? and if not, is there another way, more convinient way?

Upvotes: 0

Views: 52

Answers (1)

Mehrdad Dadkhah
Mehrdad Dadkhah

Reputation: 77

I think you want associate Photo model to PhotoItem if photo_item=1, is it true? if yes you can add condiiotns to your definition of associate in model.

plese see: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

something like:

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

Upvotes: 1

Related Questions