CakePHP:不能将标量值用作数组

时间:2014-05-26 07:57:24

标签: php arrays cakephp

我正在尝试这段代码:

我的代码段

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

但我收到此错误

错误

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]

1 个答案:

答案 0 :(得分:1)

commentor所述,$ query不是数组,而beforeFind返回boolean。所以下面的一行有问题。

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

更正是

没有使用调用父方法,因为它被覆盖它以实现自己的逻辑。

所以正确的代码是

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