dev-master
9999999-dev https://github.com/porcelanosa/yii2-relatedYii2 Extenstion
MIT
The Requires
- php >=5.4.0
- yiisoft/yii2 *
- kartik-v/yii2-widget-select2 *
by Alex Porcelanosa
yii2 yii yii 2
Yii2 Extenstion
Yii2 extenstions - relate the similar entities , (*1)
composer require porcelanosa/yii2-related
Run migration, (*2)
$ php yii migrate/up --migrationPath=@vendor/porcelanosa/yii2-related/migrations
For model set Behavior, (*3)
model_id_field_name - field name of primary key model_name_field_name - field name of Name model - for example, 'name' or 'title' post_name - POST attribute for send data, (*4)
public function behaviors() { return [ 'relatedBehavior' => [ 'class' => RelatedBehavior::className(), 'model_name' => $this::className(), 'model_id_field_name' => 'id', 'model_name_field_name' => 'name', 'post_name' => 'related_objects', 'whereCondition' => 'deleted!=1' // Conditions for list of related entities ], ...... ] }
In admin view:, (*5)
echo \porcelanosa\yii2related\RelatedWidget::widget( [ 'model' => $model, 'model_name' => $model::className(), 'behaviorName' => 'relatedBehavior', 'title' => 'Похожие модели', 'placeholder' => 'Выберите похожие модели ...', ] );
Get related models, (*6)
<? use yii\helpers\Html; $brand = Brands::findOne(1); foreach($brand->related as $rel) { echo Html::a($rel->name, $rel->slug); echo '<br>'; } ?>
Yii2 Extenstion
MIT
yii2 yii yii 2