Search code examples
phpyiiyii-extensions

Need help to create CRUD screen with dropdown (relation) using yii-framework


I need to create CRUD screen using yii-framework. Simple CRUD screen using one table is working perfectly fine. I'm facing problem/issue while using dropdown (linking table).

I have installed giix extension which is suppose to create CRUD with dropdown if FK is specified but I dnt have MySql Engine InnoDB on my hosting provider, so I'm not able to use that extension. I need to do it manually.

I have two tables

main:- id store_id prefix

store:- id name

Now store_id of main is FK to id of store table. And I want to create CRUD for main table.

So that Add Screen should show:-

Store Name:- Dropdown prefix:- Textbox

View screen should use name column of store table instead of showing store_id

Thanks in anticipation.


Solution

  • Generate CRUD using Gii, then read my blog. http://jmmurphy.blogspot.com/2013/05/using-yii-model-relations.html

    Basically you will have something like this for your store_id field after Gii Generation

    <?php echo $form->labelEx($model,'store_id'); ?>
    <?php echo $form->textField($model, 'store_id', array('size'=>60,'maxlength'=>255));?>
    <?php echo $form->error($model,'store_id'); ?>
    

    The textField line is replaced by:

    <?php $list = CHtml::listData(Store::model()->findAll(), 'id', 'name'); ?>
    <?php echo $form->dropDownList($model, 'store_id', $list, array('empty'=>'(Select a Store)')); ?>
    

    You also need to define relations in your Main model so that you can access related tables (even if your database does not support foreign keys) like this:

    public function relations()
    {
        return array(
            'store'=>array(self::BELONGS_TO, 'Store', 'store_id'),
        );
    }
    

    And to complete this relation, you should also add the following relation to your Store model:

    public function relations()
    {
        return array(
            'main'=>array(self::HAS_MANY, 'Main', 'store_id'),
        );
    }
    

    These relations define a One to Many relation between Store and Main where store is the parent, and Main is the Child. To make it a One to One relationship change HAS_MANY to HAS_ONE. The HAS_* goes in the parent model and points to the foreign key attribute in the child table. The BELONGS_TO goes in the child and tells the attribute in the child table that points to the primary key in the parent.

    Now to see the store name in the view action, you need to change 'store_id' in your view.php to an array that looks like:

    array(
       'name' => 'store_id',
       'value' => $model->store->name,
    )
    

    The admin view is slightly different. I am not sure exactly why, but to view the store name instead of the id in the admin view you will need to use an array that looks like:

    array(
       'name' => 'store_id',
       'value' => '$data->store->name',
    )
    

    Note that Gii generates this so that $data is the model instead of $model, and also it does a funky double indirection thing so that you need to put the variable declaration in single quotes.