1:n relation after crud creation

Hi,

I have a newbie question. I have two tables, models and CRUD both generated and working ok.

I want to replace the default text area for the field that points to the second table, and put a dropdown.

I went to my controllers second table (the 1 in the relation) and i have created a public method like this:




public function getOptions(){

            $something = $this->findAll();

            $res = array();

            foreach ($something as $p)

                $res[$p->id] = $p->name;


            return (count($res) > 0) ? $res : FALSE;

        }



In the view file of the form of my first table (the N part of the relation) i have that:




<?php echo $form->dropDownList($model, 'second_table_id', second_table::model()->getOptions()); ?>



… and it works fine.

Im just want to know if thats the way to go to handle this 1:n relations.

Thanks

hm, what do you mean by “the way to do this”…if it works fine for you than you are all set :P

Have a look at CHtml::listData(array $models, string $valueField, string $textField, string $groupField=’’);

Link: http://www.yiiframework.com/doc/api/CHtml#listData-detail

Hi, thanks for your reply. You have to forgive my english…

What i wanted to say is, if i have a 1:n relations, y have to go to the n table model and create a public function like i made? there is a better aproach to this?

This is my second day with yii and im a "little confused" in how i have to do somethings…

Thanks again

Keep up the good work. You could’ve also done this:


public function getOptions(){

            $something = $this->findAll();

            $res = CHtml::listData($something,'id','name');


            return $res;

        }

Yes, having a public function for your drop down is good enough

Perfect, thank you very much!