将另一个表的列值插入$ criteria作为当前模块表的列值

时间:2013-06-13 11:02:04

标签: php arrays printing yii

我想做什么: 我想使用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

的foregin键

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

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

更新(因为我猜你没有真正理解这个链接。或者根本就不想读它,因为它很长......)

我假设:

  • 您的bookdetails表名为'bookdetails'
  • 你的bookDetails表中有一个名为'name'的列

第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:现在将“name”添加到CGridView中的columns数组中。应该变得像:

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

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