porcelanosa / yii2-related
Yii2 Extenstion
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=5.4.0
- kartik-v/yii2-widget-select2: *
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-12-29 09:26:37 UTC
README
yii2-related
Yii2 extenstions - relate the similar entities
Install
composer require porcelanosa/yii2-related
Run migration
$ php yii migrate/up --migrationPath=@vendor/porcelanosa/yii2-related/migrations
Set Behavior
For model set Behavior
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
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 ], ...... ] }
Use widget
In admin view:
echo \porcelanosa\yii2related\RelatedWidget::widget( [ 'model' => $model, 'model_name' => $model::className(), 'behaviorName' => 'relatedBehavior', 'title' => 'Похожие модели', 'placeholder' => 'Выберите похожие модели ...', ] );
Usage
Get related models
<? use yii\helpers\Html; $brand = Brands::findOne(1); foreach($brand->related as $rel) { echo Html::a($rel->name, $rel->slug); echo '<br>'; } ?>