2

Error message

Error: Cannot use string offset as an array
File: C:\wamp\www\itsm\app\Model\TaskAttribute.php
Line: 48

lines of error

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

My Method

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;
        }

    }   
5
  • Did you check with debug($items); to see what structure the $items array has? Commented Jun 21, 2014 at 11:17
  • yes checked... its as follows: array( (int) 35 => '35', (int) 40 => '40', (int) 50 => '50', (int) 51 => '51', (int) 53 => '53', (int) 54 => '54', (int) 55 => '55', (int) 56 => '56', Commented Jun 21, 2014 at 11:19
  • Dumping entity encoded code, seriously? And isn't it obvious that the array isn't formatted the way your code is expecting it? Commented Jun 21, 2014 at 11:33
  • @ndm I used debug for both '$items' and '$row'... they are fetching data.... the problem is with the line '$categoryId = $row['Category']['id'];' Commented Jun 21, 2014 at 11:36
  • 1
    The problem is how your data is formatted. Please read the CookBook on what a find('list') call returns. No offense, but that's CakePHP basics, and SO isn't really the place to teach stuff like that. Commented Jun 21, 2014 at 11:40

1 Answer 1

2

Find list returns a list

This code in the question:

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

Returns a flat list - i.e. $items is of the structure:

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

As such - attempting to treat 'value string' as an array results in an error.

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

As indicated above, 2 of the three bits of info the code tries to obtain aren't in the results at all.

Use find all to return an array

The simplest solution is just call find all:

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

debug($items);
die;

Only restrict the fields returned after verifying that all the required data is present.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.