dimanche 12 avril 2015

Symfony2: Form should show Entity by Field related to another table

I have a form which should show all entries in a table, that is related to another table.


controller:



public function maintainProjectAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$project = $em->getRepository('PsoProjectBundle:Project')->find($id);
$form = $this->createForm(new MaintainProjectType(),$project);
$form->handleRequest($request);

$article = $em->getRepository('PsoProjectBundle:Project')->findOneByProjects($id);
$form2 = $this->createForm(new ArticleType(),$article);
$form2->handleRequest($request);
}


Entity:



// src/Pso/ProjectBundle/Entity/Article.php
namespace Pso\ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
* @ORM\Entity
* @ORM\Table(name="article", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
*/
class Article

{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="Pso\ProjectBundle\Entity\Project", inversedBy="article")
* @ORM\JoinColumn(name="project_id", referencedColumnName="id")
*/
private $projects;

/**
* @ORM\ManyToOne(targetEntity="Pso\LogBundle\Entity\User", inversedBy="article")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $users;


/**
* @ORM\Column(type="string", length=50, nullable=false)
*/
private $category;

/**
* @ORM\Column(type="blob", nullable=true)
*/
private $product_description;

/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $article_nr;


/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $color;

/**
* @ORM\Column(type="integer", length=20, nullable=true)
*/
private $carton_qty;

/**
* @ORM\Column(type="integer", length=20, nullable=true)
*/
private $qty;

/**
* @ORM\Column(type="integer", length=20, nullable=true)
*/
private $carton;

/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $size;

/**
* @ORM\Column(type="float", length=2, nullable=true)
*/
private $price;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $price_calculation_file;

/**
* @ORM\Column(type="smallint", length=1)
*/
private $closed;


/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbinsert;


/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbupdate;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $product_packaging;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $outer_packaging;
}


The problem is, that the Formtype should show all entries, where $projects = $id


All things I tried, didn't work.


I tried to get it work also inside the formbuilder with



->add('id','entity', 'hidden', array(
'label' => 'ID',
'class' => 'Pso\ProjectBundle\Entity\Article',
'property' => 'id',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('a')
->FROM('PsoProjectBundle:article a')
->JOIN('a.projects p')
->WHERE('p.id = :id')
->setParameter('id', $id);
},
))


The goal is, that in the form sould be shown all articles, that belong to the project-ID in the controller.


Aucun commentaire:

Enregistrer un commentaire