在 Yii2 中访问数据
Accessing data in Yii2
我是 Yii2 框架的新手,我现在正在尝试使用关系访问 Listview 中的数据。有人可以解释为什么我的代码不起作用。
我想找到属于文档的标签。
这是我的数据库的截图:
这是我的关系:
public function getTags() {
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('tbl_document_tag', ['document_id' => 'id']);
}
这是我的控制器:
public function actionTag() {
$model = new Search();
$tag = Yii::$app->getRequest()->getQueryParam('tag');
//Documents
$documentModel = new Document;
$documentSearch = $model->searchDocumentsByTag($documentModel, $tag);
return $this->render('results', [
'model' => $model,
'documentSearch' => $documentSearch,
'documentModel' => $documentModel
]);
}
这是我的观点:
public function searchDocumentsByTag($documentsModel, $keyword) {
$query = Document::find()
->with('tags')
->andFilterWhere([
'or',
['like', 'tags.state', 1],
['like', 'tags.slug', $keyword],
]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
我收到以下错误:
Database Exception – yii\db\Exception
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tags.state' in 'where clause'
The SQL being executed was: SELECT COUNT(*) FROM tbl_document
WHERE (tags
.state
LIKE '%1%') OR (tags
.slug
LIKE '%steekwoord%')
Error Info: Array
(
[0] => 42S22
1 => 1054
[2] => Unknown column 'tags.state' in 'where clause'
)
↵
Caused by: PDOException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tags.state' in 'where clause'
你应该简单地使用 joinWith()
instead of with()
:
This method allows you to reuse existing relation definitions to perform JOIN queries. [...] Note that because a JOIN query will be performed, you are responsible to disambiguate column names.
例如:
$query = Document::find()
->joinWith('tags tags')
->andFilterWhere([
'or',
['like', 'tags.state', 1],
['like', 'tags.slug', $keyword],
]);
我是 Yii2 框架的新手,我现在正在尝试使用关系访问 Listview 中的数据。有人可以解释为什么我的代码不起作用。 我想找到属于文档的标签。
这是我的数据库的截图:
这是我的关系:
public function getTags() {
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('tbl_document_tag', ['document_id' => 'id']);
}
这是我的控制器:
public function actionTag() {
$model = new Search();
$tag = Yii::$app->getRequest()->getQueryParam('tag');
//Documents
$documentModel = new Document;
$documentSearch = $model->searchDocumentsByTag($documentModel, $tag);
return $this->render('results', [
'model' => $model,
'documentSearch' => $documentSearch,
'documentModel' => $documentModel
]);
}
这是我的观点:
public function searchDocumentsByTag($documentsModel, $keyword) {
$query = Document::find()
->with('tags')
->andFilterWhere([
'or',
['like', 'tags.state', 1],
['like', 'tags.slug', $keyword],
]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
我收到以下错误:
Database Exception – yii\db\Exception
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tags.state' in 'where clause' The SQL being executed was: SELECT COUNT(*) FROM
tbl_document
WHERE (tags
.state
LIKE '%1%') OR (tags
.slug
LIKE '%steekwoord%') Error Info: Array ( [0] => 42S22 1 => 1054 [2] => Unknown column 'tags.state' in 'where clause' ) ↵ Caused by: PDOExceptionSQLSTATE[42S22]: Column not found: 1054 Unknown column 'tags.state' in 'where clause'
你应该简单地使用 joinWith()
instead of with()
:
This method allows you to reuse existing relation definitions to perform JOIN queries. [...] Note that because a JOIN query will be performed, you are responsible to disambiguate column names.
例如:
$query = Document::find()
->joinWith('tags tags')
->andFilterWhere([
'or',
['like', 'tags.state', 1],
['like', 'tags.slug', $keyword],
]);