【问题标题】:Yii fail to retrieve max column valueYii 无法检索最大列值
【发布时间】:2011-08-19 12:06:32
【问题描述】:

我有两种模式,一种是Auction,一种是Bid

拍卖有很多出价。它们由 Bid

中的外键auction_id 关联

现在,我想找出每次拍卖的出价的最高值。

$dataProvider = new CActiveDataProvider('Auction', array('criteria' => array(
                    'with' => array(
                        'bids' => array(
                            'alias'=>'b',
                            'group' => 'auction_id',
                            'select' => 'max(b.price) as maxprice'
                        )
                   )
             )
        )
);

我在 Auction 的模型类中定义了一个 maxprice 属性。

但是,如果我尝试检索 maxprice 属性,它会返回 NULL

更具体地说,我将 $dataprovider 渲染到视图页面,它无法获取 maxprice 属性。

PS:

我在mysql中执行查询,查询结果是正确的。

那么,Yii 代码肯定有问题

SQL 代码:

SELECT `t`.`id` , max(b.price) as maxprice

FROM `auction` `t` 

LEFT OUTER JOIN `bid` `b` ON (`b`.`auction_id`=`t`.`id`)  GROUP BY auction_id

【问题讨论】:

    标签: select yii max relational


    【解决方案1】:

    把你想要的值放在关系之前,像这样:

    $dataProvider = new CActiveDataProvider('Auction', array('criteria' => array(
        'select' => 't.*, max(b.price) as maxprice',
        'with' => array(
            'bids' => array(
                'alias'=>'b',
                'group' => 'auction_id',
                'together'=>true,
            )
    

    您可以根据需要将“t.*”替换为特定的字段名称。

    或者您可以简单地在您的拍卖模型上使用 select、join 和 group 属性并完全跳过关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      相关资源
      最近更新 更多