Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flashcard step 1 (ORM)
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Flashcard
{
/**
* @ORM\Column(name="id", type="string", length=36)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Util\Doctrine\UuidIdGenerator")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
public $question;
/**
* @ORM\Column(type="string", length=255)
*/
public $answer;
/**
* @ORM\ManyToOne(targetEntity="Lesson", inversedBy="flashcards")
*/
public $lesson;
public function __construct($id = null)
{
$this->id = $id;
}
public function getId(): ?string
{
return $this->id;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Lesson
{
/**
* @ORM\Column(name="id", type="string", length=36)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Util\Doctrine\UuidIdGenerator")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
public $name;
/**
* @ORM\OneToMany(targetEntity="Flashcard", mappedBy="lesson", cascade={"persist", "remove"})
*/
public $flashcards;
/**
* @ORM\ManyToOne(targetEntity="Subject", inversedBy="lessons")
*/
public $subject;
public function __construct($id = null)
{
$this->id = $id;
}
public function getId(): ?string
{
return $this->id;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Subject
{
/**
* @ORM\Column(name="id", type="string", length=36)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Util\Doctrine\UuidIdGenerator")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
public $name;
/**
* @ORM\OneToMany(targetEntity="Lesson", mappedBy="subject", cascade={"persist", "remove"})
*/
public $lessons;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
public $user;
public function __construct($id = null)
{
$this->id = $id;
}
public function getId(): ?string
{
return $this->id;
}
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity()
* @ORM\Table(name="`user`")
* @UniqueEntity("email")
*/
class User implements UserInterface
{
/**
* @ORM\Column(type="string", length=36)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="App\Util\Doctrine\UuidIdGenerator")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
public $name;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
public $email;
/**
* @ORM\Column(type="string", length=64)
*/
public $password;
/**
* @ORM\Column(type="json_array")
*/
public $roles;
public $plainPassword;
public function __construct($id = null)
{
$this->id = $id;
}
public function getId(): ?string
{
return $this->id;
}
public function getRoles()
{
return $this->roles;
}
public function getPassword()
{
return $this->password;
}
public function getSalt()
{
return null;
}
public function getUsername()
{
return $this->name;
}
public function eraseCredentials()
{
$this->plainPassword = null;
}
}
<?php
namespace App\Util\Doctrine;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
class UuidIdGenerator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity)
{
if ($entity->getId() !== null) {
return $entity->getId();
}
return \Ramsey\Uuid\Uuid::uuid4();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.