有 3 个表文章,作者和大学
我正在为表格文章制作网格。每篇文章都属于一个作者,每个作者都属于一所大学。我需要展示 University.name |Author.name |品。*
在管理员视图中
$gridWidget=$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'dados-cd-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'author_name'=>array(
'name' => 'author_name',
'value' => '$data->author->name'
),
'university_name'=>array(
'name' => 'university_name',
'value' => '$data->university->author->name'
),
...
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
在文章模型中,我有
public $author_name;
public $university_name;
public function rules()
{
...
return array(
array('author.name, university.name, ... ', 'safe', 'on'=>'searc
h'),
...
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array( 'author','author.university' );
$criteria->compare('author.name',$this->author_name,true);
$criteria->compare('university.name',$this->university_name,true);
...
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'author_name'=>array(
'asc'=>'author.name',
'desc'=>'author.name DESC',
),
'university_name'=>array(
'asc'=>'university.name',
'desc'=>'university.name DESC',
),
'*',
),
),
));
}
网格工作正常。但是,当我尝试按author_name搜索或排序时,我收到一条错误消息。我会尝试从葡萄牙语翻译错误消息,但我不知道英语是否是这样的:"未定义的表:7 错误:缺少表作者的条目"
有趣的是,大学的搜索和秩序运作良好。而且我看不出它们之间的区别。
有人可以帮助我吗?
编辑
我已经检查了数据库中的列名是否真的是"名称"在错误消息中,它显示查询的一部分是"(WHERE author.name LIKE ycp0)"。我认为这是我的Prioblem的关键,但我不知道具体如何。
编辑
错误信息错误 500:
CDb 异常
CDbCommand falhou ao executar o comando SQL: SQLSTATE[42P01]: Undefined table:7 ERRO: faltando entrada para tabela "autor" na cláusula FROM LINE 1: ..."autor"."universidade_id"="大学"。id") 其中(自动...
^.执行的 SQL 语句是 SELECT COUNT(DISTINCT 'T')。id') 从 "ARTIGO" 'T' 左外连接 'autor' 'autor' 上 ('t')。阿蒂戈'。autor_id'='autor'."id') 左外加入 "Universidade" 'universidade' ON ('AUTR')。universidade_id'='大学'。id') WHERE (autor.nome LIKE: ycp0)
看起来有点奇怪:
return array(
array('author.name, university.name, ... ', 'safe', 'on'=>'search'),
我猜这些属性名称的规则应该在它们自己的类中。我认为您无法为模型类中的另一个模型定义验证规则。我认为这会让 Yii 认为有一个名为"author.name"的数据库列。
也:
你有这个代码:
'$data->university->author->nome'
.不应该是->name
吗?
请用您看到的母语添加错误。
找到了!!
我使用''表'。列"'和作品。我不明白为什么它没有" "对于大学来说是必需的,但对于作者来说是必需的。然而,我也把"放在大学里并且仍在工作,所以我相信使用"是正确的方法。
感谢所有试图帮助我的人。天堂属于你们。<3
//排序
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'author_name'=>array(
'asc'=>'"author"."name"',
'desc'=>'"author"."name" DESC',
),
'university_name'=>array(
'asc'=>'university.name',
'desc'=>'university.name DESC',
),
'*',
),
),
//搜索
$criteria->compare('"author"."name"',$this->author_name,true);
$criteria->compare('university.name',$this->university_name,true);