Skip to content

Instantly share code, notes, and snippets.

@zspine
Created Sep 25, 2019
Embed
What would you like to do?
Symfony User Management
<?php
namespace App\Entity;
use App\Model\Account\MembershipInterface;
use App\Model\Resource\TimestampableTrait;
use App\Model\Resource\ToggleableTrait;
use App\Model\Resource\UuidTrait;
use App\Model\User\UserInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
* @ORM\Table(name="app_accounts")
*/
class Account
{
use UuidTrait, TimestampableTrait, ToggleableTrait;
/**
* @var string
* @ORM\Column(type="string", length=196, nullable=false)
*/
private $description;
/**
* @var UserInterface|null
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
private $owner;
/**
* @var Collection|MembershipInterface[]
* @ORM\OneToMany(targetEntity="App\Entity\Membership", mappedBy="user", cascade={"persist", "remove"})
*/
private $memberships;
}
<?php
namespace App\Entity;
use App\Model\Account\AccountInterface;
use App\Model\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\UuidInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\MembershipRepository")
* @ORM\Table(name="app_memberships", uniqueConstraints={
* @ORM\UniqueConstraint(name="account_user_rel", columns={"account_id", "user_id"})
* })
* @UniqueEntity(
* fields={"account", "user"},
* message="Membership already exist",
* errorPath="user"
* )
*/
class Membership
{
/**
* @var UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
*/
protected $id;
/**
* @var AccountInterface|null
* @ORM\ManyToOne(targetEntity="App\Entity\Account", inversedBy="users")
* @ORM\JoinColumn(nullable=false)
*/
private $account;
/**
* @var UserInterface
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="accounts")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @var array
* @ORM\Column(type="json", nullable=false)
*/
private $roles;
}
<?php
namespace App\Entity;
use App\Model\Account\AccountInterface;
use App\Model\Resource\ArraySerializableTrait;
use App\Model\Resource\TimestampableTrait;
use App\Model\Resource\ToggleableTrait;
use App\Model\Resource\UuidTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @UniqueEntity(fields={"email"}, message="Email address already exist", groups={"create"})
*
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table(name="app_users")
*/
class User
{
use UuidTrait, TimestampableTrait, ToggleableTrait, ArraySerializableTrait;
/**
* @var string
* @ORM\Column(type="string", nullable=false)
*/
protected $password;
/**
* @var string|null
*/
protected $plainPassword;
/**
* @var array
* @ORM\Column(type="json", nullable=false)
*/
protected $roles = [];
/**
* @var string|null
* @ORM\Column(type="string", nullable=false, unique=true)
*/
protected $email;
/**
* @var ArrayCollection|AccountInterface[]
* @ORM\OneToMany(targetEntity="App\Entity\Membership", mappedBy="account", cascade={"persist", "remove"})
*/
private $accounts;
}
@zspine

This comment has been minimized.

Copy link
Owner Author

@zspine zspine commented Sep 25, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment