Yii2 - 计数和总和不包括ActiveQuery的结果

时间:2017-05-30 16:53:12

标签: mysql yii2

基本上我在mysql中有一个遗留查询,我将在ActiveRecord中实现,

传统方式,正如所料

SELECT 
  a.container_seal, 
  a.size ,
  count(a.size) as bundles,
  sum(a.piece) as pieces
FROM pipe a
WHERE a.outgoing_pipe_id IS NULL
GROUP by a.container_seal, a.size
ORDER by a.container DESC

AR Way

 $modelPipe = Pipe::find()

        ->select([
            "container_seal",
            "size",
            "count(size) as bundles",
            "sum(piece) as pieces"
        ])

        ->where(['outgoing_pipe_id' => NULL])
        ->groupBy(['container_seal', 'size'])
        ->orderBy(['container' => SORT_DESC])
        ->all();

但是,为什么,当我调试它时,

  <pre>
     <?php
        print_r($modelPipe);
     ?>
  </pre>

计数和总和不在。

Array
(
[0] => app\models\hanwa\Pipe Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
            (
                [container_seal] => TEMU6099067/ EMCCSQ6566
                [size] => 15 x 35 x 0.85 x 6000
            )

        [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
            (
                [container_seal] => TEMU6099067/ EMCCSQ6566
                [size] => 15 x 35 x 0.85 x 6000
            )

        [_related:yii\db\BaseActiveRecord:private] => Array
            (
            )

        [_errors:yii\base\Model:private] => 
        [_validators:yii\base\Model:private] => 
        [_scenario:yii\base\Model:private] => default
        [_events:yii\base\Component:private] => Array
            (
            )

        [_behaviors:yii\base\Component:private] => Array
            (
            )

    )

[1] => app\models\hanwa\Pipe Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
            (
                [container_seal] => TEMU6099067/ EMCCSQ6566
                [size] => 35 x 35 x 0.75 x 6000
            )

在'select'的情况下,Yii2是否有另一种使用sum和count的方法?

1 个答案:

答案 0 :(得分:2)

您需要在管道模型中添加用于捆绑和碎片的pubblic var (用于查看查询结果)

class Pipe extends \yii\db\ActiveRecord
{
   public $bundles;
   public $pieces;
 ......