Last active
February 8, 2018 02:50
-
-
Save weaverryan/0ad2c1de469e33bd2104bd23741749ae to your computer and use it in GitHub Desktop.
Symfony Demo with make:entity --regenerate --overwrite
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 | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace App\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @ORM\Entity | |
* @ORM\Table(name="symfony_demo_comment") | |
* | |
* Defines the properties of the Comment entity to represent the blog comments. | |
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class | |
* | |
* Tip: if you have an existing database, you can generate these entity class automatically. | |
* See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html | |
* | |
* @author Ryan Weaver <[email protected]> | |
* @author Javier Eguiluz <[email protected]> | |
*/ | |
class Comment | |
{ | |
/** | |
* @var int | |
* | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @var Post | |
* | |
* @ORM\ManyToOne(targetEntity="Post", inversedBy="comments") | |
* @ORM\JoinColumn(nullable=false) | |
*/ | |
private $post; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="text") | |
* @Assert\NotBlank(message="comment.blank") | |
* @Assert\Length( | |
* min=5, | |
* minMessage="comment.too_short", | |
* max=10000, | |
* maxMessage="comment.too_long" | |
* ) | |
*/ | |
private $content; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(type="datetime") | |
* @Assert\DateTime | |
*/ | |
private $publishedAt; | |
/** | |
* @var User | |
* | |
* @ORM\ManyToOne(targetEntity="App\Entity\User") | |
* @ORM\JoinColumn(nullable=false) | |
*/ | |
private $author; | |
public function __construct() | |
{ | |
$this->publishedAt = new \DateTime(); | |
} | |
/** | |
* @Assert\IsTrue(message="comment.is_spam") | |
*/ | |
public function isLegitComment(): bool | |
{ | |
$containsInvalidCharacters = false !== mb_strpos($this->content, '@'); | |
return !$containsInvalidCharacters; | |
} | |
public function getId(): ?int | |
{ | |
return $this->id; | |
} | |
public function getContent(): ?string | |
{ | |
return $this->content; | |
} | |
public function setContent(string $content) | |
{ | |
$this->content = $content; | |
} | |
public function getPublishedAt(): ?\DateTimeInterface | |
{ | |
return $this->publishedAt; | |
} | |
public function setPublishedAt(\DateTimeInterface $publishedAt) | |
{ | |
$this->publishedAt = $publishedAt; | |
} | |
public function getPost(): ?Post | |
{ | |
return $this->post; | |
} | |
public function setPost(?Post $post) | |
{ | |
$this->post = $post; | |
} | |
public function getAuthor(): ?User | |
{ | |
return $this->author; | |
} | |
public function setAuthor(?User $author) | |
{ | |
$this->author = $author; | |
} | |
} |
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 | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace App\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @ORM\Entity(repositoryClass="App\Repository\PostRepository") | |
* @ORM\Table(name="symfony_demo_post") | |
* | |
* Defines the properties of the Post entity to represent the blog posts. | |
* | |
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class | |
* | |
* Tip: if you have an existing database, you can generate these entity class automatically. | |
* See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html | |
* | |
* @author Ryan Weaver <[email protected]> | |
* @author Javier Eguiluz <[email protected]> | |
* @author Yonel Ceruto <[email protected]> | |
*/ | |
class Post | |
{ | |
/** | |
* Use constants to define configuration options that rarely change instead | |
* of specifying them under parameters section in config/services.yaml file. | |
* | |
* See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options | |
*/ | |
public const NUM_ITEMS = 10; | |
/** | |
* @var int | |
* | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
* @Assert\NotBlank | |
*/ | |
private $title; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
*/ | |
private $slug; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
* @Assert\NotBlank(message="post.blank_summary") | |
*/ | |
private $summary; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="text") | |
* @Assert\NotBlank(message="post.blank_content") | |
* @Assert\Length(min=10, minMessage="post.too_short_content") | |
*/ | |
private $content; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(type="datetime") | |
* @Assert\DateTime | |
*/ | |
private $publishedAt; | |
/** | |
* @var User | |
* | |
* @ORM\ManyToOne(targetEntity="App\Entity\User") | |
* @ORM\JoinColumn(nullable=false) | |
*/ | |
private $author; | |
/** | |
* @var Comment[]|ArrayCollection | |
* | |
* @ORM\OneToMany( | |
* targetEntity="Comment", | |
* mappedBy="post", | |
* orphanRemoval=true, | |
* cascade={"persist"} | |
* ) | |
* @ORM\OrderBy({"publishedAt": "DESC"}) | |
*/ | |
private $comments; | |
/** | |
* @var Tag[]|ArrayCollection | |
* | |
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"}) | |
* @ORM\JoinTable(name="symfony_demo_post_tag") | |
* @ORM\OrderBy({"name": "ASC"}) | |
* @Assert\Count(max="4", maxMessage="post.too_many_tags") | |
*/ | |
private $tags; | |
public function __construct() | |
{ | |
$this->publishedAt = new \DateTime(); | |
$this->comments = new ArrayCollection(); | |
$this->tags = new ArrayCollection(); | |
} | |
public function getId(): ?int | |
{ | |
return $this->id; | |
} | |
public function getTitle(): ?string | |
{ | |
return $this->title; | |
} | |
public function setTitle(string $title) | |
{ | |
$this->title = $title; | |
} | |
public function getSlug(): ?string | |
{ | |
return $this->slug; | |
} | |
public function setSlug(string $slug) | |
{ | |
$this->slug = $slug; | |
} | |
public function getSummary(): ?string | |
{ | |
return $this->summary; | |
} | |
public function setSummary(string $summary) | |
{ | |
$this->summary = $summary; | |
} | |
public function getContent(): ?string | |
{ | |
return $this->content; | |
} | |
public function setContent(string $content) | |
{ | |
$this->content = $content; | |
} | |
public function getPublishedAt(): ?\DateTimeInterface | |
{ | |
return $this->publishedAt; | |
} | |
public function setPublishedAt(\DateTimeInterface $publishedAt) | |
{ | |
$this->publishedAt = $publishedAt; | |
} | |
public function getAuthor(): ?User | |
{ | |
return $this->author; | |
} | |
public function setAuthor(?User $author) | |
{ | |
$this->author = $author; | |
} | |
/** | |
* @return Collection|Comment[] | |
*/ | |
public function getComments(): Collection | |
{ | |
return $this->comments; | |
} | |
public function addComment(Comment $comment) | |
{ | |
if ($this->comments->contains($comment)) { | |
return; | |
} | |
$this->comments[] = $comment; | |
$comment->setPost($this); | |
} | |
public function removeComment(Comment $comment) | |
{ | |
if (!$this->comments->contains($comment)) { | |
return; | |
} | |
$this->comments->removeElement($comment); | |
// set the owning side to null (unless already changed) | |
if ($comment->getPost() === $this) { | |
$comment->setPost(null); | |
} | |
} | |
/** | |
* @return Collection|Tag[] | |
*/ | |
public function getTags(): Collection | |
{ | |
return $this->tags; | |
} | |
public function addTag(Tag $tag) | |
{ | |
if ($this->tags->contains($tag)) { | |
return; | |
} | |
$this->tags[] = $tag; | |
} | |
public function removeTag(Tag $tag) | |
{ | |
if (!$this->tags->contains($tag)) { | |
return; | |
} | |
$this->tags->removeElement($tag); | |
} | |
} |
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 | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace App\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity() | |
* @ORM\Table(name="symfony_demo_tag") | |
* | |
* Defines the properties of the Tag entity to represent the post tags. | |
* | |
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class | |
* | |
* @author Yonel Ceruto <[email protected]> | |
*/ | |
class Tag implements \JsonSerializable | |
{ | |
/** | |
* @var int | |
* | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string", unique=true) | |
*/ | |
private $name; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function jsonSerialize(): string | |
{ | |
// This entity implements JsonSerializable (http://php.net/manual/en/class.jsonserializable.php) | |
// so this method is used to customize its JSON representation when json_encode() | |
// is called, for example in tags|json_encode (app/Resources/views/form/fields.html.twig) | |
return $this->name; | |
} | |
public function __toString(): string | |
{ | |
return $this->name; | |
} | |
public function getId(): ?int | |
{ | |
return $this->id; | |
} | |
public function getName(): ?string | |
{ | |
return $this->name; | |
} | |
public function setName(string $name) | |
{ | |
$this->name = $name; | |
} | |
} |
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 | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace App\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
/** | |
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") | |
* @ORM\Table(name="symfony_demo_user") | |
* | |
* Defines the properties of the User entity to represent the application users. | |
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class | |
* | |
* Tip: if you have an existing database, you can generate these entity class automatically. | |
* See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html | |
* | |
* @author Ryan Weaver <[email protected]> | |
* @author Javier Eguiluz <[email protected]> | |
*/ | |
class User implements UserInterface, \Serializable | |
{ | |
/** | |
* @var int | |
* | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
*/ | |
private $fullName; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string", unique=true) | |
*/ | |
private $username; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string", unique=true) | |
*/ | |
private $email; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
*/ | |
private $password; | |
/** | |
* @var array | |
* | |
* @ORM\Column(type="json") | |
*/ | |
private $roles = []; | |
/** | |
* Returns the salt that was originally used to encode the password. | |
* | |
* {@inheritdoc} | |
*/ | |
public function getSalt(): ?string | |
{ | |
// See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html | |
// we're using bcrypt in security.yml to encode the password, so | |
// the salt value is built-in and you don't have to generate one | |
return null; | |
} | |
/** | |
* Removes sensitive data from the user. | |
* | |
* {@inheritdoc} | |
*/ | |
public function eraseCredentials(): void | |
{ | |
// if you had a plainPassword property, you'd nullify it here | |
// $this->plainPassword = null; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function serialize(): string | |
{ | |
// add $this->salt too if you don't use Bcrypt or Argon2i | |
return serialize([$this->id, $this->username, $this->password]); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function unserialize($serialized): void | |
{ | |
// add $this->salt too if you don't use Bcrypt or Argon2i | |
[$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]); | |
} | |
public function getId(): ?int | |
{ | |
return $this->id; | |
} | |
public function getFullName(): ?string | |
{ | |
return $this->fullName; | |
} | |
public function setFullName(string $fullName) | |
{ | |
$this->fullName = $fullName; | |
} | |
public function getUsername(): ?string | |
{ | |
return $this->username; | |
} | |
public function setUsername(string $username) | |
{ | |
$this->username = $username; | |
} | |
public function getEmail(): ?string | |
{ | |
return $this->email; | |
} | |
public function setEmail(string $email) | |
{ | |
$this->email = $email; | |
} | |
public function getPassword(): ?string | |
{ | |
return $this->password; | |
} | |
public function setPassword(string $password) | |
{ | |
$this->password = $password; | |
} | |
public function getRoles(): ?array | |
{ | |
return $this->roles; | |
} | |
public function setRoles(array $roles) | |
{ | |
$this->roles = $roles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment