【问题标题】:Yii - CGridView 1-to-N Display issuesYii - CGridView 1-to-N 显示问题
【发布时间】:2013-05-02 22:36:17
【问题描述】:

我一个月前开始使用 Yii,发现它非常直观,但在小部件方面却有些混乱。

在我正在开发的应用程序中,虽然我有时会使用 Active Record,但我无法使用它的关系,因为我正在使用 MyIsam(并且无法更改)并且它不支持外键.

我的问题是我有一个 CGridView 并想向其中添加自定义数据,但是遇到了问题。

它与正确模型中的 FK一对多 关系,但正如我所说,我不能使用 AR 的魔法。

我有这个,一个应用程序模型和一个配置文件模型。 Profile 模型有一个应用程序FK

我有一个函数,所以当我渲染 CGrid 时,我可以获取每个应用程序的名称,而不是它的 *id_app*。

public function appName($id){
    $app= Yii::app()->db->createCommand()
        ->select('name')
        ->from('tbl_aplications a')
        ->where('a.id=:id_app', array(':id_app'=>$id))
        ->queryRow();

    return $app;
}

在自动生成的模板中,在 Profile Admin.php 视图中,我得到了:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'profile-application-grid',
    'dataProvider'=>$model->search(), //maybe the issue is with this criteria? It is currently 'as-is' after the template generation
    'filter'=>$model,
    'columns'=>array(
        'id',
        'name',
                array(
                    'name'=>'id_app',
                    'header'=>'Aplication',

这是我的问题,我已经尝试过(以及各种变体):

'value' => 'ProfileApplication::model()->appName($data->id_app)',
'value' => 'ProfileApplication::model()->appName(id_app)',
'value' => 'ProfileApplication::model()->appName("id_app")',

结果我得到的只是 null 因为它传递的是实际字符串而不是每一行的值。如果我将直接 ID 传递给函数的查询,它会返回正确的值,例如 ->where('a.id=:id_app', array(':id_app'=>3))

是否需要更改搜索条件? 我发现了类似的问题,但他们都使用 AR,例如 Profile->application 或类似的东西,正如我所说,由于 MyIsam,我无法使用它限制。

感谢这位新人提供任何提示,或链接到有关类似问题的解决方案。

【问题讨论】:

  • MyISAM 唯一的特点是它不会在您生成模型时自动为您检测关系。但是,您可以将关系放在自己身上,您将拥有所需的所有魔力。关于如何使用 value 属性访问相关模型的属性,请参阅下面的答案。

标签: yii cgridview


【解决方案1】:

如 PrplHaz4 所说,使用数据提供者需要的值属性。然后,$data 变量具有魔力,它必须在字符串中使用,因为它是在幕后进行 eval() 的。这是您尝试执行的操作的示例:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'item-grid',
    'dataProvider'=>$model->search(),
    'columns'=>array(
        array(
            'name' => 'id',
            'header' => 'Item ID',

        ),
        array(
            'name' => 'category_search',
            'header' => 'Category',
            'value' => '$data->category->name',
        ),
    ),
)); ?>

获取相关项目类别模型的名称属性。在item模型的relationship()函数中:

return array(
    'category'=>array(self::BELONGS_TO, 'ItemCategory', 'category_id'),
);

并且在item类别模型的relationship()函数中:

return array(
    'items'=>array(self::HAS_MANY, 'Item', 'category_id'),
);

【讨论】:

    【解决方案2】:

    你应该仍然可以在 MyISAM 中使用 ActiveRecord 关系,我相信唯一的区别是 MyISAM,如果你使用模型生成器(gii 或 cmd 行),它不会自动创建关系。相反,您必须自己在 Profile 模型中指定关系。这有效地创建了一个用于 AR 的“软”fk。

    public function relations()
    {
        return array(
            'applications'=>array(self::HAS_MANY, 'Applications', 'id_app'),
        );
    }
    

    但这并不能完全解决您的问题,因为您需要使用数据提供程序来提供应用程序模型和配置文件模型。像这样的:

    $this->widget('zii.widgets.grid.CGridView', array(
      'id'=>'profile-application-grid',
      'dataProvider'=>Profile::model()->with('Applications'),
      'filter'=>$model,
      'columns'=>array(
        'id',
        'name',
        array(
          'name'=>'applications.name',
          'header'=>'Application',
        ),
    

    【讨论】:

    • 哼...我收到ProfileApplication and its behaviors do not have a method or closure named "getData"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 2015-01-23
    相关资源
    最近更新 更多