/**
  * @inheritdoc
  */
 public function validateAttribute($model, $attribute)
 {
     $label = $model->getAttributeLabel($attribute);
     if (!is_array($model->{$attribute})) {
         $model->addError($attribute, Yii::t('many-to-many', '{attribute} must be an array.', ['attribute' => $label]));
         return;
     }
     /* @var $behavior null|ManyToManyBehavior */
     $behavior = null;
     foreach ($model->behaviors as $key => $attachedBehavior) {
         if ($attachedBehavior::className() == ManyToManyBehavior::className()) {
             $behavior = $attachedBehavior;
             break;
         }
     }
     if (!$behavior) {
         throw new InvalidConfigException("Behavior not detected.");
     }
     /* @var $relation null|\arogachev\ManyToMany\components\ManyToManyRelation */
     $relation = null;
     foreach ($behavior->getManyToManyRelations() as $attachedRelation) {
         if ($attachedRelation->editableAttribute == $attribute) {
             $relation = $attachedRelation;
             break;
         }
     }
     if (!$relation) {
         throw new InvalidConfigException("Relation not detected.");
     }
     $primaryKeys = $model->{$attribute};
     if (!$primaryKeys) {
         return;
     }
     /* @var $relatedModel \yii\db\ActiveRecord */
     $relatedModel = $relation->relatedModel;
     $relatedModelPk = $relatedModel::primaryKey()[0];
     $relatedModelsCount = $relatedModel::find()->where([$relatedModelPk => $primaryKeys])->count();
     if (count($primaryKeys) != $relatedModelsCount) {
         $error = 'There are nonexistent elements in {attribute} list.';
         $model->addError($attribute, Yii::t('many-to-many', $error, ['attribute' => $label]));
     }
 }
Esempio n. 2
0
 public function behaviors()
 {
     return [TimestampBehavior::className(), BlameableBehavior::className(), DateTimeBehavior::className(), CacheBehavior::className(), ['class' => AdminLogBehavior::className(), 'titleAttribute' => 'title', 'icon' => 'fa-bars bg-olive'], ['class' => TranslateableBehavior::className(), 'translationAttributes' => ['title', 'slug', 'description', 'content', 'image_description', 'redirect', 'meta_title', 'meta_keywords', 'meta_description'], 'translationLanguageAttribute' => 'locale'], ['class' => ImageUploadBehavior::className(), 'attribute' => 'image', 'defaultImage' => '/images/no_image.png', 'thumbs' => ['small' => ['width' => 150, 'height' => 100], 'big' => ['width' => 450, 'height' => 150]], 'filePath' => '@frontend/web/uploads/news/[[id_path]]/[[pk]].[[extension]]', 'fileUrl' => '/uploads/news/[[id_path]]/[[pk]].[[extension]]', 'thumbPath' => '@frontend/web/uploads/news/[[id_path]]/[[profile]]_[[pk]].[[extension]]', 'thumbUrl' => '/uploads/news/[[id_path]]/[[profile]]_[[pk]].[[extension]]'], ['class' => ManyToManyBehavior::className(), 'relations' => [['editableAttribute' => 'categories', 'table' => '{{%news_news_category}}', 'ownAttribute' => 'news_id', 'relatedModel' => NewsCategory::className(), 'relatedAttribute' => 'category_id', 'fillingRoute' => ['news/news/create', 'news/news/update']]]], ['class' => TaggableBehavior::className()], ['class' => GalleryBehavior::className(), 'galleryClass' => NewsGallery::className()]];
 }
 /**
  * Use alternative config - relation via relation
  * @param Test|\arogachev\ManyToMany\behaviors\ManyToManyBehavior $test
  * @param array $additionalUsersRelationConfig
  * @return Test|\arogachev\ManyToMany\behaviors\ManyToManyBehavior
  */
 protected function useRelationViaRelation($test, $additionalUsersRelationConfig = [])
 {
     $test->attachBehavior('manyToMany', ['class' => ManyToManyBehavior::className(), 'relations' => [array_merge(['name' => 'usersViaRelation', 'editableAttribute' => 'editableUsers'], $additionalUsersRelationConfig)]]);
     $test->customInit();
     if (!$test->isNewRecord) {
         $test->afterFind();
     }
     return $test;
 }
Esempio n. 4
0
 /**
  * @inheritdoc
  */
 public function behaviors()
 {
     return ['manyToMany' => ['class' => ManyToManyBehavior::className(), 'relations' => [array_merge(['editableAttribute' => 'editableUsers', 'table' => 'tests_users', 'ownAttribute' => 'test_id', 'relatedModel' => User::className(), 'relatedAttribute' => 'user_id'], self::$additionalUsersRelationConfig)]]];
 }