Yii v2 snippet guide

Comparing #204 with #205

Revision #205 was created by rackycz rackycz on Jun 30, 2020, 11:21:00 PM.

FK DropDown

Content

[...]
**Drop down list for foreign-key column**
--

Do you need to specify for example currency using a predefined list, but your view contains only a simple text-input where you must manually enter currency_id from table Currency?

Read how to enhance it.
You only need models with predefined relations - this can be done using GII when relations are defined in MySQL. How to create relations [manually](https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#relational-data).
 
 
---

 

```php
use yii\helpers\ArrayHelper;
[...]
<?= $form->field($model, 'id_currency')->dropDownList($currencies) ?>
```

 
Note: In other views will need models with predefined relations to reach the correct value. Relations can be created using GII (when they are defined in MySQL) or [manually](https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#relational-data).