0

I have the following cakephp query:

$this->request->data = $this->AnsMatrixLubrication->find('all', array(
    'conditions' => array('eq_model' => $eq_model)
));

The output is something like this:

Array ( 
    [0] => Array ( 
        [AnsMatrixLubrication] => Array ( 
            [id] => 228 
            [matrix_lubrication_id] => 1 
            [eq_model] => D11 R 
            [A] => x 
            [B] => x 
            [C] => x 
            [D] => x 
            [E] => x 
            [F] => x 
            [G] => x 
            [H] => x 
            [created] => 2014-08-15 20:40:06 
            [modified] => 2014-08-15 20:40:06 
        ) 
    ) 
    [1] => Array ( 
        [AnsMatrixLubrication] => Array ( 
            [id] => 229 
            [matrix_lubrication_id] => 2 
            [eq_model] => D11 R 
            [A] => y 
            [B] => y 
            [C] => y 
            [D] => y 
            [E] => y 
            [F] => y 
            [G] => y 
            [H] => y 
            [created] => 2014-08-15 20:40:06 
            [modified] => 2014-08-15 20:40:06 
        ) 
    )
)

But I need the output in Cake naming convention, I mean [Model][0][field_name].
Why doesn't the find method show the output in CakePHP naming convention? If it should be the default behavior and How can I solve that?

Edit
CakePHP version: 2.4.6

Thanks in advance.

2
  • The conventions depend on what you are trying to do, saving associated/single/many records, creating form elements, etc... The structure returned by Model::find() is in the expected and correct format. ps, please always mention your exact CakePHP version!
    – ndm
    Aug 16, 2014 at 12:15
  • Thanks for your answer @ndm. I understand the point but is there a way to achieve that I want to do? Because I need to show many records in an edit form (via $this->request->data) and the format output of find('all) doesn't match with the format of form fields.
    – Franchy
    Aug 19, 2014 at 15:25

2 Answers 2

0

That is the expected format

As shown in the documentation that's the expected return format for a find all call:

The results of a call to find('all') will be of the following form:

Array
(
  [0] => Array
    (
        [ModelName] => Array
            (
                [id] => 83
                [field1] => value1
                [field2] => value2
                [field3] => value3
            )

        [AssociatedModelName] => Array
            (
                [id] => 1
                [field1] => value1
                [field2] => value2
                [field3] => value3
            )

    )
)

Form fields and find calls aren't the same thing

The docs you've linked to are for the naming conventions of form fields (emphasis added):

If you need to specify multiple fields using the same field name, thus creating an array that can be saved in one shot with saveAll(), use the following convention:

echo $this->Form->input('Modelname.0.fieldname');
echo $this->Form->input('Modelname.1.fieldname');

This makes no reference to the structure of data returned by find-all (though if for example a find on Post hasMany Comment were made - the comments data would be in the format you're currently expecting).

0
0

The following link solve my problem.

http://nuts-and-bolts-of-cakephp.com/2008/10/27/editing-multiple-records-with-saveall/

With the function Set::combine() I can convert the find('all') output to form field convention.

Thanks a lot for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.