Hi there I have a problem. I'm tring resolve for 2 days but nothing
namespace Core\Model\Entity;
use Core\Model\Traits\Id as IdTrait;
use Core\Model\Traits\Nombre as NameTrait;
use Core\Model\Traits\Email as EmailTrait;
/**
* Class User
*
* @package Core\Model\Entity
*/
class User extends Base {
//columns
const COL_ID = 'id';
const COL_NAME = 'name';
const COL_EMAIL = 'email';
const COL_AVATAR_ID = 'avatarId';
//relationships
const REL_AVATAR = 'avatar';
//traits all private properties
use IdTrait;
use NameTrait;
use EmailTrait;
/**
*
* @var Multimedia|null
*/
private $avatarId = null;
public function initialize()
{
//tabla
$this->setSource("users");
//avatar
$this->hasOne(
self::COL_AVATAR_ID,
Multimedia::class,
Multimedia::COL_ID,
[
'alias' => self::REL_AVATAR,
]);
}
//others setters and getters
public function setAvatar(Multimedia $multimedia = null) {
$this->avatar = $multimedia;
return $this;
}
public function getAvatar() {
return $this->avatar;
}
}
namespace Core\Model\Entity;
use Core\Model\Traits\Id;
/**
*
* @package Core\Model\Entity
*/
class Multimedia extends Base
{
const COL_ID = 'id';
const COL_FILENAME = 'filename';
//traits all private columns
use Id;
/**
* Nombre y ruta del archivo
* @var string|null
*/
private $filename = null;
}
$user = new User();
$user->setName('John');
$multimedia = new Mutlimedia();
$multimedia->setFilename('image.jpg');
$user->setAvatar($multimedia);
$user->save();
$user->getAvatar(); //yeah works!!! I get the Multimedia
//next request
$user = User::find();//find the previous created user
$user->getAvatar(); //null :( doesn't work. avatarId = Null in the db
The user and multimedia was created but avatarId is null in the database and in the next request the user has not an avatar Someone has any idea that does not work?