gpt4 book ai didi

php - yii2中如何显示关系数据

转载 作者:行者123 更新时间:2023-11-29 05:12:38 26 4
gpt4 key购买 nike

我无法理解关系在 yii 2 中是如何工作的

我在 mysql 数据库中有 2 个表,author 和 book。

book 有一个名为 author 的列,它通过外键链接到 author 表的 id。

我已经使用 gii 生成了 CRUD,我希望作者姓名出现在 ListView 中,以及作者姓名的下拉列表出现在创建和更新 View 中。

但即使在 ListView 中,我似乎也无法使关系正常工作。

这是我的代码

书籍模型:

<?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']);
}
}

图书搜索模型:

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

此外,这是 View 文件:

<?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',
];
}


}

我想我可能必须在 author/authorSearch 模型的某处更改一些内容。

谁能帮忙

谢谢

最佳答案

您还可以使用来自匿名函数的值将列添加到 gridview,如此处所述 http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html# $值(value)细节。例如,您可以在 gridview 中像这样显示作者姓名:

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

你也可以像这样返回一个 html 链接到作者的详细 View :

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

关于php - yii2中如何显示关系数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com