PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] cakephp 2.4.6 and array output

 January 16, 2022     cakephp     No comments   

Issue

When I am try pick a manufacturer name and upload the manufacturer logo image I want the image name to the manufacturer name. But the file name is always Array.

Can someone check the below code and tell me what is wrong?

View.ctp

<fieldset>
                        <label>Manufacturer</label>
                        <?php


                        echo $this->Form->input('manufacturer_id', array('options' => $manufacturers));
                        ?>
                    </fieldset> 

controller.ctp

$manufacturer_name=$this->Manufacturer->findById($product_data['Product']['manufacturer_id']);
$new_image_name =  $manufacturer_name;

Solution

Cakephp 2 returns query result in the form of [ModelName] array.

You can access any field by using [ModelName][fieldName]

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

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

You can access Manufacturer name as (assuming manufacturer name is stored in name column)

$new_image_name =  $manufacturer_name['Manufacturer']['name'];

For further reading: https://book.cakephp.org/2.0/en/models/retrieving-your-data.html



Answered By - Sehdev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing