Hello,
I use kartik select2 plugin, to select multiple member to add to a “logbook” entry. My question is not about the plugin, because it works for other entries in my form, but for the member list, i have a problem, i can’t show the members already saved. I’m pretty sure i do something wrong in the controller update.
I think the problem is under $myteams in the form and the $model->profile_ids
as in form as in controller.
If any of you understand what i do wrong, it would really really be helpfull !
Thank you!
My controller update function
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) ) {
$model->date_begin = strtotime($model->date_begin);
$model->date_end = strtotime($model->date_end);
$model->time_begin = strtotime($model->time_begin);
$model->time_end = strtotime($model->time_end);
$model->created_at = strtotime(date("Y-m-d H:i:s"));
$model->modified_at = strtotime(date("Y-m-d H:i:s"));
if($model->save()) {
if($model->type == 'tache')
{
foreach ($model->profile_ids as $profile) {
$profile = Profiles::find()->where(['profiles.id'=>$profile])->joinWith('user')->One();
$user = $profile->user;
$task = $id;
TaskNotification::create(TaskNotification::KEY_SHOW_TASK, ['profile' => $user,'task'=>$task])->send();
/*
$test = TaskNotification::create(TaskNotification::KEY_SHOW_TASK, ['profile' => $user,'task'=>$task]);
echo '<pre>';
var_dump($test);
die();
*/
}
}
Yii::$app->session->setFlash('success', "Log created successfully.");
return $this->redirect(['view', 'id' => $model->id]);
}else {
Yii::$app->session->setFlash('error', "Log not saved.");
}
}elseif (!\Yii::$app->request->isPost) {
$model->load(Yii::$app->request->get());
$model->date_begin = date('Y-m-d',$model->date_begin);
$model->date_end = date('Y-m-d',$model->date_end);
$model->time_begin = date('H:i:s',$model->time_begin);
$model->time_end = date('H:i:s',$model->time_end);
$model->profile_ids = ArrayHelper::map($model->profiles, 'id', 'firstname');
$model->team_ids = ArrayHelper::map($model->teams, 'team_name', 'team_name');
$model->project_ids = ArrayHelper::map($model->projects, 'name', 'name');
$model->tag_ids = ArrayHelper::map($model->tags, 'tag_name', 'tag_name');
$model->contact_ids = ArrayHelper::map($model->contacts, 'fullname', 'fullname');
}
return $this->render('update', [
'model' => $model,
]);
}
My form
<?php
use common\models\Profiles;
use common\models\Projects;
use common\models\Tags;
use common\models\Teams;
use common\models\Contacts;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\date\DatePicker;
use kartik\time\TimePicker;
use kartik\select2\Select2;
use dosamigos\multiselect\MultiSelect;
$session = Yii::$app->session;
$profileid = $session->get('profileid');
$profile = Profiles::findOne($profileid);
$i = 0;
foreach ($profile->teams as $team) {
# code...
$myteams = ArrayHelper::map(Profiles::find()->joinWith('teams')->where(['teams.id'=>$team->id])->all(), 'id', 'firstname');
$teamid = $team->id;
if($i == 0){
$projectdata = ArrayHelper::map(Projects::find()->joinWith('teams')->where(['teams.id'=>$team->id])->all(), 'name', 'name');
$teamcheck = 1;
$teamprofileid = $team->id;
$i++;
}
else{
$projectdata = ArrayHelper::map(Projects::find()->joinWith('teams')->all(), 'name', 'name');
$teamcheck = 0;
break;
}
}
var_dump($model->profile_ids);
var_dump($myteams);
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->errorSummary($model); ?>
<div class="logbooks-form">
<div class="row">
<div class="col-sm-6 col-xs-12">
<?php
if($teamcheck == 0) {
echo $form->field($model, 'team_ids')->widget(Select2::className(), [
'model' => $model,
'attribute' => 'team_ids',
'data' => ArrayHelper::map(Teams::find()->all(), 'team_name', 'team_name'),
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
],
])->label('Team/s');
}
if($teamcheck == 1) {
echo $form->field($model, 'team_ids')->hiddenInput(['value'=>$teamprofileid])->label(false);
}
?>
</div>
<div class="col-sm-6 col-xs-12">
<?php
echo $form->field($model, 'profile_ids')->widget(Select2::className(), [
'model' => $model,
'attribute' => 'profile_ids',
'value' => $model->profile_ids,
'data'=> $myteams,
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
],
])->label('Attach to Member/s');
/*
$myteamid = $profile->teams->id;
*/
?>
<?= $form->field($model, 'profile_ids')->widget(MultiSelect::className(),[
'data' => $myteams,
]) ?>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-xs-12">
<?php
echo $form->field($model, 'project_ids')->widget(Select2::className(), [
'model' => $model,
'attribute' => 'project_ids',
'data' => $projectdata,
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
],
])->label('Project/s');
?>
</div>
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, 'type')->dropDownList([
'assemblée'=>Yii::t('app', 'assemblée'),
'correspondance externe'=> Yii::t('app', 'correspondance externe'),
'correspondance interne'=>Yii::t('app', 'correspondance interne'),
'courrier'=>Yii::t('app', 'courrier'),
'email'=>Yii::t('app', 'email'),
'entretien'=>Yii::t('app', 'entretien'),
'manifestation'=>Yii::t('app', 'manifestation'),
'tache'=>Yii::t('app', 'tâche'),
'telephone'=>Yii::t('app', 'téléphone'),
'autre'=>Yii::t('app', 'autre'),
]) ?>
</div>
<div class="col-sm-4 col-xs-12">
<?php
echo $form->field($model, 'tag_ids')->widget(Select2::className(), [
'model' => $model,
'attribute' => 'tag_ids',
'data' => ArrayHelper::map(Tags::find()->all(), 'tag_name', 'tag_name'),
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
],
])->label('Tag/s');
?>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, 'date_begin')->widget(
DatePicker::classname(),[
'name' => 'date_begin',
'value' => date('d-M-Y'),
'options' => ['placeholder' => 'Select'],
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'todayHighlight' => true
],
]
) ?>
</div>
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, 'date_end')->widget(
DatePicker::classname(),[
'name' => 'date_end',
'value' => date('d-M-Y'),
'options' => ['placeholder' => 'Select'],
'pluginOptions' => [
'format' => 'yyyy-mm-dd',
'todayHighlight' => true
],
]
) ?>
</div>
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, 'state')->dropDownList([
'todo'=>Yii::t('app', 'à faire'),
'en cours'=>Yii::t('app', 'en cours'),
'refusé'=> Yii::t('app', 'refusé'),
'sans réponse'=>Yii::t('app', 'sans réponse'),
'terminé'=>Yii::t('app', 'terminé'),
]) ?>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, 'time_begin')->widget(TimePicker::classname(), [
'pluginOptions' => [
'showMeridian' => false,
'showSeconds' => false,
'minuteStep' => 1,
]
]); ?>
</div>
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, 'time_end')->widget(TimePicker::classname(), [
'pluginOptions' => [
'showMeridian' => false,
'showSeconds' => false,
'minuteStep' => 1,
]
]); ?>
</div>
<div class="col-sm-4 col-xs-12">
<?= $form->field($model, "allday")->checkbox(['value' => 1]); ?>
</div>
</div>
<div class="row">
<div class="col-sm-8 col-xs-12">
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4 col-xs-12">
<?php
echo $form->field($model, 'contact_ids')->widget(Select2::className(), [
'model' => $model,
'attribute' => 'contact_ids',
'data' => ArrayHelper::map(Contacts::find()->all(), 'fullname', 'fullname'),
'options' => [
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
],
])->label('Contact/s');
?>
</div>
<div class="col-xs-12">
<?= $form->field($model, 'description')->widget(\yii\redactor\widgets\Redactor::className())//->textarea(['rows' => 6]) ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>