Yii显示GridView中的每个记录

时间:2015-03-26 16:23:02

标签: php mysql yii

在我的GridView中,我想显示表'回复'的所有记录。该表与表'author'和'task'有关系,并不是每个回复都有一个任务。 表'task'与另一个名为'concern'的表有关系。

这是我的模特的关系()回复:

public function relations() {
    return array(
        'trak' => array(self::HAS_MANY, 'Task', 'reply_id', 'condition' => 'task.deleted<>1'),
        'author' => array(self::BELONGS_TO, 'Author', 'author_id'),
        );
}

我的模型Reply的search()方法有以下代码:

public function search() {

    $criteria = new CDbCriteria;

    $criteria->with = array(
        'author' => array('select' => 'id, name, role, office_id, abk', 'together' => false),
        'author.office' => array('select' => 'id, name'),
        'task' => array('select' => 'id, concern_id', 'together' => true),
        'task.concern' => array('select' => 'id, classification_id', 'alias' => 'concern'),
    );

    $criteria->compare('t.id', $this->id);
    $criteria->compare('t.create_time', $this->create_time);
    $criteria->compare('t.create_date', $this->create_date, true);
    $criteria->compare('t.office.id', $this->search_office);
    $criteria->compare('t.author_id', $this->author_id);
    $criteria->compare('t.rel', $this->rel, true);
    $criteria->compare('t.author_id', $this->author_id);
    $criteria->compare('t.lektor_id', $this->lektor_id);
    $criteria->compare('t.issue_id', $this->issue_id);
    $criteria->compare('t.reply_text', $this->reply_text, true);
    $criteria->compare('t.deleted', $this->deleted);
    if (EWMParam::getValue(EWMParam::MODUL_SCHLAGWORTE))
        $criteria->compare('t.tags', $this->tags, true);
    $criteria->compare('t.text_name', $this->text_name, true);
    $criteria->compare('t.use_count', $this->use_count);
    $criteria->compare('concern.classification_id', $this->classification_id);
    $criteria->compare('t.update_time', $this->update_time);
    $criteria->compare('t.update_user', $this->update_user);
    $criteria->compare('t.global', $this->global);
    if (EWMParam::getValue(EWMParam::MODUL_confirmationN))
        $criteria->compare('t.confirmation', $this->confirmation);
    $criteria->compare('t.confirmation_text', $this->confirmation_text, true);
    $criteria->compare('t.use', $this->use, true);



    $pagination = EWMPageSortFilterHelper::getPagination($this);
    $sort = new CSort();
    $sort->defaultOrder = 't.id DESC';
    $sort->attributes = array(
        'global' => 't.global',
        'search_office' => 'office.name',
        'id' => 't.id',
        'text_name' => 't.text_name',
        'confirmation' => 't.confirmation',
        'author_id' => 'author.name',
        'create_date' => 't.create_date',
        'tags' => 't.tags',
        'use' => 't.use',
        'classification_id' => 'classification_id',
    );
    $sort->applyOrder($criteria);

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
        'pagination' => $pagination,
        'sort' => $sort
    ));
}

在我的GridVew中,只显示带有任务的回复,并且所有过滤器都能正常工作。但我希望显示所有带有任务的回复和所有没有任务的回复。如果我 在$ criteria-&gt;数组的search()方法中删除元素'task'和'task.concern',所有回复都是显示的。但是行的过滤器 来自关系'task.concern'的'分类'不起作用。从逻辑上讲,我在“where子句”中收到错误“未找到列:1054未知列'concern.classification_id'。”

是否可以显示所有回复并按分类过滤这些回复?你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您应该了解Yii关系如何运作。

我想到的一件事是,在with()属性中添加关系时,生成的SQL可能包含INNER JOIN。

这就是为什么当包含关系时你没有得到任何没有任务的回复。

为了解决这个问题,你应该确保生成的SQL正在使用LEFT JOIN。

您可以使用关系的joinType属性来执行此操作: http://www.yiiframework.com/doc/api/1.1/CActiveRelation#joinType-detail

你可以在那里指定LEFT JOIN。

    'task' => array('select' => 'id, concern_id', 'together' => true, 'joinType'=>'LEFT JOIN'),
    'task.concern' => array('select' => 'id, classification_id', 'alias' => 'concern', 'together' => true, 'joinType'=>'LEFT JOIN'),

这可能会成功。但是,如果它没有像预期的那样工作,你可能需要更多的调整。

如果要调试实际发生的情况,可以将错误的字段/表名放在导致数据库错误的条件中,然后查看已执行的SQL代码并查看表的连接方式

建议的链接:
http://www.yiiframework.com/doc/guide/1.1/en/database.arr http://www.yiiframework.com/wiki/527/relational-query-lazy-loading-and-eager-loading-with-and-together/ http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation