查询Yii2中的搜索功能

时间:2015-10-29 04:32:32

标签: mysql yii2

当添加收入或支出时,对名为TabGLEntry的表进行两次输入(两个条目除了借记,贷方之外具有最相似的值,两者都具有相同的凭证否)。我想显示TabGLEntry的值(每个凭证只能显示一个值)。所以这是我对这个场景的SQL查询

SELECT * 
FROM  `tabGLEntry` 
GROUP BY  `voucher_no` 

当我在我的数据库上尝试这个时,它按预期工作。现在在Yii2中实现它 我在搜索模型(TabglentrySearch.php)中尝试了这个查询

public function search_all($params)
    {
        //~ $query = TabGLEntry::find();
        $query = TabGLEntry::find()->groupBy(['voucher_no'])->all();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'creation' => $this->creation,
            'modified' => $this->modified,
            'voucher_category' => $this->voucher_category,
            'credit' => $this->credit,
            'transaction_date' => $this->transaction_date,
            'clearance_date' => $this->clearance_date,
            'debit' => $this->debit,
            'posting_date' => $this->posting_date,
            'payment_method_id' => $this->payment_method_id,
            'reference_date' => $this->reference_date,
            'is_editable' => $this->is_editable,
            'created_by' => $this->created_by,
            'created_at' => $this->created_at,
            'updated_by' => $this->updated_by,
            'updated_at' => $this->updated_at,
            'is_deleted' => $this->is_deleted,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'entity_name', $this->entity_name])
            ->andFilterWhere(['like', 'modified_by', $this->modified_by])
            ->andFilterWhere(['like', 'account', $this->account])
            ->andFilterWhere(['like', 'fiscal_year', $this->fiscal_year])
            ->andFilterWhere(['like', 'against_voucher', $this->against_voucher])
            ->andFilterWhere(['like', 'against_voucher_type', $this->against_voucher_type])
            ->andFilterWhere(['like', 'is_opening', $this->is_opening])
            ->andFilterWhere(['like', 'against', $this->against])
            ->andFilterWhere(['like', 'voucher_type', $this->voucher_type])
            ->andFilterWhere(['like', 'is_advance', $this->is_advance])
            ->andFilterWhere(['like', 'remarks', $this->remarks])
            ->andFilterWhere(['like', 'cost_center', $this->cost_center])
            ->andFilterWhere(['like', 'reference_no', $this->reference_no])
            ->andFilterWhere(['like', 'voucher_no', $this->voucher_no]);

        return $dataProvider;
    }

现在我收到了错误

PHP Fatal Error – yii\base\ErrorException

Call to a member function andFilterWhere() on a non-object

1 个答案:

答案 0 :(得分:2)

根据active record find api查找返回一个yii\db\ActiveQueryInterface实例,可以通过在调用yii\db\ActiveQueryInterfaceone()之前调用all()中定义的方法进一步自定义该实例返回填充的ActiveRecord实例。

请删除all(),然后添加过滤器。