Answer the question
In order to leave comments, you need to log in
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;
}
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;
}
}
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] => []
)
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question