H
H
hollanditkzn2017-09-13 14:23:18
Yii
hollanditkzn, 2017-09-13 14:23:18

Is it possible to do this in detailView?

Situation, there is a user, User at the moment is the shop. There are employees in the store, you need to make it so that it is recorded which employees are on the shift. I roughly implemented it, but somehow everything turned out cumbersome, and I would like to know if there may be some alternative way to solve this problem.
I must say right away that there are 3 tables: model User - users are stored, at the moment these are stores, Personnel - staff is stored, Shifts - shifts are stored.
All models implemented by User

public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at', 'name'], 'required'],
            [['status', 'created_at', 'updated_at'], 'integer'],
            [['phone'], 'number'],
            [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['name', 'telegram_chat_id', 'telegram_token', 'address'], 'string', 'max' => 50],
            [['username'], 'unique'],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
            [['otdel_id'], 'exist', 'skipOnError' => true, 'targetClass' => Otdel::className(), 'targetAttribute' => ['otdel_id' => 'id']],
        ];
    }
...
/**
     * @return string
     */
    public function getNameSotrud()
    {
        return $this->last_name.' '.$this->name;//Показывает Фамилия Имя сотрудника
    }
/**
     * @return \yii\db\ActiveQuery
     */
    public function getShifts()
    {
        return $this->hasMany(Shifts::className(), ['id_user' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getShiftSotrud()
    {
        return $this->hasMany(Personnel::className(), ['id' => 'id_sotrud'])->via('shifts');
    }

    public function getPersonnelAsString()
    {
//Проверяем кто начал смену сегодня
        $arr = \app\models\Shifts::find()->andWhere(['id_user' => $this->id])
            ->andWhere(['between', 'start', date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
            ->andWhere(['end' => '0000-00-00 00:00:00'])
            ->all();
        $sotrud = ArrayHelper::map($arr, 'id', 'id_sotrud');//Получаем массив из данных
        foreach ($sotrud as $key => $id_sotrud){
//Перебираем массив и выводим Фамилию Имя каждого сотрудника который имеется в массиве
            $user = \app\models\Shifts::findOne(['id_sotrud' => $id_sotrud]);
            return $user->idSotrud->nameSotrud;
        }
    }

Personnel
public function rules()
    {
        return [
            [['last_name', 'name', 'phone'], 'required'],
            [['action'], 'integer'],
            [['last_name', 'name', 'shedule'], 'string', 'max' => 50],
            [['job_duties'], 'string', 'max' => 86],
            [['phone'], 'string', 'max' => 15],
        ];
    }

Shitts
return [
            [['start', 'end'], 'safe'],
            [['id_sotrud', 'id_user'], 'integer'],
            [['id_user'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['id_user' => 'id']],
            [['id_sotrud'], 'exist', 'skipOnError' => true, 'targetClass' => Personnel::className(), 'targetAttribute' => ['id_sotrud' => 'id']],
        ];
/**
     * @return \yii\db\ActiveQuery
     */
    public function getIdUser()
    {
        return $this->hasOne(User::className(), ['id' => 'id_user']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getIdSotrud()
    {
        return $this->hasOne(Personnel::className(), ['id' => 'id_sotrud']);
    }

Well Detailview
use kartik\detail\DetailView;

<?= DetailView::widget([
    'model' => $model,
    'hover' => false,
    'mode'=>DetailView::MODE_VIEW,
    'striped' => false,
    'attributes' => [
        'email:email',
        'address:text',
        'phone',
        'personnelAsString',
    ]
]) ?>

I only get one value

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry Bay, 2017-09-13
@hollanditkzn

You can display whatever you want in the DetailView.

'attributes' => [
        'email:email',
'attribute'=>'email', // можно использовать без модели, например добавить 'label'
'format'=>'raw',
'value'=>function($model) {
echo Html::a('Это емаил'.$model->email, '#');
}

        'address:text',
        'phone',
        'personnelAsString',
    ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question