0

我正在尝试做的事情:我想使用 CGridView 打印表中的数据,但还有一件重要的事情 - 我需要将 $criteria 中的“bookId”的值更改为其他表中的“bookName”的值。

我怎样才能做到这一点?

问候。

/// here comes code from model

  public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.
        $criteria = new CDbCriteria;

     $criteria->compare('bookId',$this->bookId);
           $criteria->compare('bookBookdetailId', $this->bookBookdetailId);
           $criteria->compare('bookState', 1);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,

/// here is code from view


print_r($model);
 $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $model->search(), 'columns' => array(
        'bookCatalgoueNumber', 
        'bookDescription',
        'bookBookdetailId'
        ),

        )
);

/// the name of the model is Book, the other is Bookdetail (and so are the tables) 

/// 同样 bookBookdetailId 是链接到 Bookdetail.bookdetailId 的外键

// 我希望结果将书名 (bookdetailTitle) 替换为 bookBookdetailId

4

2 回答 2

0

在您的管理员中使用它

array('name' => 'book_id', 'header' => 'Book', 'value' =>'YourModel::getName($data["book_id"])')

getName()必须是您模型中的函数,如下所示

public function getName($book_id) 
{
    $sql = "SELECT book_name  from `Your Table Name` where `id`='$book_id'";
    $command = Yii::app()->db->createCommand($sql);
    $rs = $command->queryAll();
    return $rs;
}
于 2013-06-13T11:12:20.657 回答
0

如果您还想按该列搜索/排序,这可能是我能给您的最佳答案:

http://www.mrsoundless.com/php/yii/searching-and-sorting-a-column-from-a-related-table-in-a-cgridview/

更新(因为我猜你并没有真正理解链接。或者只是不想阅读它,因为它很长..)

我假设:

  • 您与 bookdetails 表有一个关系,称为“bookdetails”
  • 您的 bookDetails 表中有一个名为“名称”的列

第 1 步:将其添加到您的模型中:

private $_name = null;
public function getName()
{
    if ($this->_name === null && $this->bookDetails !== null)
    {
        $this->_name = $this->bookDetails->name;
    }
    return $this->_name;
}
public function setName($value)
{
    $this->_name = $value;
}

第 2 步:现在在 Book 模型的“规则”功能中添加“名称”到搜索规则

第 3 步:将 Book 模型的“搜索”功能更改为:

public function search() {
    $criteria=new CDbCriteria;
    $criteria->with = "bookDetails"; // Make sure you query with the post table.

    $criteria->compare('t.bookId',$this->bookId,true);
    $criteria->compare('t.bookState',$this->bookState);
    $criteria->compare('t.bookBookdetailId',$this->bookBookdetailId,true);
    $criteria->compare('bookDetails.name', $this->name,true);

    $sort = new CSort();
    $sort->attributes = array(
        'defaultOrder'=>'t.create_time DESC',
        'bookId'=>array(
            'asc'=>'t.bookId',
            'desc'=>'t.bookId desc',
        ),
        'bookState'=>array(
            'asc'=>'t.bookState',
            'desc'=>'t.bookState desc',
        ),
        'bookBookdetailId'=>array(
            'asc'=>'t.bookBookdetailId',
            'desc'=>'t.bookBookdetailId desc',
        ),
        'name'=>array(
            'asc'=>'bookDetails.name',
            'desc'=>'bookDetails.name desc',
        ),
    );

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

第 4 步:现在将“名称”添加到 CGridView 中的列数组中。应该变成这样:

'dataProvider' => $model->search(), 
'columns' => array(
    'bookCatalgoueNumber', 
    'bookDescription',
    'bookBookdetailId',
    'name',
),

第 5 步:阅读文章以了解发生了什么。第6步:享受

于 2013-06-13T13:24:58.963 回答