Загружаю картинку, через связь populateRelation запись заносится в таблицу с картинками,
заполняется id и имя картинки, остальное не заполнено, заполняется только через
$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
{
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] => []
)
поля user_id и sort не являются атрибутами... хотя поле filename заполняется