Cakephp: can Model-> find ('all') return results without model name

Using Model-> find ('all') returns an array with the following structure:

array(
  0 => array('Model1' => array(/* Model1 fields*/), 'Model2' => array(/* Model2 fields*/), ...),
  1 => array('Model1' => array(/* Model1 fields*/), 'Model2' => array(/* Model2 fields*/), ...),
  ...)

      

When one model is requested (i.e. recursive = -1), is it possible to return the results as an array with the following structure:

array(0 => /* Model1 fields*/, 1 => /* Model1 fields*/, etc...)

      

I thought I was reading this somewhere a while ago, but can't figure out how to do it or if it's possible.

+2


source to share


3 answers


You can also change this aftersave method of the model to return $ data ['Modelname'] after the query is done ... Basically it would be basically an array offset and you would only have $ arrayname ['fieldname'], and not $ arrayname ['Model'] ['fieldname']. Is this what you asked?



+1


source


How about this? works in php 5.4+

$result = array_map( function ($elem) {
        return $elem['YourModelName'] ;
    } ,$this->Tagcloud-> find('all')
);

      



For older php versions you need to iterate after getting the results

+2


source


Perhaps you are thinking of related models that can be returned this way? AFAIK Cake's query results are pretty standardized and that's a good thing.

array(
    0 => array(
        'Model' => array(
            'id',
            'field1',
            ...
         ),
        'belongsTo/hasOneModel' => array(
            'id',
            'field1',
            ...
         )
        'habtm/hasManyModel' => array(
            0 => array(
                'id',
                'field1',
                ...
            ),
            1 => array(
                ...
            )
        )
    ),
    1 => array(
        'Model' => array(
            ...
        ),
        ...
    )
)

      

As you can see, related HABTM or hasMany models are returned in a "flat" array, but the base model must always contain the model name.

+1


source







All Articles