Last active
September 14, 2018 08:36
-
-
Save piotrbrzezina/484121812136ea7a209aa297481a0c55 to your computer and use it in GitHub Desktop.
Flashcard step 1 (ORM)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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