Creating dropdown list from related table yii


I have two tables: <em>product</em> and <em>product_type</em> (related to the models resp. Product and Product_type):

product : product_id, product_type_id, name, etc... product_type : product_type_id, name, desc, etc...

Both are related with key "product_type_id".

I have created the crud for both tables using gii crud generator. Now on Product form page I want to display the listing of all product_type name in dropdown field using Yii ActiveRecord. I have added this line in <em>views/product/_form.php</em> script:

<?php $list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'id', 'id'); echo $form->dropDownList($model, 'product_type_id', $list); ?>

But it's showing blank dropdown field :(

How can I do this?


Solved MySelf :)

By just providing product_type_name.

<?php $list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'product_type_id', 'product_type_name'); echo $form->dropDownList($model, 'product_type_id', $list); ?>


In <a href="http://www.yiiframework.com/doc-2.0/index.html" rel="nofollow">Yii2</a> the class <em>CHtml</em> does not exist anymore. Below is a solution, based on the following assumptions:

<ul><li>The <a href="http://www.yiiframework.com/doc-2.0/index.html" rel="nofollow">Yii2</a> framework is used;</li> <li>The table <em>product_type</em> is associated with the model <em>Product_type</em>;</li> <li>The <em>product_type</em> table has a field called "<em>type-name</em>". </li> </ul>

<br />

<?php // get all product types from the corresponding table: $productTypes = Product_type::find()->orderBy('type-name')->asArray()->all(); // create an array of pairs ('id', 'type-name'): $productTypeList = ArrayHelper::map($productTypes, 'id', 'type-name'); // finally create the drop-down list: $form->field($model, 'product_type_id')->dropDownList($productTypeList) ?>


Change your code like bellow

<?php $list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'table_col_name1', 'table_col_name2'); //table_col_name1 is value of option, table_col_name2 is label of option // echo $form->dropDownList($model, 'product_type_id', $list); echo CHtml::dropDownList('name_of_the_dropdown', $model, $list); ?>


  • How to post form by clicking in anchor element
  • How do I render a partial for a view of a nested resource? - Rails 3.1
  • Rails 3 Link_to :remote is not triggering RJS
  • how to remove module name from url in yii [duplicate]
  • Yii2 database session - store additional attributes and user information
  • acts_as_taggable_on Tags added twice
  • Yii: any way to save the images in compressed form?
  • How can i use Django smart select to filter ManyToManyField?
  • Rails CarrierWave versions are not created for some reason
  • ruby/rails: extending or including other modules
  • Client side validation mvc dropdown
  • Action Pack components in Rails
  • Yii2: Finding file and getting path in a directory tree
  • Creating a DropDownList
  • accepts_nested_attributes_for practical form use for in Rails 3
  • ListItem.Attributes.Add not working
  • How to define and use opencv mat of user type
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Typescript - Unable to get 'import' statement to function
  • How to install a .deb file on a jailbroken iphone programmatically?
  • DomPDF {PAGE_NUM} not on first page
  • Using $this when not in object context
  • Retrieving value from sql ExecuteScalar()
  • Javascript simulate pressing enter in input box
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Cassandra Data Model
  • Perl system calls when running as another user using sudo
  • Importing jscolor library in angular 2
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Traverse Array and Display in markup
  • A cron job substitute?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Hits per day in Google Big Query
  • Getting Messege Twice Using IMvxMessenger
  • Checking variable from a different class in C#
  • Python/Django TangoWithDjango Models and Databases