W
W
WildJust2016-06-21 14:43:12
Yii
WildJust, 2016-06-21 14:43:12

Yii2 ActiveRecord how to replace value with ID on save?

I wanted to use yii \ jui \ AutoComplete, I spent half a day, and I still can’t fasten it so that not the value but id is saved.
Here is the controller

$scope = new Scope;
        $scopeItems = $scope->find()
            ->select(['scope as value', 'scope as label', 'id as id'])
            ->asArray()
            ->all();

        $vacancy = new Vacancy();
        if ($vacancy->load(Yii::$app->request->post()))
        {
            $vacancy->scope_id = $scope->id;
            if ($vacancy->save()){
                $vacancy = new Vacancy();
            }
            else {
            var_dump($vacancy->getErrors());
            }

Here is the form
<div class="vacancy-form">
<?php Pjax::begin(['id' => 'new_vacancy']) ?>
    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($vacancy, 'title')->textInput(['maxlength' => true]) ?>
    <?= $form->field($vacancy, 'text')->textarea(['rows' => 6]) ?>
    <?= $form->field($vacancy, 'date')->widget(DatePicker::className(),['dateFormat' => 'dd-MM-yy', 'language' => 'ru']) ?>
    <?= $form->field($vacancy, 'minprice')->textInput() ?>
    <?= $form->field($vacancy, 'maxprice')->textInput() ?>
    <?= $form->field($vacancy, 'employer_id')->textInput() ?>
    <?= $form->field($scope, 'scope')->widget(AutoComplete::className(), [
    'clientOptions' => [
    'source' => $scopeItems,
    'minLength'=>'2', 
    'options' => ['class' => 'form-control'],
    ]]) ?>
    <?= $form->field($vacancy, 'skill_id')->textInput() ?>
    <?= $form->field($vacancy, 'jobtime_id')->textInput() ?>
    <?= $form->field($vacancy, 'city_id')->textInput() ?>
    <?= $form->field($vacancy, 'mtop')->textInput() ?>
    <div class="form-group">
        <?= Html::submitButton($vacancy->isNewRecord ? 'Create' : 'Update', ['class' => $vacancy->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>

</div>

I'm waiting for help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Евгений Бухарев, 2016-06-21
@evgenybuckharev

Что то какой то бред у вас в контроллере

Никита, 2016-06-21
@bitver

Кажется

->select(['scope as value', 'scope as label', 'id as id']) // Это ваше
->select(['id as value', 'scope as label']) // А так для jui ac

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question