Yii Framework Forum: Yii framework: how to get id from DB, when choosing related record from DB - Yii Framework Forum

Jump to content

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

Yii framework: how to get id from DB, when choosing related record from DB Yii framework: how to get id from DB, when choosing related record fro

#1 User is offline   jhd235 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-March 18

Posted 15 March 2018 - 05:17 AM

could you please advise: I have Yii2. I have text edit box with Autocomplete::widget on the web page. Autocomplete populates from DB. When I start to type and choose Autocomplete item (which is First Name from DB, in my case) I need to get associated id from DB. How could this get implemented?
<?php
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Rating;
use app\models\Teacher;
use yii\jui\AutoComplete;
use yii\web\JsExpression;



/* @var $this yii\web\View */
/* @var $model app\models\Rating */
/* @var $form ActiveForm */

    $data = Teacher::find()
    ->select(['lname as value', 'lname as  label','id as id'])
    ->asArray()
    ->all();

    //print_r($data[0].id);//exit;


?>

<div class="rating-input">
ФИО
    <?php 

    echo AutoComplete::widget([
    'name' => 'Teacher',
    'id' =>'ddd',
    'clientOptions' => [
    'source' => $data,
    'autoFill'=>true,
    'minLength'=>'1',
     ],
     ]);    


0

#2 User is offline   safitanet 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 04-August 11

Posted 25 March 2018 - 04:10 PM

Hi,




You can create a public property, teacherId. In the form is hidden y it will have id of Teacher…




echo AutoComplete::widget([

'name' => ‘Teacher’,

'id' => 'ddd',

'clientOptions' => [

'source' => $data,

'autoFill'=>true,

'minLength'=>’1’,

'select' => new JsExpression("function( event, ui ) {

$('#model-teacherId’).val(ui.item.id);

}")],

]);

?>




<?= Html::activeHiddenInput($model, ‘teacherId’)?>


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