5.8.4: How to save a referenced User object ID in my database with Doctrine?

Permalink
I need to store a user object ID in my database:
use Concrete\Core\User\User as User;
...
/**
* Many Posts have one Author
* @ORM\ManyToOne("Concrete\Core\Entity\User\User")
* @ORM\JoinColumn(name="author",referencedColumnName="uID",onDelete="SET NULL")
*/
protected $author;
public function getAuthor()
{
    return $this->author;
}
public function setAuthor($author)
{
    $this->author = $author;

But when I save it, it throws the error:

Doctrine \ ORM \ ORMInvalidArgumentException
Expected value of type "Concrete\Core\Entity\User\User" for association field "Blog\Post\Post#$author", got "Concrete\Core\User\User" instead.

What's wrong here? A Concrete\Core\User\User can't be used in Doctrine.

Thank you.

linuxoid
 
mesuva replied on at Permalink Reply
mesuva
It's really just the case that those are two different types of object, one is a doctrine entity and one is more of a concrete5 specific object with different functionality. Doctrine only works with entities.

I haven't really tested this, but I'd suggest changing what you pass the setAuthor function:
$userInfo = $user->getUserInfoObject();
$userEntity = $userInfo->getEntityObject()
$post->setAuthor($userEntity);

That should then be an entity object that doctrine is expecting to work with.
linuxoid replied on at Permalink Reply
linuxoid
Looks like that did the trick. Thank you very much!

Here's the code:
$app = Application::getFacadeApplication();
$ui = $app->make(UserInfoRepository::class)->getByID($data['author']);
$ue = $ui->getEntityObject();
$post->setAuthor($ue);
mesuva replied on at Permalink Reply
mesuva
When you are saving the $post object, are you definitely getting a uID stored against your database record?
linuxoid replied on at Permalink Reply
linuxoid
Yes. It all works fine. In phpMyAdmin it follows to the user in the Users table. And I can get all the user info in the controller:
if ($post && $post->getAuthor()) {
    $this->set('user_id', $post->getAuthor()->getUserID());
    $this->set('user_name', $post->getAuthor()->getUserName());
}