yii2 从旧 id 创建历史合约

yii2 create history contract from old id

我正在尝试显示旧 ID 之后的合同历史记录。如果员工创建新合同,那么他可以在视图历史合同中看到最后一份合同。 我试过了,但只能显示最后一份合同。如果员工有 2 个最后的合同,视图中只会出现一个。

这是我的合约控制器

    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,
        ],
    ],
]); ?>

您应该从 $result 中获取列名(例如:$result->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 的变量,那么您应该在渲染相同的变量名时使用该操作

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

最后你以这种方式在你的 gridview 中 $dataProvider 包含你在查询中构建的值

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