Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:25
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 fesor/db2dd19e71a40ed2297b to your computer and use it in GitHub Desktop.
Save fesor/db2dd19e71a40ed2297b to your computer and use it in GitHub Desktop.
<?php
class Photo
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var File
* @ORM\Embedded(class="File")
*/
private $image;
/**
* @var User
* @ORM\ManyToOne(targetEntity="User")
*/
private $author;
/**
* @var Complaint[]|ArrayCollection
* @ORM\OneToMany(targetEntity="Complaint", mappedBy="photo", cascade={"persist", "remove"})
*/
private $complaints;
/**
* @var \DateTime
* @ORM\Column(type="datetime")
*/
private $createdAt;
public function __construct(File $image, User $author)
{
$this->image = $image;
$this->author = $author;
$this->compltaints = new ArrayCollection();
$this->createdAt = new \DateTime();
}
public function complain(User $user) {
// тут еще можно проверить не жаловался ли уже чувак но мне лень
$this->compltaints->add(new Complaint($this, $user));
}
}
<?php
class PhotoController extends Controller {
public function createAction(Request $request) {
// ... какие-то штуки для загрузки файлов и прочая чушь
$photo = new Photo($file, $this->getUser());
$this->get('app.repository.photo')->add($photo);
return $photo // или что-то в этом духе
}
// парам конвертеры все за нас достанут по id-шке
public function compltainAction(Photo $photo) {
$photo->compltain($this->getUser());
return $photo;
}
}
<?php
class PhotoRepository
{
private $em;
public function __construct(EntityManagerInterface $em) {
$this->em = $em;
}
public function add(Photo $photo) {
$this->em->persist($photo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment