Skip to content

Instantly share code, notes, and snippets.

@zspine
Created September 25, 2019 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zspine/b7be391d4e7142d6b65e84240dec02ba to your computer and use it in GitHub Desktop.
Save zspine/b7be391d4e7142d6b65e84240dec02ba to your computer and use it in GitHub Desktop.
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
Copy link
Author

zspine commented Sep 25, 2019

image

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