Dropdown with values from the database

Hi,

a few weeks ago i’ve found an article about a dropdown with values from the database that sounds really good.

But after an extended forum and cookbook search i cannot find it again.

  1. Could someone tell me the best way to get the values (id, GUI-value) for a form dropdown from the database.

  2. And how to translate them? (en_us => de_de)

THX

BJ

the link you are looking for is this one

to translate , and I’m not sure if this is the easiest solution, you should extend CHtml::listData to fit your needs

for translations, you could have the keys stored on the database, for example:




echo Yii::t('category',$model->description); 



There is a cool wiki that explains how you can do it per model basis: http://www.yiiframework.com/wiki/66/i18n-for-your-model-in-just-1-line

Have a look to get ideas…

Thanks, but this is not the link i’m looking for. It was a really simple solution with only one dropdown.

Have found posts and tipps like the following but i’m not sure if they are best practice.

a.) Dropdown list from database/model

b.) How to deal with foreign keys properly?

c.) activeDropDownList form database

d.) DropDownList from database

e.) activeDropDownList

Some ideas?

It’s actually pretty simple




echo $form->dropDownList(

	$model,

	'fieldName',

	CHtml::listData(MyModel::model()->findAll('user_id>1'),'user_id','user_name')

);



I agree with Gustavo, it’s actually pretty simple. To put your mind at peace, check this and scroll down the bottom: http://www.yiiframework.com/wiki/48/by-example-chtml/

Jaymard

Great! ;D

That’s really simple. That’s the solution i’m looking for.

THANKS

i can´t get the dropDownList, what am i doing wrong?

My model Dir.php




<?php


/**

 * This is the model class for table "tbl_dir".

 *

 * The followings are the available columns in table 'tbl_dir':

 * @property integer $id

 * @property string $d_codigo

 * @property string $d_asenta

 * @property string $d_mnpio

 * @property string $d_estado

 * @property string $c_estado

 * @property string $c_mnpio

 */

class Dir extends CActiveRecord

{

    /**

     * Returns the static model of the specified AR class.

     * @return Dir the static model class

     */

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }


    /**

     * @return string the associated database table name

     */

    public function tableName()

    {

        return 'tbl_dir';

    }


    /**

     * @return array validation rules for model attributes.

     */

    public function rules()

    {

        // NOTE: you should only define rules for those attributes that

        // will receive user inputs.

        return array(

            array('d_codigo, d_asenta, d_mnpio, d_estado, c_estado, c_mnpio', 'required'),

            array('d_codigo, d_asenta, d_mnpio, d_estado', 'length', 'max'=>50),

            array('c_estado, c_mnpio', 'length', 'max'=>5),

            // The following rule is used by search().

            // Please remove those attributes that should not be searched.

            array('id, d_codigo, d_asenta, d_mnpio, d_estado, c_estado, c_mnpio', 'safe', 'on'=>'search'),

        );

    }


    /**

     * @return array relational rules.

     */

    public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

        );

    }


    /**

     * @return array customized attribute labels (name=>label)

     */

    public function attributeLabels()

    {

        return array(

            'id' => 'ID',

            'd_codigo' => 'D Codigo',

            'd_asenta' => 'D Asenta',

            'd_mnpio' => 'D Mnpio',

            'd_estado' => 'D Estado',

            'c_estado' => 'C Estado',

            'c_mnpio' => 'C Mnpio',

        );

    }


    /**

     * Retrieves a list of models based on the current search/filter conditions.

     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

     */

    public function search()

    {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria=new CDbCriteria;


        $criteria->compare('id',$this->id);

        $criteria->compare('d_codigo',$this->d_codigo,true);

        $criteria->compare('d_asenta',$this->d_asenta,true);

        $criteria->compare('d_mnpio',$this->d_mnpio,true);

        $criteria->compare('d_estado',$this->d_estado,true);

        $criteria->compare('c_estado',$this->c_estado,true);

        $criteria->compare('c_mnpio',$this->c_mnpio,true);


        return new CActiveDataProvider(get_class($this), array(

            'criteria'=>$criteria,

        ));

    }

} 






My model empresa.php




<?php


/**

 * This is the model class for table "tbl_empresa".

 *

 * The followings are the available columns in table 'tbl_empresa':

 * @property integer $id

 * @property string $empresa

 * @property string $rfc

 * @property string $domicilio

 * @property string $colonia

 * @property integer $cp

 * @property string $mun_del

 * @property string $estado

 * @property string $fecha_ingreso

 */

class Empresa extends SolinforActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Empresa the static model class

	 */


	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'tbl_empresa';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('empresa, rfc', 'unique'),

			array('empresa, rfc, domicilio, colonia, cp, mun_del, estado', 'required'),

			array('cp', 'numerical', 'integerOnly'=>true),

			array('empresa, domicilio, colonia, mun_del, estado', 'length', 'max'=>100),

			array('rfc', 'length', 'max'=>13),

			array('rfc', 'length', 'min'=>11),

			array('cp', 'length', 'min'=>5),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, empresa, rfc, domicilio, colonia, cp, mun_del, estado', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'empresa' => 'Nombre de la Empresa',

			'rfc' => 'Rfc',

			'domicilio' => 'Calle y Número',

			'colonia' => 'Colonia',

			'cp' => 'Codigo Postal',

			'mun_del' => 'Delegación o Municipio',

			'estado' => 'Estado',

			'fecha_ingreso' => 'Fecha Ingreso',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('empresa',$this->empresa,true);

		$criteria->compare('rfc',$this->rfc,true);

		$criteria->compare('domicilio',$this->domicilio,true);

		$criteria->compare('colonia',$this->colonia,true);

		$criteria->compare('cp',$this->cp);

		$criteria->compare('mun_del',$this->mun_del,true);

		$criteria->compare('estado',$this->estado,true);

		$criteria->compare('fecha_ingreso',$this->fecha_ingreso,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

}



My _form.php




	<div class="row">

		<?php 

                      echo $form->labelEx($model,'colonia'); 



i change this line

[b]




                         echo $form->dropDownList(

                         $model,

                         'colonia',

                         CHtml::listData(Dir::model ()->findAll('id>1'),'id','d_asenta')

                         ); 



[/b]




                      echo $form->error($model,'colonia');

                ?>

        </div>



Any help will be appreciate

Do you get an error message or are there problems with the display of the dropdown list?

Any more details?

@blackjack

may be this will work

echo $form->dropDownList(

                     &#036;model,


                     'colonia',


                     CHtml::listData(Dir::model ()-&gt;findAll('id&gt;1'),'id','id','d_asenta','d_asenta')


                     );