YII2 returns an array with the primary key ID as the key name

Source: Internet
Author: User
Tags yii

branch.php

<?PHPnamespaceApp\models;use Yii;/** * the Model Class for table "branch". * * @property integer $id * @property string $name **/classBranch extends \yii\db\activerecord{/** * @inheritdoc*/     Public Staticfunction TableName () {return 'Branch'; }    /** * @inheritdoc*/     Publicfunction Rules () {return [            [['name'],'string','Max'= $],        ]; }    /** * @inheritdoc*/     Publicfunction Attributelabels () {return [            'ID'='ID',            'name'='Name',        ]; } Public Staticfunction Getkeyvaluepairs () {$sql='SELECT ID, name from'. Self::tablename ().'ORDER by name ASC'; returnYii:: $app->db->createcommand ($sql)Queryall (\pdo::fetch_key_pair); }}

Call

$model->getkeyvaluepairs () obtained as follows:

Display on front end:

<?phpuse yii\helpers\html;use yii\widgets\activeform;use app\models\branch;/*@var $this Yii\web\view*//*@var $model App\models\pos*//*@var $form yii\widgets\activeform*/? ><divclass="Pos-form"> <?php $form = Activeform::begin ();?> <?= $form->field ($model,'Serial')->textinput (['maxlength'=true])?> <?= $form->field ($model,'branch_id')->dropdownlist (Branch::getkeyvaluepairs ())?> <?= $form->field ($model,'is_enable')->checkbox ()?> <divclass="Form-group"> <?= Html::submitbutton ($model->isnewrecord?'Create':'Update', ['class'= $model->isnewrecord?'btn btn-success':'btn Btn-primary'])?> </div> <?php activeform::end ();?></div>

YII2 returns an array with the primary key ID as the key name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.