1

I'm looking for a way to have the results of a query that uses the Containable behavior to be formatted the same way as find('list') results are. E.g.:

$this->ModelA->find('all', array(
    'contain' => array(
        'ModelB' => array(
            'ModelC' => array(
                // This does NOT work but I need ModelC's fields to be formatted as an associative array where IDs are keys and names are values
                'fields' => array('id' => 'name')
            )
        )
    )
));

NOTE: Setting recursive to 1 or 2 instead of using Containable is not an option unless there's no available solution with Containable.

lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48
  • Without showing the current response format, or specifying your associations, it's not possible to give you a specific answer. Please add that to the question and tag with the version this question relates to. – AD7six Sep 15 '15 at 07:18

2 Answers2

1

I'm not sure you can get the returned contained data back in the format you want, but you can alter it after finding it. The way Cake actually builds the return from find('list') is using Hash::combine(). So you can alter the returned data using this to make the data look list a find('list') return:-

$data = [
    [
        'id' => 1,
        'name' => 'Foo'
    ],
    [
        'id' => 2,
        'name' => 'Bar'
    ]
];
$results = Hash::combine($data, '{n}.id', '{n}.name');

This would return:-

[
    1 => 'Foo',
    2 => 'Bar'
]

Not sure exactly what your find('all') data looks like as you've not supplied the associations, but guess you can do something like this:-

foreach ($data as &$row) {
    $row['ModelB']['ModelC'] = Hash::combine(
        $row['ModelB']['ModelC'], 
        '{n}.id', 
        '{n}.name'
    );
}
drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
0

$this->ModelA->Behaviors->load('Containable');

$this->ModelA->find('all', array( 'contain' => array( 'ModelB' ,'ModelC'=>array( 'fields' => array('id', 'name')) ) ) );

Please make sure you have created association of model

ASH
  • 77
  • 9
  • 1
    This would result in ModelC results being in this format: array('id' => 1, 'name' => 'SomeName'). What I want is this format: array(1 => 'SomeName'). Just like find('list') results format, as stated in the question. Please feel free to correct me if I misunderstood something from your answer. – lucasnadalutti Sep 15 '15 at 03:10