Newbie's question - read from specific data table

Hi there, I’m new to Yii and i need some help real bad :(

Here’s what i got, i have 2 table tbl_breeddata and tbl_user in Mysql database, connected fine. I create user model and CRUD. I did not create breeddata model and CRUD, b/c its just contains one column of breed names.

What i want to do is read from tbl_breeddata within User.php(user model file) and return the data. So i can get it in views/user/_form.php and generate a dropdown list.

Is that doable and plz help

First create Model for tbl_breeddata. Then in User controller file, you can query the breed data and pass it to crate view. So if your model for breed table is "Breed", the code will look similar to:




public function actionCreate()

{

....

$BreedData = Breed::model()->findAll();

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

}



You can then use $BreedData array to create your dropdown list.

Thank you for replay .D3!!! I found another solution as well, I redesigned the table and create relation between those two table, and use gii generate the new User model to update the public_function_relation. therefore i can query the breed table in User model file.

And about ur method, i understand the $BreedData = Breed::model()->findAll(); is to put breed table data into $BreedData, would you mind explain the

$this->render(‘create’,array(‘model’=>$model, ‘BreedData’=>$BreedData)); and like you said this is all in User controller file, how to access/call the ‘create view’ in User model file or I can simply call it in /view/user/_form.php

Thank you again for your kindly helps :)

Parameters you passed to $this->render(‘create’,array(‘model’=>$model, ‘BreedData’=>$BreedData)); will make the data available in view files. In the above case, two variables $model and $BreedData are available in protected/views/users/create.php

Since the create.php file partially renders _form.php file so it will be available in _form.php file as well.

You should not call any view file from Model class. Views must be called from controller file. If you check the create.php view file, you will see that it call to _form.php file


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>

but renders it partially NOT FULLY.