Custom attribute yii2 activerecord

I am trying to use a custom attribute in a model class that extends db\activerecord.

I tried to declare public $categories = [], and then assigned values ​​to it directly through $model->categories = [1,2,3]or using the setter method in my model class public function setCategories($ids) {..., and then assigning through again $model->categories = [1,2,3].

I also tried updating the attribute with $model->setAttribute('categories', [1,2,3]).

In all cases $model->categoriesit is not filled.

My ultimate goal is to categorize the model and then update the db relation / tables with afterSave()andbeforeSave()

Can this be done, or should I extend the model class from db\model? If so, what functionality will I lose?

Edit Perhaps I was mistaken in my problem.

I have a form where the user can select categories for a specific model (for example, "Product"). All categories already exist, and products are assigned to categories through the product_category connection table ('product_id', 'category_id') with a one-to-many relationship (one product has many categories).

Now, in the controller processing the view, I get a list of category identifiers, and I want to assign them to an attribute so that I can handle them, i.e. delete or add (via link()) entries in the product_category table for a specific product.

+4
source share
2 answers

, ? ?

(--) activerecord , , ( , ActiveRecord, ModelCategory model_id a category_id:

public function getProductCategories() 
{ 
   return $this->hasMany(ProductCategory::className(), ['product_id' => 'id']);
}

public function getCategories()
{
    return $this->hasMany(Category::className(), ['id' => 'category_id'])
        ->via('productCategories');
}

( viaTable() via() , .)

, :

$product->categories  

( , __get(), ).

. Yii :

$category = new Category();
// Assign attributes
$category->save();

$product = new Product();
// Assign attributes
$product->save();

$product->link('categories', $category);

link -function. , , .

:

public function actionAssignCategories($product, $categories) 
{
   $product = Product::findOne($product);

   $existingCategories = \yii\base\ArrayHelper::getColumn($product->categories, 'category_id');
   $removeCategories = array_diff($existingCategories, $categories); 
   $addCategories = array_diff($categories, $existingCategories);             

   foreach ($addCategories as $category) { 
      $category = Category::findOne($category);
      $product->link('categories', $category);
   }

   foreach ($removeCategories as $category) { 
      $category = Category::findOne($category);
      $product->unlink('categories', $category);
   }
}

, , .

+2
class ClassName extends \yii\db\ActiveRecord
{

   public $addition;   //what attribute you want

    /* your code */

   public function fields()
   {
        $fields = parent::fields();
        $fields[] = 'addition';      //the value must be the same as the new attribute

        /* your code */

        $this->addition = 'done'
        return $fields
   }
}
+2

All Articles