cakephp 2.4.6和数组输出

时间:2018-07-06 06:44:37

标签: cakephp

当我尝试选择制造商名称并上传制造商徽标图像时,我希望将图像名称更改为制造商名称。但是文件名始终是Array。

有人可以检查下面的代码并告诉我什么地方出了错吗?

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;

2 个答案:

答案 0 :(得分:2)

Cakephp 2以 [ModelName] 数组的形式返回查询结果。

您可以使用[ModelName] [fieldName]

来访问任何字段
Array
(
    [ModelName] => Array
        (
            [id] => 83
            [field1] => value1
            [field2] => value2
            [field3] => value3
        )

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

您可以使用以下名称访问制造商名称(假设制造商名称存储在名称列中)

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

进一步阅读: https://book.cakephp.org/2.0/en/models/retrieving-your-data.html

答案 1 :(得分:1)

我无法具体回答,因为我不知道Manufacturer对象包含什么,但是...试试吧,尽管有点麻烦。在controller.ctp中添加print_r行:

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

它应该向您显示$manufacturer_name变量的构成方式,然后让您选择实际要查找的内容,我怀疑这将类似于:

$new_image_name =  $manufacturer_name['some_index'];