yii2从旧id创建历史合同

时间:2017-05-22 03:28:42

标签: gridview activerecord yii2 yii2-advanced-app cactivedataprovider

我试图在旧ID后显示合约历史记录。如果员工创建新合同,那么他可以在查看历史合同中查看最后一份合同。 我试过了,但只能显示最后一份合同。如果员工有最后一份合同,则只有一份合同出现。

这是我的合同控制员

    public function actionView($id)
{
    $model = $this ->findModel($id);
    $employee = $model->employee;
    $params = [":old" => $employee->old_id];
    $result = Yii::$app->db->createCommand('select * from employee where id=:old or old_id=:old', $params)->queryOne();

    $contractQuery = Contract::find()->where(['id_contract' => $result]);

    $dataProvider = new ActiveDataProvider([
        'query' => $contractQuery ,
        'pagination' => [
            'pageSize' => 20,
        ],
    ]);

    return $this->render('view', [
        'model' => $model,
        'teNumDataProvider' => $teNumDataProvider,
    ]);
}

contract.php模型

public function getEmployee()
{
return $this->hasOne(Employee::className(), ['id' => 'id_employee']);
}

view.php

<div class="contract-index">
<h4>History Contract</h4>

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        'id_employee',
        'startdate:date',
        'enddate:date',
        'employee_status',
        'status',

        [
            'class' => 'yii\grid\ActionColumn',
            'visible' => FALSE,
        ],
    ],
]); ?>

1 个答案:

答案 0 :(得分:0)

您应该从$ result获取列名称(例如:$ result-&gt; contract_id);

$result = Yii::$app->db->createCommand('select * from employee 
                       where id=:old or old_id=:old', $params)->queryOne();

$contractQuery = Contract::find()->where(['id_contract' => $result->contract_id]);

以及如果您构建一个名为$ dataProvider的var的操作,您应该使用它来渲染相同的varname

return $this->render('view', [
    'model' => $model,
    'dataProvider' => $dataProvider,
]);

并在网格视图中以这种方式持续使用$ dataProvider包含您在查询操作中构建的值

 <?= GridView::widget([
   'dataProvider' => $dataProvider,
   ...