DropDownList from database

Hi there, I am trying to do a small webapp with yii, I created 2 table, users and area. A user is part of an area of the company (production, design, etc). I want to add in the CRUD forms a dropdown with the areas, and create update of modify the idarea in the user table. How can I do this, what changes should I do in view-controller-model files. My files area this right now:

I also don't know If the relations are ok.

models\Usuario.php

<?php





class Usuario extends CActiveRecord


{


	/**


	 * Returns the static model of the specified AR class.


	 * This method is required by all child classes of CActiveRecord.


	 * @return CActiveRecord 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 'Usuario';


	}





	/**


	 * @return array validation rules for model attributes.


	 */


	public function rules()


	{


		return array(


			array('nombre','length','max'=>200),


			array('apellido','length','max'=>200),


			array('loginname','length','max'=>50),


			array('password','length','max'=>50),


			array('email','length','max'=>50),


			array('email', 'email'),


			array('loginname, password, email, idarea', 'required'),


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


		);


	}








	// -----------------------------------------------------------


	// Uncomment the following methods to override them if needed


	


	public function relations()


	{


		return array(


			'idarea'=>array(self::HAS_ONE, 'Area', 'idarea'),			


		);


	}


	/*


	public function attributeLabels()


	{


		return array(


			'authorID'=>'Author',


		);


	}





	public function protectedAttributes()


	{


		return array();


	}


	*/


}

models\area.php

<?php





class area extends CActiveRecord


{


	/**


	 * Returns the static model of the specified AR class.


	 * This method is required by all child classes of CActiveRecord.


	 * @return CActiveRecord 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 'area';


	}





	/**


	 * @return array validation rules for model attributes.


	 */


	public function rules()


	{


		return array(


			array('nombre','length','max'=>100),


			array('nombre', 'required'),


		);


	}








	// -----------------------------------------------------------


	// Uncomment the following methods to override them if needed


	/*


	public function relations()


	{


		return array(


			'author'=>array(self::BELONGS_TO, 'User', 'author_id'),


			'comments'=>array(self::HAS_MANY, 'Comment', 'post_id', 'with'=>'author', 'order'=>'create_time DESC'),


			'tags'=>array(self::MANY_MANY, 'Tag', 'post_tag(post_id, tag_id)', 'order'=>'name'),


		);


	}





	public function attributeLabels()


	{


		return array(


			'authorID'=>'Author',


		);


	}





	public function protectedAttributes()


	{


		return array();


	}


	*/


}

All the other files are the ones created with the CRUD command.

Thanks a lot in advance.

The relationship for 'idarea' (you should choose a different name since you already have a 'idarea' column) in Usuario should be BELONGS_TO.

You can use the following view code to generate the dropdown:



<?php echo CHtml::activeDropDownList($model, 'idarea', CHtml::listData($areas, 'id', 'name')); ?>


where $areas is obtained by:



$areas=area::model()->findAll();


Thanks a lot qiang. So I should change idarea in the dbase usuario table or changing in the model php file will be ok?

the first line you give me must be in the view file, and the second in the model, isn't it? in area or in usuario model file?

See you!

You should change the model class file regarding the relation.

Yes, the first line of code is for view. The second line can be in either controller or view.

I made somechanges but it says:

Fatal error: Call to a member function hasErrors() on a non-object in C:xampphtdocscanaldoceyiiyiiframeworkwebhelpersCHtml.php on line 904

I made this changes:

in views/usuario/create.php I add this:

<div class="simple">


<?php $areas=area::model()->findAll(); ?>


<?php echo CHtml::activeDropDownList($model, 'idarea', CHtml::listData($areas, 'idarea', 'nombre')); ?>


</div>

and in usuario.php I modify the relation lines:



class Usuario extends CActiveRecord


{


//.....


        public function relations()


	{


		return array(


			'idarea'=>array(self::BELONGS_TO, 'Area', 'idarea'),			


		);


	}


}


What am I doing wrong?

Do you have $model variable defined?

You should also choose a different relation name in your relations(). The name 'idarea' is already used as the column name. You probably should name it as 'area'.

I don't have a $model variable defined, you are right, how should I define it? You mention that I can also do this is the controller, but I don't know in which method of the controller should I do this. Well, how can I do this?

Also about the selection for the variable name in the relation, how should it be?

Option 1:

public function relations()

{

      return array(

        'area_id'=>array(self::BELONGS_TO, 'Area', 'idarea'),       

      );

}

Option 2:

public function relations()

{

      return array(

        'idarea'=>array(self::BELONGS_TO, 'Area', 'area_id'),       

      );

}

No one, and I am totally wrong. Thanks you again and thanks for your patience!.

I am spanish speaker so If I can help in any way with Yii I am here to serve.

You may pass the 'model' variable to the view when you call render() method in the controller action, like the following:



$model=new MyModelClass;


.......


$this->render('viewName', array('model'=>$model));


I think 'area' should be a good name because you may want to access the related object like $model->area, which is very meaningful.