Cakephp:不能将字符串偏移量用作数组

时间:2014-06-21 11:01:16

标签: php arrays cakephp

错误消息

  

错误:不能将字符串偏移用作数组
  文件:C:\ wamp \ www \ itsm \ app \ Model \ TaskAttribute.php
  行:48

错误行

47    foreach($items as $row){
48          $categoryId = $row['Category']['id'];
49          $item_id =  $row['Item']['id'];
50          $attr_id = $row['Attribute']['id'];

我的方法

public function getTree($branch_id = NULL){
        $tree = array();
        $items =  $this->find('list',array(
            'fields' => array('ItemAttribute.item_id','ItemAttribute.item_id'),
            'joins' =>array(
                        array(
                            'table' => 'item_attributes',
                            'alias' => 'ItemAttribute',
                            'type' => 'INNER',
                            'conditions' => 'TaskAttribute.item_attribute_id = ItemAttribute.id',
                         ),                 


                ),
                'recursive'=>-1
            )
        );  



        if(isset($items)){
            $assets = $this->Checklist->ChecklistAttribute->AssetAttribute->Asset->getList($branch_id ,array_keys($items));
            //pr($assets);die();
        }

        foreach($items as $row){
            $categoryId = $row['Category']['id'];
            $item_id =  $row['Item']['id'];
            $attr_id = $row['Attribute']['id'];

            if(!isset($tree[$categoryId])){
                $tree[$categoryId] = array('id'=>$categoryId ,'name'=>$row['Category']['name'] ,'Items'=>array());

            }


            $attrib = array('id'=>$row['Attribute']['id'] ,'name'=>$row['Attribute']['name'],'type'=>$row['Attribute']['type']);

            if(isset($items[$item_id])){

                if($asset = ifExist($assets ,$item_id)){
                    if(!isset($tree[$categoryId]['Items'][$item_id])){
                        $tree[$categoryId]['Items'][$item_id]  = array('id'=>$item_id ,'name'=>$row['Item']['model']);
                    }                       
                    if(!isset($tree[$categoryId]['Items'][$item_id]['Assets'])){
                        $tree[$categoryId]['Items'][$item_id]['Assets'] = array('id'=>$asset['id'] ,'name'=>$asset['serial'] ,'Attributes'=>array());
                    }
                    if(!isset($tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id])){
                        $tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id] = $attrib;
                    }
                    if($option_id = ifExist($row ,'AttributeOptions' ,'id')){
                        if(!isset($tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'])){
                            $tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'] = array();
                        }
                        $tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'][$option_id] = $row['AttributeOptions']['label'];
                    }
                }
            }else{

                if(!isset($tree[$categoryId]['Items'][$item_id])){
                    $tree[$categoryId]['Items'][$item_id]  = array('id'=>$item_id ,'name'=>$row['Item']['model'] ,'Attributes'=>array());
                }           
                if(!isset($tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id])){
                    $tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id] = $attrib;
                }       
                if($option_id = ifExist($row ,'AttributeOptions' ,'id')){   
                    if(!isset($tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'])){
                        $tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id]['options'] = array();
                    }
                    $tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id]['options'][$option_id] = $row['AttributeOptions']['label'];
                }
            }


        }

            if(isset($tree)){
                foreach($tree as $key=>$items){
                    if(!ifExist($items ,'Items')){
                    unset($tree[$key]);
                }
            }

            return $tree;
        }

    }   

1 个答案:

答案 0 :(得分:2)

查找列表返回列表

此问题中的代码:

$items =  $this->find('list',array(
    'fields' => array('key field','value field')
    ...
));

返回一个平面列表 - 即$items具有以下结构:

array(
    'key' => 'value string',
    'key2' => 'value string2',
);

因此 - 尝试将'value string'视为数组会导致错误。

foreach($items as $item_id => $item_id) {
    //                        ^ It's a string

    // So this won't work:
    // $categoryId = $item_id['Category']['id'];

    $categoryId = ...; // not in the results
    $item_id =  ...; // it's the key and the value returned by find list
    $attr_id = // not in the results

如上所述,代码试图获取的三位信息中的2位完全不在结果中。

使用find all返回数组

最简单的解决方案就是调用find all:

$items =  $this->find('all',array(
    //'fields' => array('key field','value field')
    ...
));

debug($items);
die;

在验证所有必需数据存在后,仅限制返回的字段。