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:
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.
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;
Viewing 15 lines of 20 lines. View entire code block.
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.
Looks like that did the trick. Thank you very much!
Here's the code:
Here's the code:
$app = Application::getFacadeApplication(); $ui = $app->make(UserInfoRepository::class)->getByID($data['author']); $ue = $ui->getEntityObject(); $post->setAuthor($ue);
When you are saving the $post object, are you definitely getting a uID stored against your database record?
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()); }
I haven't really tested this, but I'd suggest changing what you pass the setAuthor function:
That should then be an entity object that doctrine is expecting to work with.