我想在尝试保持时避免重复,但是如果在Brands
实体中已经设置了品牌名称,那么我在控制器中的逻辑似乎不起作用。因此,如果我尝试将Mercedes
作为品牌名称继续存在,那么我会收到此错误“品牌已存在于数据库中”。它应该做的是简单地避免持久化brands
并将cars
对象与id
brands
实体保持在一起。我该如何解决这个问题?
1 to Many (Brands to Cars)
注意:删除@UniqueEntity(fields="name", message = "The Brand already exists in database.")
的选项不可行,因为我在应用程序的其他位置使用brands
表单类型。
CONTROLLER
$form = $this->createForm(new BothType(), null, array('action' => $this->generateUrl('both')));
$form->handleRequest($request);
if ($form->isValid())
{
$brandsData = $form->get('brands')->getData();
$carsData = $form->get('cars')->getData();
$repo = $this->getDoctrine()->getRepository('CarBrandBundle:Brands');
$brands = $repo->findByName($brandsData->getName());
//If Brands doesn't exist yet
if (! $brands)
{
$brands = new Brands();
$brands->setName($brandsData->getName());
$brands->setOrigin($brandsData->getOrigin());
$cars = new Cars();
$cars->setModel($carsData->getModel());
$cars->setPrice($carsData->getPrice());
$cars->setBrands($brands);
$em = $this->getDoctrine()->getManager();
$em->persist($brands);
$em->persist($cars);
$em->flush();
}
//If exists
else
{
$cars = new Cars();
$cars->setModel($carsData->getModel());
$cars->setPrice($carsData->getPrice());
$cars->setBrands($brands);
$em = $this->getDoctrine()->getManager();
$em->persist($cars);
$em->flush();
}
return new Response('Brands ID:'. $brands->getId().'-Cars ID:'.$cars->getId());
}
FORM TYPE
class BothType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->setAction($options['action'])
->add('brands', new BrandsType())
->add('cars', new CarsType())
->add('button', 'submit', array('label' => 'Add'))
;
}
}
品牌
/**
* @ORM\Entity
* @ORM\Table(name="brands", uniqueConstraints={@ORM\UniqueConstraint(columns={"name"})})
* @UniqueEntity(fields="name", message = "The Brand already exists in database.")
*/
class Brands
{
protected $id;
protected $name;
/**
* @ORM\OneToMany(targetEntity="Cars", mappedBy="brands")
*/
protected $cars;
public function __construct()
{
$this->cars = new ArrayCollection();
}
public function getId() { return $this->id; }
public function setName($name) { $this->name = $name; return $this; }
public function getName() { return $this->name; }
public function addCar(\Car\BrandBundle\Entity\Cars $cars)
{
$this->cars[] = $cars;
return $this;
}
public function removeCar(\Car\BrandBundle\Entity\Cars $cars)
{
$this->cars->removeElement($cars);
}
public function getCars()
{
return $this->cars;
}
}
CARS
/**
* @ORM\Entity
* @ORM\Table(name="cars")
*/
class Cars
{
protected $id;
protected $model;
/**
* @ORM\ManyToOne(targetEntity="Brands", inversedBy="cars")
* @ORM\JoinColumn(name="brands_id", referencedColumnName="id")
*/
protected $brands;
public function getId() { return $this->id; }
public function setModel($model) { $this->model = $model; return $this; }
public function getModel() { return $this->model; }
public function setBrands(\Car\BrandBundle\Entity\Brands $brands = null)
{
$this->brands = $brands;
return $this;
}
public function getBrands()
{
return $this->brands;
}
}