Hi!
I’m fiddling around with my first Nextcloud application, and I’m struggling with setting up a set of very basic database entities, or more precisely, the relations between them. I’ve followed the ‘notes’ tutorial in the Nextcloud documentation, but that doesn’t cover my topic, and I’m confused.
In short, I have two data models: Group and GroupUser. A Group “has many” GroupUsers, and each GroupUser points back to one Group.
From what I’ve read about Doctrine, I believed that all I have to do is add an annotation to the variable in my Entity that describes that link. Hence, my code in lib/Db/Group.php currently looks like this:
<?php
namespace OCA\MyApp\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
use Doctrine\Common\Collections\ArrayCollection;
class Group extends Entity implements JsonSerializable {
/**
* @OneToMany(targetEntity="GroupUser", mappedBy="group", orphanRemoval=true)
*/
protected $users;
public function __construct() {
$this->addType('users', 'ArrayCollection');
$this->users = new ArrayCollection();
}
public function jsonSerialize(): array {
return [
'users' => $this->users,
];
}
}
But this doesn’t work. When the Controller calls into the Mapper via the Service, to return the JSON object, the ‘users’ member comes up empty.
Also, I wonder why no other Nextcloud app that I looked at uses that Doctrine annotations, they rather all roll their own thing to get relations going, which I want to avoid if possible.
I’ve used Rails for many years, so I am very well aware of the concepts of ORM systems.
What am I doing wrong?
Thanks!