Symfony教义多对多插入 [英] Symfony Doctrine Many to Many insert
问题描述
只有很多元素的实体请求
class Requests {
/ **
* @ ORM\ManyToMany(targetEntity =Tipi,inversedBy =requests)
* @ ORM\JoinTable(name =tipi_richieste)
* /
private $ tipi;
public function __construct(){
$ this-> tipi = new \Doctrine\Common\Collections\ArrayCollection();
}
/ **
*添加tipi
*
* @param \AppBundle\Entity\Tipi $ tipi
*
* @return请求
* /
public function addTipi(\AppBundle\Entity\Tipi $ tipi){
$ this-> tipi [] = $ tipi;
return $ this;
}
/ **
*删除tipi
*
* @param \AppBundle\Entity\Tipi $ tipi
* /
public function removeTipi(\AppBundle\Entity\Tipi $ tipi){
$ this-> tipi-> removeElement($ tipi);
}
/ **
*获取提示
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getTipi(){
return $ this-> tipi;
}
}
只有很多元素的实体Tipi
class Tipi {
/ **
* @ ORM\ManyToMany(targetEntity =Requests,mappedBy = tipi)
* /
private $ requests;
/ **
*构造函数
* /
public function __construct(){
$ this-> requests = new \\ \\Doctrine\Common\Collections\ArrayCollection();
}
/ **
*添加请求
*
* @param \AppBundle\Entity\Requests $ request
*
* @return Tipi
* /
public function addRequest(\AppBundle\Entity\Requests $ request)
{
$ this->请求[] = $ request;
return $ this;
}
/ **
*删除请求
*
* @param \AppBundle\Entity\Requests $ request
* /
public function removeRequest(\AppBundle\Entity\Requests $ request)
{
$ this-> requests-> removeElement($ request);
}
/ **
*获取请求
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getRequests()
{
return $ this-> requests;
}
}
插入的表单类型是EntityType
- > add('tipi',EntityType :: class,array(
'label'=>'Tipo' ,
'class'=>'AppBundle:Tipi',
'mapped'=> false,
'attr'=> array('class'=>'form-控制'),
'multiple'=> true,
'by_reference'=> false,
'query_builder'=> function(EntityRepository $ er){
return $ er-> createQueryBuilder('t');
},
))
在我的控制器中,我以这种方式工作:
public function indexAction(Request $ request){
$ requests = new Requests();
$ em = $ this-> getDoctrine() - > getManager();
$ form = $ this-> createForm(RequestsType :: class,$ requests);
$ form-> handleRequest($ request);
if($ form-> isSubmitted()&& $ form-> isValid()){
$ requests-> setCreateAt(new \日期时间( '现在'));
$ request_tipi = $ form [tipi] - > getData();
$ tipi_array = [];
die($ form [tipi] - > getData());
$ tipi_array = $ em-> getRepository('AppBundle:Tipi') - > findOneBy(array('codice'=> $ form [tipi] - > getData()));
$ tipi = new Tipi();
$ requests-> addTipi($ form [tipi] - > getData());
$ em-> persist($ requests);
$ em-> flush();
// return $ this-> redirectToRoute('immovable_edit',array('id'=> $ immovables-> getId()));
}
return $ this-> render('AppBundle:Requests:index.html.twig',array(
'requests'=> $ requests,
'form '=> $ form-> createView(),
));
}
当我将死亡返回值 $ form [tipi] - > getData()
我得到一个数组集合:
\Common\Collections\ArrayCollection @ 000000005b52ae6b00000000731dd0b4
但是我收到这个错误
可追踪致命错误:参数1传递给AppBundle\Entity\Requests :: addTipi()必须是AppBundle\Entity\Tipi的一个实例, Doctrine\Common\Collections\ArrayCollection的实例,在C:\xampp\htdocs\bugaro\src\AppBundle\Controller\RequestsController.php在第31行中调用并定义
请求:: addTipi()
单个提示到其内部集合。因此,您无法在通话中添加整个 ArrayCollection
。
您有两个选项。
执行 foreach
foreach($ form [tipi] - > getData()as $ tipi){
$ requests-> addTipi($ tipi);
}
创建多个选项卡,如 Requests :: addCollections (ArrayCollection [])
public function addTipis($ tipis){
foreach($ tipis as $ tipi){
$ this-> tipi [] = $ tipi;
}
return $ this;
}
无论如何,您的代码中都有很多修复。对于单个帖子来说太多了。
但是这些修复中最重要的是您不需要大多数控制器代码。 ; - )
由于您将 $ requests
对象传递到表单中,所以它已经被双向,这意味着Symfony的表单组件应该使用新的值自动填充它的属性。这包括许多关系的集合。
另外如果你不传递该对象, $ form-> getData()
应该返回一个新的 Resnponses
实例,因此您不需要创建它并手动传递,直到现有实例的版本。
I have a problem with my entities and controllers in Symfony. I would insert on my DB value in a many to many table generated.
Entity Requests with only many to many elements
class Requests {
/**
* @ORM\ManyToMany(targetEntity="Tipi", inversedBy="requests")
* @ORM\JoinTable(name="tipi_richieste")
*/
private $tipi;
public function __construct() {
$this->tipi = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add tipi
*
* @param \AppBundle\Entity\Tipi $tipi
*
* @return Requests
*/
public function addTipi(\AppBundle\Entity\Tipi $tipi) {
$this->tipi[] = $tipi;
return $this;
}
/**
* Remove tipi
*
* @param \AppBundle\Entity\Tipi $tipi
*/
public function removeTipi(\AppBundle\Entity\Tipi $tipi) {
$this->tipi->removeElement($tipi);
}
/**
* Get tipi
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTipi() {
return $this->tipi;
}
}
Entity Tipi with only many to many elements
class Tipi {
/**
* @ORM\ManyToMany(targetEntity="Requests", mappedBy="tipi")
*/
private $requests;
/**
* Constructor
*/
public function __construct() {
$this->requests = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add request
*
* @param \AppBundle\Entity\Requests $request
*
* @return Tipi
*/
public function addRequest(\AppBundle\Entity\Requests $request)
{
$this->requests[] = $request;
return $this;
}
/**
* Remove request
*
* @param \AppBundle\Entity\Requests $request
*/
public function removeRequest(\AppBundle\Entity\Requests $request)
{
$this->requests->removeElement($request);
}
/**
* Get requests
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRequests()
{
return $this->requests;
}
}
The Form type for insert is an EntityType
->add('tipi', EntityType::class, array(
'label' => 'Tipo',
'class' => 'AppBundle:Tipi',
'mapped' => false,
'attr' => array('class' => 'form-control'),
'multiple' => true,
'by_reference' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('t');
},
))
And in my controller I work in this way:
public function indexAction(Request $request) {
$requests = new Requests();
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(RequestsType::class, $requests);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$requests->setCreateAt(new \DateTime('now'));
$request_tipi = $form["tipi"]->getData();
$tipi_array = [];
die($form["tipi"]->getData());
$tipi_array = $em->getRepository('AppBundle:Tipi')->findOneBy(array('codice' => $form["tipi"]->getData()));
$tipi = new Tipi();
$requests->addTipi($form["tipi"]->getData());
$em->persist($requests);
$em->flush();
//return $this->redirectToRoute('immovable_edit', array('id' => $immovables->getId()));
}
return $this->render('AppBundle:Requests:index.html.twig', array(
'requests' => $requests,
'form' => $form->createView(),
));
}
When I put die for return the value of $form["tipi"]->getData()
I get an array collection :
Doctrine\Common\Collections\ArrayCollection@000000005b52ae6b00000000731dd0b4
But I get this error
Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\Requests::addTipi() must be an instance of AppBundle\Entity\Tipi, instance of Doctrine\Common\Collections\ArrayCollection given, called in C:\xampp\htdocs\bugaro\src\AppBundle\Controller\RequestsController.php on line 31 and defined
Request::addTipi()
fuction adds single tipi to its internal collection. Therefore you can't add whole ArrayCollection
in call.
You have two options.
Do a foreach
foreach($form["tipi"]->getData() as $tipi) {
$requests->addTipi($tipi);
}
Create multisetter like Requests::addCollections(ArrayCollection[])
public function addTipis($tipis) {
foreach($tipis as $tipi) {
$this->tipi[] = $tipi;
}
return $this;
}
Anyway there's a lot to fix in your code. Too much for a single post here.
But the most important of these fixes is that you don't need most of your controller code. ;-)
Since you pass $requests
object into the form, it's already bined two-way, which means that Symfony's Form Component should automatically fill it's properties with new values. That includes many to many relation's collections.
Also if you wouldn't pass that object, $form->getData()
should return a new Resnponses
instance, therefore you don't need to create it and pass manually until it's edition of existing instance.
这篇关于Symfony教义多对多插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!