dinkan
dinkan

Reputation: 83

Cakephp HABTM removing extra fields

I have models named Place and Category which are connected using an HABTM relationship. Now when i read specific fields from Place, i get additional fields in the result like this.

array(
'Place' => array(
    'id' => '8'
),
'Category' => array(
    (int) 0 => array(
        'id' => '2',
        'CategoriesPlace' => array(
            'id' => '673',
            'place_id' => '8',
            'category_id' => '2'
        )
    ),
    (int) 1 => array(
        'id' => '3',
        'CategoriesPlace' => array(
            'id' => '674',
            'place_id' => '8',
            'category_id' => '3'
        )
    ),
    (int) 2 => array(
        'id' => '5',
        'CategoriesPlace' => array(
            'id' => '675',
            'place_id' => '8',
            'category_id' => '5'
        )
    )
)

)

I dont want this CategoriesPlace array to be displayed. Any idea how?

Upvotes: 0

Views: 297

Answers (1)

L. Sanna
L. Sanna

Reputation: 6552

Two cakephp solutions:

$this->Place->recursive=-1;

before the find.

Or:

$this->Place->contain();

contain docs

Edit: For greater control of the output:

containable docs

Put:

public $actsAs = array('Containable');

in the model.

Then:

$this->Place->find('all', array(
    'contain' => array(
        'Place',
        'Category' => array(
            'fields'=>array('id')
        )
    )
  )
);

It may not work as cakephp adds all the foreign keys it needs to execute a query.

Upvotes: 1

Related Questions