cakephpcakephp-1.3cakephp-model

Cake Php Read function to retrieve result as 1-Dimensional array


Cake Php Read function to retrieve result as simple array

$result = $this->Model->read('id, name, title', $id);

It will result as

Array
(
    [Model] => Array
    (
        [id] => 1
        [name] => test
        [title] => New Head title
    )

)

It there any way to retrieve result array directly from query as below

Array
    (
        [id] => 1
        [name] => test
        [title] => New Head title
    )

Without using any temp storage of a variable.


Solution

  • Just run the result through a Set::extract call, like this:

    $result = $this->Model->read('id, name, title', $id);
    $result = Set::extract('/Model', $result);
    

    Set is a very powerful class, I suggest you read on it. :) Cheers.