2016-05-17 50 views
0

我無法理解的關係在警予2如何顯示在yii2

是如何工作的我在MySQL數據庫中,作者和書籍2個表關係數據。

本書有一個名爲author的列,它通過外鍵鏈接到作者表的id。

我已經使用gii生成了CRUD,並且我希望作者名稱出現在列表視圖中,以及作者姓名在創建和更新視圖中的下拉列表中。

但我似乎無法得到即使在列表視圖中工作的關係。

這裏是我的代碼

書型號:

<?php 

namespace app\models; 

use Yii; 
use app\models\Author; 

/** 
* This is the model class for table "book". 
* 
* @property integer $id 
* @property string $name 
* @property integer $author 
*/ 
class Book extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'book'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['name', 'author'], 'required'], 
      [['author'], 'integer'], 
      [['name'], 'string', 'max' => 11] 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => 'ID', 
      'name' => 'Name', 
      'author' => 'Author', 
     ]; 
    } 

    public function getAuthor() 
    { 
     return $this->hasOne(Author::className(), ['id' => 'author']); 
    } 
} 

的BookSearch型號:

<?php 

namespace app\models; 

use Yii; 
use yii\base\Model; 
use yii\data\ActiveDataProvider; 
use app\models\Book; 

/** 
* BookSearch represents the model behind the search form about `app\models\Book`. 
*/ 
class BookSearch extends Book 
{ 
    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['id', 'author'], 'integer'], 
      [['name'], 'safe'], 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function scenarios() 
    { 
     // bypass scenarios() implementation in the parent class 
     return Model::scenarios(); 
    } 

    /** 
    * Creates data provider instance with search query applied 
    * 
    * @param array $params 
    * 
    * @return ActiveDataProvider 
    */ 
    public function search($params) 
    { 
     $query = Book::find(); 
     $query->joinWith('author'); 

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

     var_dump($dataProvider); 
     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, 
      'author' => $this->author, 
     ]); 

     $query->andFilterWhere(['like', 'name', $this->name]); 

     return $dataProvider; 
    } 
} 

而且,這裏的視圖文件:

<?php 

use yii\helpers\Html; 
use yii\grid\GridView; 

/* @var $this yii\web\View */ 
/* @var $searchModel app\models\BookSearch */ 
/* @var $dataProvider yii\data\ActiveDataProvider */ 

$this->title = 'Books'; 
$this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="book-index"> 

    <h1><?= Html::encode($this->title) ?></h1> 
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?> 

    <p> 
     <?= Html::a('Create Book', ['create'], ['class' => 'btn btn-success']) ?> 
    </p> 

    <?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 
     'columns' => [ 
      ['class' => 'yii\grid\SerialColumn'], 

      'id', 
      'name', 
      [ 
       'attribute' => 'author', 
       'value'  => 'author.name', 
      ], 

      ['class' => 'yii\grid\ActionColumn'], 
     ], 
    ]); ?> 

</div> 

作者型號:

<?php 

namespace app\models; 

use Yii; 

/** 
* This is the model class for table "author". 
* 
* @property integer $id 
* @property string $name 
*/ 
class Author extends \yii\db\ActiveRecord 
{ 
    /** 
    * @inheritdoc 
    */ 
    public static function tableName() 
    { 
     return 'author'; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function rules() 
    { 
     return [ 
      [['name'], 'required'], 
      [['name'], 'string', 'max' => 200] 
     ]; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'id' => 'ID', 
      'name' => 'Name', 
     ]; 
    } 


} 

我想我可能需要改變作者/作者搜索模型中的某處。

有人能幫助

感謝

+0

你應該閱讀本。例如,你可以在GridView顯示作者的名字像這樣:http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#working-with-model-relations – soju

+0

'[ 'attribute'=>'author', 'value'= > $ model-> author-> name ]' –

+0

也發佈您的作者模型代碼。 –

回答

0

你好你的BookSearch模式,請在下面添加這樣的代碼。

$query->joinWith(array('author')); 
$query->andFilterWhere(['id' => $this->id,]) 
->andFilterWhere(['like', 'author.author_name', $this->author]); 

而在你的視圖文件中請使用下面給出的代碼,下面的代碼是查看文件裏面的grid attrubute。

[ 
     'attribute' => 'author', 
     'value' => 'author.name', 
] 

我希望這會幫助你。我希望作者表名存儲在名稱列中。

+0

'$ this-> id'爲空,所以這不起作用。謝謝 – TEster

+0

@ TEster請嘗試添加id字段的安全規則數組它可以幫助你這樣[['id','name'],'safe'] –

+0

仍然沒有運氣。我已將上述內容添加到視圖文件中。這是我現在在BookSearch的公共函數規則中的規則方法() { return [[['id','author'],'integer'], [['id','name']''safe '], ]; }' – TEster

0

您可以使用關係名稱訪問任何crud視圖文件中的關係表數據。 $模型 - > relName->屬性名稱。

,您可以在GridView控件以下面的方式訪問關係表數據:

<?= GridView::widget([ 
'dataProvider' => $dataProvider, 
'filterModel' => $searchModel, 
'columns' => [ 
    [ 
     'attribute' => 'author', 
     'value'=>'author.author_name', //relation name with their attribute 
    ] 
], 

]);

0

這爲我工作 - 與BookSearch模型中:現在

public function search($params) 
{ 

    $dataProvider = new \yii\data\SqlDataProvider([ 
     'sql' => 'SELECT book.id as id ,book.author_fk, book.name as book_name , author.name as author_name' . 
        ' FROM book ' . 
        'INNER JOIN author ON (book.author_fk = author.id) ' 
    ]); 


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



    return $dataProvider; 
} 

唯一的問題是獲得ActionColumn正常工作,他們目前鏈接到錯誤的ID,幫助任何人。

1

您還可以使用來自匿名函數的值向gridview添加列,如此處所述http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html# $ value-detail。

<?= GridView::widget([ 
'dataProvider'=>$dataProvider, 
'filterModel'=>$searchModel, 
'columns'=>[ 
    [ 
     'attribute'=>'author.name', 
     'value'=>function ($model, $key, $index, $column) { 
      return $model->author->name; 
     }, 
    ], 
    //...other columns 
]); 
?> 

你也可以返回一個HTML鏈接到作者的詳細視圖這樣的:

//... 
'columns'=>[ 
    [ 
     'attribute'=>'author', 
     'value'=>function ($model, $key, $index, $column) { 
      return Html::a($model->author->name, ['/author/view', 'id'=>$model->author->id]); 
     }, 
    ], 
    //... 
], 
//... 
+0

這似乎很整齊,謝謝! – TEster