【问题标题】:How Can I get all the columns for joined table using dataprovider in Yii?如何在 Yii 中使用 dataprovider 获取连接表的所有列?
【发布时间】:2012-06-21 17:55:58
【问题描述】:

我的控制器

$criteria = new CDbCriteria();
$criteria -> select = 't.*,b.*';
$criteria -> join = 'INNER JOIN tbl_b b on b.b_id = t.id ';
$criteria -> join .= 'INNER JOIN tbl_c c on c.id = b.c_id';
$criteria -> condition = 'c.id = :cid';
$criteria -> params = array(':cid' => 1);
$dataProvider = new CActiveDataProvider('tbl_a',array(
            'criteria' => $criteria
        ));
$this->render('view',array('dataProvider' => $dataProvider));

我的观点

$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'my-grid',
'dataProvider' => $dataProvider,
'columns' => array(
    'name',
    'description',
    array(
        'header' => 'Column from tbl_b',
        'value' => ''
    ),      
        array(
        'class'=>'CButtonColumn',
        'template' => '{view}'
), ),));

我的问题是:如何显示来自tbl_b 的列值。由于在dataprovider 中,我指定了tbl_a,它只从tbl_a 而不是tbl_b 中提取数据,尽管我也从tbl_b 中选择了所有记录。

据我所知,它应该显示为$data -> tbl_b -> col_b。但这会给出错误,因为未定义 tbl_a.tbl_b不知道是什么问题?

Is it something regarding the relation? tbl_atbl_cMANY_MANYtbl_b 相关。即tbl_b 有两列链接tbl_atbl_c 的主ID。

注意:名称和描述来自tbl_a并显示出来。

请推荐!

【问题讨论】:

    标签: activerecord orm yii


    【解决方案1】:

    我通常为这种事情做的是在 ActiveRecord 中创建一个属性。

    class A extends CActiveRecord {
    
        public $bAttribute1
    
    }
    

    当我将表 A 与表 B 连接时,我从 B 重命名了我想用我创建的属性显示的字段(在本例中为 bAttribute

    $dataProvider = new CActiveDataProvider('A', array(
        'criteria' => array(
            'select' => array('t.*', 'b.attribute1 AS bAttribute1'),
            'join' => 'JOIN B AS b ON b.joinId = t.id',
        )
    ));
    

    然后我可以在 GridView 中显示bAttribute1

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $dataProvider,
        'columns' => array(
            'bAttribute1',  
    )));
    

    这应该可以工作。但缺点是,如果要显示连接表中的很多列,则必须创建很多属性。

    更新

    很奇怪,所以我试着从头开始做例子。我创建了两个表ModelAModelB,如下所示。

    CREATE TABLE IF NOT EXISTS `ModelA` (
      `id` int(11) NOT NULL,
      `attribute2` int(11) NOT NULL,
      `attribute3` int(11) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    CREATE TABLE IF NOT EXISTS `ModelB` (
      `id` int(11) NOT NULL,
      `aId` int(11) NOT NULL,
      `attribute3` int(11) NOT NULL,
      `attribute4` int(11) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    

    如您所见,ModelB 中的aIdModelA 的外键。然后我举了一些例子。

    INSERT INTO `ModelA` (`id`, `attribute2`, `attribute3`) VALUES
    (1, 1, 1),
    (2, 2, 2);
    INSERT INTO `ModelB` (`id`, `aId`, `attribute3`, `attribute4`) VALUES
    (1, 1, 10, 100),
    (2, 2, 20, 200),
    (3, 1, 30, 300),
    (4, 1, 40, 400);
    

    因此,ModelA 中的条目 #1 将被 ModelB 中的 3 个条目引用,而条目 #2 将只有 1 个。

    然后我创建了模型代码。

    class ModelA extends CActiveRecord {
        public $bAttribute3;
        public $bAttribute4;
        public static function model($className = __CLASS__) {
                return parent::model($className);
        }
    }
    

    查看模型中的bAttribute3bAttribute4,我将ModelB 表中的attribute3attribute4 分别放在那里。然后在控制器中我创建了 DataProvider

    public function actionIndex(){
        $dataProvider = new CActiveDataProvider('ModelA', array(
            'criteria' => array(
                'select' => array(
                    '`t`.*', 
                    '`b`.`attribute3` AS `bAttribute3`',
                    '`b`.`attribute4` AS `bAttribute4`'
                ),
                'join' => 'JOIN `ModelB` AS `b` ON `b`.`aId` = `t`.`id`',
            )
        ));
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }
    

    在视图中,我做了这个

    $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'my-grid',
        'dataProvider' => $dataProvider,
        'columns' => array(
            'id',
            'attribute2',
            'attribute3',
            'bAttribute3',
            'bAttribute4',
        ),
    ));
    

    所以,这就是我所看到的

    这是你想要的吗?即使对于 CActiveDataProvider,我通常也这样做。我认为您可能会错过代码中的某些内容。

    【讨论】:

    • 我试过这个....我认为这应该有效,但它没有。 dataProvider 不包含本地创建的变量的值。它只有来自 tblA 的记录,而不是 bAttribute1。可能是什么问题?是我又错过了什么还是解决方案不起作用?当我尝试查询时,它工作正常!只是在使用 dataprovider 时不会。
    • 我更新了我的答案。我尝试从头开始制作一个,它可以工作。
    • 哇!!那是一个很好的答案!但正如我在问题中提到的那样,我的情况略有不同。表 A 和表 c 没有直接连接,因此它们没有外来关系。它们通过tableB连接在一起。 TableB 包含 tableA 和 tableC 的主键并将它们关联起来。我知道这听起来有点奇怪,但我必须以这种方式维持对外关系。那么,对于这种情况,有什么解决办法呢?如果他们有直接关系,您的代码是正确的,但在我的情况下,它不是。那么,对这种情况有什么建议吗?
    • 实际上,它几乎相同,你只需要添加另一个属性,比如说cAttribute1,然后在选择中重命名字段c.attribute1 AS cAttribute1,,
    • 你在专栏里试过'value' => '$data->tableC->attribute1这样的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2017-02-25
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多