Last active
October 31, 2016 22:11
-
-
Save xphere/5425313 to your computer and use it in GitHub Desktop.
Trying to reproduce https://github.com/symfony/symfony/issues/6727
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 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(); | |
} | |
} |
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 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; | |
} | |
} |
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 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(); | |
} | |
} |
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 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