Using Doctrine Associations / Relations in Nextcloud apps

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!

The reason this is not working, is because we do not have Doctrine ORM in place.
Only Doctrine DBAL which is used to execute the actual queries and manage the connection.

1 Like

Ah, okay. That explains, thanks. So what is the preferred way to get model associations in place then when builing upon the app tutorial? All I need is a way to get from the Group to its users in a clean way. Whether or not the relations are lazy-loaded or joined in at query time is not even important at this point, though it would be nice to have control over that.

Is there any documentation that describes the possibilities specifically for Nextcloud environments?

Thanks!

I always use the mail app as example, just look at the Alias classes:
Entity+Mapper: https://github.com/nextcloud/mail/tree/master/lib/Db/
Service: https://github.com/nextcloud/mail/blob/master/lib/Service/AliasesService.php

The Account ID, is basically a link to https://github.com/nextcloud/mail/blob/master/lib/Db/MailAccount.php also with 1-to-n like in your case.
In the service there are methods to get all for the account-id. But there is just no automation in between.

Alright, thank you! To say the least, starting to work with Nextcloud to write applications is quite a bit rocky. It would really help to have some central piece of comprehensive documentation that explains the features of Doctrine that can be used, how associations should be implemented etc. Looking at many different apps, I couldn’t infer any canonical way that follows a best practice or such.

Anyway, your pointers are really helpful, so thanks again :slight_smile:

This conversation was most helpful to me. Did anything change about Doctrine ORM support or are there plans to support it in the future?