Returning Empty Array For Relational Active Records

I have below written code in my controller, and the data is being used for the purpose of active dropdown. but when i run the code, drop down doesn’t show any entry. i believe this function is returning an empty array. can you please point out where i am going wrong?

tblCategories is defined in the relations function of (brand)model class for many many relationship between category and brand.

Thanks in advance :)

in controller:

public function getCategoryOptions()

    {


          $model = new Brand;


          $categoryArray = CHtml::listData($model->tblCategories,'brand_code','index');


          return $categoryArray;


    }

in form:

<?php echo $form->dropDownList($model,'category_code', $this->getCategoryOptions()); ?>

in model:

    'tblCategories' => array(self::MANY_MANY, 'Category', 'tbl_category_brand_assign(brand_code, category_code)'),

Hi rajkumar,

This will not work, because $model is a newly created instance and has no relational objects yet.




public function getCategoryOptions($modelId)

{

    $model = Brand::model()->findByPk($modelId);

    $categoryArray = CHtml::listData($model->tblCategories,'brand_code','index');

    return $categoryArray;

}



This may work for updating scenario.

But it will not return all the categories. It just returns the categories that a specific instance of Brand already has.

And also it doesn’t work for creating scenario, because you have no $modelId yet.

So, I think what you want to do is something like this:

in Category.php




public static function getCategoryOptions()

{

    $models = Category::model()->findAll();

    $categoryArray = CHtml::listData($models, 'brand_code', 'index');

    return $categoryArray;

}



in form:




<?php echo $form->dropDownList($model,'category_code', Category::getCategoryOptions()); ?>