Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xphere
Last active October 31, 2016 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xphere/5425313 to your computer and use it in GitHub Desktop.
Save xphere/5425313 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\DemoBundle\Entity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class DemoController extends Controller
{
/**
* @Route("/")
*/
public function indexAction()
{
$user = $this->findUser();
$product = $this->findProduct();
$like = new Entity\Like($user, $product);
$errors = $this->get('validator')->validate($like);
if (count($errors) === 0) {
$this->getDoctrine()->getManager()->persist($like);
$this->getDoctrine()->getManager()->flush($like);
}
var_dump($errors);
}
/**
* @return Entity\User
*/
protected function findUser()
{
$user = $this->getDoctrine()->getRepository('AcmeDemoBundle:User')->findOneBy(array());
if (!empty($user)) {
return $user;
}
$user = new Entity\User();
$manager = $this->getDoctrine()->getManager();
$manager->persist($user);
$manager->flush($user);
return $this->findUser();
}
/**
* @return Entity\Product
*/
protected function findProduct()
{
$product = $this->getDoctrine()->getRepository('AcmeDemoBundle:Product')->findOneBy(array());
if (!empty($product)) {
return $product;
}
$product = new Entity\Product();
$manager = $this->getDoctrine()->getManager();
$manager->persist($product);
$manager->flush($product);
return $this->findProduct();
}
}
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity
* @ORM\Table(name="likes", uniqueConstraints={
* @ORM\UniqueConstraint(columns={"user_id", "product_id"})
* })
* @UniqueEntity(fields={"user", "product"})
*/
class Like
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="likes")
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="likes")
*/
private $product;
public function __construct(User $user, Product $product)
{
$this->user = $user;
$this->product = $product;
}
}
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Like", mappedBy="product")
*/
private $likes;
public function __construct()
{
$this->likes = new ArrayCollection();
}
}
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Like", mappedBy="user")
*/
private $likes;
public function __construct()
{
$this->likes = new ArrayCollection();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment