C0ol_Cod3r
C0ol_Cod3r

Reputation: 949

CakePHP getting data form an ID which is inside another table?

Right, so my data returns in the following way,

  (int) 0 => array(
    'MODEL-XX' => array(
        //DATA HERE
        'xxs_id' => '11',
        'aas_id' => '44',
        'vvs_id' => '2'
    ),
    'xxs' => array(
        'id' => '11',
        'customername' => 'XX name here'
    ),
    'aas' => array(
        'id' => '44',
        'clientname' => 'aa name here',
        'xxs_id' => '11'
    ),
    'vvs' => array(
        'id' => '2',
        'start' => '1405296000',
        'end' => '1405814400',
        'users_id' => '1'
    )

This works fine, but I want to know how to link my users table to this model. So the details of each user for my VV model would become apart of the data. My MODEL-XX does not have any links with my users table so the place I need to call in the users details are held with my VV model.

I have been looking into this but have not been able to find a simple easy method for doing this?

I was thinking that this would be doable with my model, so I opened my my XX model and added the following within my '$belongsTo' section,

    'Users' => array(
        'className' => 'Users',
        'foreignKey' => 'vvs.users_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )

So is there a easy method for linking data like this?

Please give me time, if I have not explained myself right or not enough data, please tell me and let me fix or explain better.

Thanks,

Upvotes: 0

Views: 124

Answers (1)

Alex Stallen
Alex Stallen

Reputation: 2252

Either set your recusive higher:

$this->MODEL-XX->recursive = 1; //or 2

Or and this should be your prefered way to go, start using the containable behaviour: http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html

In your appModel:

public $actsAs = array('Containable');

Then try this find:

$this->MODEL-XX->recursive = -1;

$data = $this-MODEL-XX>find(
        'all', array(
            'conditions' => $conditions,
            'contain' => array('xxs', 'aas', 'vvs', 'user')
        )
    );

I might be 'vvs.user' but I forgot what is used for deeper models

Upvotes: 1

Related Questions