samedi 27 août 2016

How do you correctly implement Doctrine ACL Organisations in Laravel 5.3?

TL;DR

Does anyone have a solid example of how to implement Organisations using the Doctrine Laravel package as I feel like the docs don't say enough (or maybe i'm just after too much hand holding)


I'm trying to create and attach my user entity to an another entity that implements organisations in Doctrine Laravel but I'm completely stuck at what to do next from reading the documentation...

Currently my set up is like this:

<?php

namespace App\Entities;

use Doctrine\ORM\Mapping as ORM;
use LaravelDoctrine\ACL\Contracts\Organisation;
use LaravelDoctrine\ACL\Mappings as ACL;

/**
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="App\Repositories\ShelterRepository")
 * @ORM\Table(name="shelters")
 */
class Shelter implements Organisation
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $name = null;

    /**
     * @ORM\Column(type="string", unique=true, nullable=false)
     */
    protected $handle;


    //... Getters and setters
}

And my user entity:

<?php

namespace App\Entities;

use Doctrine\ORM\Mapping as ORM;
use LaravelDoctrine\ORM\Auth\Authenticatable;
use LaravelDoctrine\Extensions\Timestamps\Timestamps;
use Illuminate\Auth\Passwords\CanResetPassword;
use LaravelDoctrine\ACL\Mappings as ACL;
use LaravelDoctrine\ACL\Contracts\BelongsToOrganisations;

/**
 * @ORM\Entity(repositoryClass="App\Repositories\UserRepository")
 * @ORM\Table(name="users")
 * @ORM\HasLifecycleCallbacks()
 */
class User implements \Illuminate\Contracts\Auth\Authenticatable, BelongsToOrganisations
{
    use Authenticatable;
    use Timestamps;
    use CanResetPassword;

    /**
     * @ACL\BelongsToOrganisations
     * @var Shelter[]
     */
    protected $organisations;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @return Organisation[]
     */
    public function getOrganisations()
    {
        return $this->organisations;
    }

    //... Other properties and getters/setters

}

When I then run doctrine:schema:update no new columns get added nor any pivots or anything to suggest a relation between Shelter and Userand if I get the first user and try $user->belongsToOrganisation($shelter) I get:

Call to undefined method App\Entities\User::belongsToOrganisation()

I'm not really sure what to do next and the docs kinda stop at this point, am I meant to implement the rest myself? It feels like @ACL\BelongsToOrganisations on the User entity should be doing something but nothing happens so I'm not sure how I should go about adding relations etc.

Anyone with any public code where they've implemented this or any pointers would be amazing, my searches in google/youtube/github have turned up fruitless.

Thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire