Indzi
Indzi

Reputation: 390

CakePHP: Cannot use a scalar value as an array

I am trying this code:

My Code snippet

public function beforeFind($query) {
        $query = parent::beforeFind($query);
        if(!isset($query['conditions'])){
            $query['conditions'] = array();
        }
        if(!$this->Authake->isAdmin()){
            if(empty($query['conditions']) || is_array($query['conditions'])){
                $query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
            }
        }
        return $query;
    }

But I am getting this error

Error

Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]

Warning (2): Cannot use a scalar value as an array [APP\Model\Ticket.php, line 56]

Upvotes: 1

Views: 693

Answers (1)

Indzi
Indzi

Reputation: 390

As said by commentor that $query is not an array, and beforeFind is returning boolean. So the following line is having problem.

$query = parent::beforeFind($query);

The correction is

There is no use of calling parent method as it is overridden it to implement own logic.

so the correct code is

public function beforeFind($query) {
            if(!isset($query['conditions'])){
            $query['conditions'] = array();
        }
        if(!$this->Authake->isAdmin()){
            if(empty($query['conditions']) || is_array($query['conditions'])){
                $query['conditions'] = array('organ_id' => $this->Organ->Group->getUserBranches());
            }
        }
        return $query;
    }

Upvotes: 1

Related Questions