Yii2 ActiveQuery连接关系问题

时间:2016-11-21 11:48:55

标签: php yii2 left-join

ActiveQuery未选择 map.src_fin_position_id

$query = FinancePossibleFinPositions::find()
            ->select (['dst.row_num', 'dst.src_value', 'map.src_fin_position_id'])
            ->from('finance.possible_fin_positions dst')
            ->leftJoin('finance.fin_pos_mapping map','map.map_fin_position_id = dst.possible_fin_positions_id')
            ->leftJoin('finance.possible_fin_positions src','src.possible_fin_positions_id = map.src_fin_position_id AND src.possible_fin_docs_id = :possible_fin_docs_id',[":possible_fin_docs_id"=> 95])
            ->where('dst.possible_fin_docs_id=:possible_fin_docs_id', [":possible_fin_docs_id"=> 88])
            ->orderBy('dst.row_num')
            ->all();

给出

array(2) {
   ["row_num"]=>
    string(4) "1000"
    ["src_value"]=>
    string(18) "My value"
}

虽然

$query = (new \yii\db\Query())
                ->select(["dst.row_num", "dst.src_value", "map.src_fin_position_id"])
                ->from('finance.possible_fin_positions dst')
                ->join('LEFT JOIN','finance.fin_pos_mapping map','map.map_fin_position_id = dst.possible_fin_positions_id')
                ->join('LEFT JOIN','finance.possible_fin_positions src','src.possible_fin_positions_id = map.src_fin_position_id AND src.possible_fin_docs_id = :possible_fin_docs_id',[":possible_fin_docs_id"=> $fin_doc])
                ->where('dst.possible_fin_docs_id=:possible_fin_docs_id', [":possible_fin_docs_id"=> $main_fin_doc])
                ->orderBy('dst.row_num')->all();

给出了我需要使用ActiveQuery

获得的结果
array(3) {
    ["row_num"]=>
    string(4) "1000"
    ["src_value"]=>
    string(18) "My value"
    ["src_fin_position_id"]=>
    int(3426)
  }

我会使用\yii\db\Query()但是当我使用此数组填充表格形式时, $ _ POST 为空。 有谁知道如何解决这个问题?

更新

更简单的例子

form.php的

<div class="col-md-6">
        <?php  
            $form = ActiveForm::begin(['id'=>'post-multiple','action' => ['submit-posts']]);
            echo TabularForm::widget([
            'form' => $form,
            'dataProvider' => $dataProvider,
            'serialColumn' => false,
            'actionColumn' => false,
            'checkboxColumn' => false,
            'attributes' => [
                'post_id' => ['type' => TabularForm::INPUT_STATIC, 'columnOptions'=>['hAlign'=>GridView::ALIGN_CENTER]],
                'post_title' => ['type' => TabularForm::INPUT_STATIC],
                'post_description' => ['type' => TabularForm::INPUT_TEXT, 'options' => ['class' => 'droppable']],
            ],
            'gridSettings' => [
                'floatHeader' => true,
                'panel' => [
                    'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i> Manage Books</h3>',
                    'type' => GridView::TYPE_PRIMARY,
                    'after'=> 
                        Html::a(
                            '<i class="glyphicon glyphicon-plus"></i> Add New', 
                            '#', 
                            ['class'=>'btn btn-success']
                        ) . '&nbsp;' . 
                        Html::a(
                            '<i class="glyphicon glyphicon-remove"></i> Delete', 
                            '#', 
                            ['class'=>'btn btn-danger']
                        ) . '&nbsp;' .
                        Html::submitButton(
                            '<i class="glyphicon glyphicon-floppy-disk"></i> Save', 
                            ['class'=>'btn btn-primary']
                        )
                ]
            ]     
        ]); 
        ActiveForm::end(); ?>
    </div>

控制器

        public function actionTabular()
        {
    //      $query = (new \yii\db\Query())
    //      ->select("post_id, post_title, post_description")
    //      ->from('posts');

            $query = Posts::find()->indexBy('post_id');

            $dataProvider = new ActiveDataProvider([
                    'query' => $query,
                    'pagination' => false,
            ]);

            return $this->render('tabular', [
                'dataProvider'=>$dataProvider,
            ]);
        }
    public function actionSubmitPosts() 
    {
        var_dump(Yii::$app->request->post());exit;
}


$query = Posts::find()->indexBy('post_id');

<input type="text" id="posts-40-post_description" class="droppable form-control ui-droppable" name="Posts[40][post_description]" value="tre">

$ _ POST不为空

$query = (new \yii\db\Query())
->select("post_id, post_title, post_description")
->from('posts');

<input type="text" id="-28-post_description" class="droppable ui-droppable" name="[28][post_description]" value="tre">

$ _ POST为空

两个查询都正确填写表单 不同之处在于输入名称。但我不需要验证这个表格,这很奇怪

[28] [post_description]不会进入$ _POST。

2 个答案:

答案 0 :(得分:0)

来自Yii 2 Guide

  

注意:如果在急切加载关系时调用select(),则必须确保选择了关系声明中引用的列。否则,可能无法正确加载相关型号。例如,

$orders = Order::find()->select(['id', 'amount'])->with('customer')->all();
// $orders[0]->customer is always null.
// To fix the problem, you should do the following:
$orders = Order::find()
    ->select(['id', 'amount', 'customer_id'])
    ->with('customer')->all();

答案 1 :(得分:0)

我想出来了。 对于 yii \ db \ Query

我应该用

<?= Html::beginForm(['save-posts','method'=>'post']); ?>