I
I
Igor2020-01-01 22:44:53
PHP
Igor, 2020-01-01 22:44:53

Why is ActiveRecord not populating?

I upload a picture, through the populateRelation connection, the record is entered into the table with pictures,
the id and the name of the picture are filled in, the rest is not filled, it is filled only through$image->setAttribute('user_id',$this->user_id);

public function create($userId, ProfileNewPostForm $form): MyBlogPost
    {
        $user = $this->users->get($userId);
        $post = MyBlogPost::create($user->id, $form->title, $form->text);
        $post->addPhoto($form->imageFiles);

        $post->save();
        return $post;
    }


post { model
public function setImages($images)
    {
        $this->populateRelation('images', $images);

    }

    public function addPhoto($files): void
    {
        $images = [];

        foreach ($files as $num => $file) {
            $image = MyBlogPhoto::create($file);
            $image->setUser($this->user_id);
            $image->setSort($num);
            $images[] = $image;
        }
       // \yii\helpers\VarDumper::dump($images  , 8, true);die;
        $this->setImages($images);

    }

    public function getImages(): ActiveQuery
    {
        return $this->hasMany(MyBlogPhoto::class, ['post_id' => 'id']);
    }

    public function afterSave($insert, $changedAttributes): void
  $relatedRecords = $this->getRelatedRecords();

        if (isset($relatedRecords['images'])) {
            foreach ($relatedRecords['images'] as $image) {
                $this->link('images', $image);
            }
        }
        parent::afterSave($insert, $changedAttributes);
    }

<?php
  
class MyBlogPhoto extends ActiveRecord
{
    const status_img = 1;

    public $status;
    public $user_id;
    public $sort;

    public static function create( $imageFiles): self
    {
        $photo = new static();
        $photo->status = self::status_img;
        $photo->filename = $imageFiles->name;
        return $photo;
    }

    public function setSort($sort): void
    {
        $this->sort = $sort;
    }
    public function setUser($user_id): void
    {
        $this->user_id = $user_id;
    }
 
 


}


after filling it turns out like this
2 => src\dbmodel\MyBlogPhoto#3
    (
        [status] => 1
        [user_id] => 1
        [sort] => 2
        [yii\db\BaseActiveRecord:_attributes] => [
            'filename' => 'czP0AxMbpcU.jpg'
        ]
        [yii\db\BaseActiveRecord:_oldAttributes] => null
        [yii\db\BaseActiveRecord:_related] => []
        [yii\db\BaseActiveRecord:_relationsDependencies] => []
        [yii\base\Model:_errors] => null
        [yii\base\Model:_validators] => null
        [yii\base\Model:_scenario] => 'default'
        [yii\base\Component:_events] => []
        [yii\base\Component:_eventWildcards] => []
        [yii\base\Component:_behaviors] => []
    )


the user_id and sort fields are not attributes... even though the filename field is populated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bashcode, 2020-01-03
@igordrum

Reference excerpts:
populateRelation($relationName, $relatedModelOrArray) - adds a related model to the parent.
Note:
This method does not check if a relationship between these models is declared (getter), and also does not set the desired values ​​in the attributes. (Ie, you need to set the links yourself).
link($relationName, relatedModel, $extraColumns = []) - unlike populateRelation, this method, in addition to adding a related model, also binds models by setting the necessary indexes. Immediately it saves ONLY the associated model. $extraColumns will be stored in the pivot table if the link is through it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question