Yii Framework Forum: How to set active data provider in gridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to set active data provider in gridview Create History Contact with gridview

#1 User is offline   Ryan Sergius 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 05-April 17

Posted 22 May 2017 - 02:34 AM

I've a problem to create a history contract.
I've table employee

id_number <pk>
name
dob
old_id

and contract
id <pk>
id_employee <fk>
join date
end date

if employee create new contract so he has new id_number. And then i want to display a history contract with gridview based of 'old_id' at table employee.

this my contract controller
public function actionView($id)
        {
        $employee = NULL;
        $employees = Employee::find()->indexBy('id_number')->orderBy('id_number')->all();
        
        $query = Contract::find();
        if ($id !== null && isset($employees[$id])){
            $employee = $employees[$id];
            $query->where(['id_employee' => $this->getEmployeeIds($employees, $id)]);
        }


$dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 6,
            ],
        ]);
        
        return $this->render('view', [
            'model' => $this ->findModel($id),
            'dataProvider' => $dataProvider,
        ]);
    }

private function getEmployeeIds($employees, $employeeId, &$employeeIds = [])
    {
        foreach ($employees as $employee) {
            if ($employee->id_number == $employeeId) {
                $employeeIds[] = $employee->id_number;
            }
            elseif ($employee->old_id == $employeeId) {
                $this->getEmployeeIds($employees, $employee->id_number, $employeeIds);
            }
        }
        return $employeeIds;
    }

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users