Symfony2 simple many-to-many issue - symfony

I have a simple question,
I have two tables in relation many to many, Post and Category,
in an intact form PostType a collection of form CategoryType, but here the problems begin ..
I followed the instructions on the cookbook collection form to persist the data, I just do not get the desired result ..
Here's the code:
class Post
{
/**
*
* #ORM\ManyToMany(targetEntity="Categories", inversedBy="posts", cascade={"persist", "remove"})
* #ORM\JoinTable(name="AnCat",
* joinColumns={
* #ORM\JoinColumn(name="post_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="categories_id", referencedColumnName="id")
* }
* )
**/
protected $categories;
public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addCategory($categories)
{
foreach ($categories as $category) {
$category->addPosts($this);
}
$this->categories[] = $categories;
}
class Categories
{
/**
*
* #ORM\ManyToMany(targetEntity="Post", mappedBy="categories")
*/
protected $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
/**
*
* #param Post $post
* #return Categories
*/
public function addPosts(Post $posts)
{
// I tried it but I get the same result!
/*if (!$this->posts->contains($posts)) {
$this->posts->add($posts);
}*/
$posts->addCategory($this);
$this->posts[] = $posts;
}
class PostType extends AbstractType
{
->add('Categories', 'collection', array('type' => new CategoriesType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__categ__',
'by_reference' => false
))
class CategoriesType extends AbstractType
{
->add('category', 'entity', array(
'attr' => array('class' => 'cat'),
'class' => 'MyBusinessBundle:Categories',
'property' => 'category',
'label' => 'Categories'
))
The problem is that inserts a new field Category, instead of creating a simple relationship Post-Category.
I don't understand where I'm wrong ..

In your postType, change collection type into entity Type
class PostType extends AbstractType
{
$builder->add('Categories', 'entity',
array( 'label' => 'Categories',
'required' => false,
'expanded' => true,
'class' => 'xxx\xxxBundle\Entity\Categories',
'property' => 'title',
'multiple' => true,
));
In your post creation form you will have checkboxes with categories. If you want a multi select field, change expanded by false

Related

Symfony2 Cannot instantiate interface Doctrine\Common\Collections\Collection

Symfony version 2.8
I have a problem when I try to add new Collection(); in construct function of User Entity.
public function __construct()
{
$this->sectors = new Collection();
parent::__construct();
}
Sector has many to many relationship
/**
* #ORM\ManyToMany(targetEntity="UserBundle\Entity\Sectors", fetch="EAGER")
* #ORM\JoinTable(
* joinColumns={#ORM\JoinColumn(onDelete="CASCADE")},
* inverseJoinColumns={#ORM\JoinColumn(onDelete="CASCADE")}
* )
*/
public $sectors;
and getter/setter methods are
/**
* Add sector
*
* #param UserBundle\Entity\Sectors $sector
*
* #return User
*/
public function addSector(UserBundle\Entity\Sectors $sector)
{
$this->sectors[] = $sector;
return $this;
}
/**
* Remove sector
*
* #param UserBundle\Entity\Sectors $sector
*/
public function removeSector(UserBundle\Entity\Sectors $sector)
{
$this->sectors->removeElement($sector);
}
/**
* Get sectors
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getSectors()
{
return $this->sectors;
}
When in a FormType I do:
$builder
->add('sectors', EntityType::class, array(
'class' => 'UserBundle\Entity\Sectors',
'placeholder' => 'Select Sector ...',
'label' => 'Sector',
'required' => false,
'attr' => ['placeholder' => 'Select Sector ...', 'data-jcf' => '{"wrapNative": false, "wrapNativeOnMobile": false, "useCustomScroll": true, "multipleCompactStyle": true}'],
'multiple' => true,
'expanded' => false,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
));
$formModify = function (FormInterface $form, \Doctrine\Common\Collections\ArrayCollection $sector, $factory) {
$output = [];
foreach($sector as $sec) {
$output[] = $sec->id;
}
$formOption = array(
'class' => 'UserBundle\Entity\UserAccreditation',
'multiple' => true,
'auto_initialize' => false,
'required' => false,
'expanded' => true,
'choice_attr' => function ($output) {
return ['class' => 'attr_checkbox'];
},
'query_builder' => function(EntityRepository $ertt) use ($output) {
$qb = $ertt->createQueryBuilder('g');
$qb->select(array('g'));
$qb->where('g.sector IN (:sector_id)');
$qb->setParameters( array('sector_id' => $output) );
$qb->orderBy('g.name', 'ASC');
return $qb;
},
);
$form->add($factory->createNamed('accreditationdata', EntityType::class, null, $formOption));
};
$builder->addEventListener(FormEvents::PRE_SET_DATA,function (FormEvent $event) use ($formModify,$factory) {
$data = $event->getData();
$form = $event->getForm();
if ($data != null) {
//print_r(get_object_vars($data->getSectors()));
$formModify($event->getForm(), $data->getSectors(),$factory);
}
}
);
$factory = $builder->getFormFactory();
$builder->get('sectors')->addEventListener(FormEvents::POST_SUBMIT,function (FormEvent $event) use ($formModify,$factory) {
$sector = $event->getForm()->getData();
//print_r($sector);
$formModify($event->getForm()->getParent(), $sector,$factory);
}
);
I get following error:
Fatal error: Cannot instantiate interface Doctrine\Common\Collections\Collection
Earlier I am using ArrayCollection instead of Collection, I have to do this because I am getting error
Type error: Argument 2 passed to UserBundle\Form\Type\ProfileAboutMeFormType::UserBundle\Form\Type{closure}() must be an instance of Doctrine\Common\Collections\ArrayCollection, instance of Doctrine\ORM\PersistentCollection given,
and by searching on google I found this solution on github link
https://github.com/doctrine/orm/issues/5946
but still I am facing the problem. can anyone tell me What goes wrong here ?

How do I fecth and set value from database to my entity field in the edit form (Symfony2)?

I am devolping an application in Symfony 2.7, I generated the CRUD from a PRODUCT TABLE (InProducto) which is related OneToMany to another table (InUnidadMedida).
When I open Edit Form, the value in ENTITY FIELD (which is a select field from UNIDAD DE MEDIDA table) always appears the first option of related table (UNIDAD DE MEDIDA). And It suppose to get the value in the field of the table INPRODUCTO
InProductoType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nombre')
->add('descripcion')
->add('unidadMedida', 'entity', array(
'class' => 'NivalInventarioBundle:InUnidadMedida',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.nombre', 'ASC');
},
'choice_label' => 'nombre',
'by_reference' => false,
'property' => 'type',
'expanded' => false,
'multiple' => false
))
}
Controller:
private function createEditForm(InProducto $entity)
{
$form = $this->createForm(new InProductoType(), $entity, array(
'action' => $this->generateUrl('inproducto_update', array('id' => $entity->getIdProducto())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Guardar'));
return $form;
}
Producto table (Entity)
/**
* InProducto
*
* #ORM\Table(name="in_producto")
* #ORM\Entity
*/
class InProducto
{
/**
* #ORM\ManyToOne(targetEntity="InSubLinea", inversedBy="InProducto")
* #ORM\JoinColumn(name="id_sub_linea", referencedColumnName="id_sub_linea")
*/
protected $subLinea;
/**
* #ORM\ManyToOne(targetEntity="InUnidadMedida", inversedBy="InProducto")
* #ORM\JoinColumn(name="id_unidad_medida", referencedColumnName="id_unidad_medida")
*/
protected $unidadMedida;
/**
* #var integer
*
* #ORM\Column(name="id_producto", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
Unidad de medida TABLE (entity)
/**
* InUnidadMedida
*
* #ORM\Table(name="in_unidad_medida")
* #ORM\Entity
*/
class InUnidadMedida
{
/**
* #ORM\OneToMany(targetEntity="InProducto", mappedBy="InUnidadMedida")
*/
protected $InProducto;
public function __construct()
{
$this->InProducto = new ArrayCollection();
}
The form type will be guessed automatically by Symfony, if you've mapped these entities correctly.
So make it just
->add('unidadMedida', null, array(
'choice_label' => 'nombre',
'expanded' => false,
'multiple' => false
))
And there is no such option as property. Did you mean property_path?
After hours of scratching head, it was very simple, I add the following properties to my fields:
'by_reference' => true,
'mapped' => true,
Thanks Dmitry Malyshenko for your time.

Controller - No access in class - Entity

I got this error message, but I don't really understand why.
Neither the property "categories" nor one of the methods "addCategory()"/"removeCategory()", "setCategories()", "categories()", "__set()" or "__call()" exist and have public access in class "Checkout\Bundle\ItemBundle\Entity\Item".
The thing is, in my entity "Item" I really have all of this stuff:
/**
* #ORM\ManyToMany(targetEntity="Checkout\Bundle\ItemBundle\Entity\Category", mappedBy="items")
**/
private $categories;
and
/**
* Add categories
*
* #param Category $categories
* #return Item
*/
public function addCategory(Category $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove categories
*
* #param Category $categories
*/
public function removeCategory(Category $categories)
{
$this->categories->removeElement($categories);
}
/**
* Get categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
Okay, okay - and what is in my Controller?
/**
* Creates a new Item entity.
*
* #Route("/create", name="item_create")
* #param Request $request
* #return \Symfony\Component\HttpFoundation\Response
*/
public function createAction(Request $request)
{
$entity = new Item();
$currentUser = $this->getUser();
$form = $this->createFormBuilder($entity)
->add('name', null, array(
'label' => 'Item Name',
'required' => true,
))
->add('categories', 'entity', array(
'label' => 'Select a Category',
'required' => false,
'class' => 'CheckoutItemBundle:Category',
'property' => 'name',
'query_builder' => function (EntityRepository $er) use ($currentUser) {
return $er->createQueryBuilder('c')
->where('c.user = :user')
->setParameter('user', $currentUser);
},
))
->add('submit', 'submit', array('label' => 'Speichern'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
}
return $this->render(
'CheckoutItemBundle:Item:create.html.twig',
array(
'entity' => 'entity',
'form' => $form->createView()
)
);
}
So, the big question is, why he say that he didn't find it, when it is obviously there. Any idea? Thank you in advance!
->add('categories', 'entity', array(
'label' => 'Select a Category',
'required' => false,
'class' => 'CheckoutItemBundle:Category',
'property' => 'name',
'multiple' => true,
'query_builder' => function (EntityRepository $er) use ($currentUser) {
return $er->createQueryBuilder('c')
->where('c.user = :user')
->setParameter('user', $currentUser);
},
))
You have a Many-To-Many relation, but your form expects a Many-To-One. To fix this behavior need to set multiple to true.

Use ChoiceList for entity type

I'm trying to use a choice list in a form with the entity type but it's not working if I add data to the form. It's giving me a "could not convert object to int" error.
My buildForm method
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('department', 'entity', array(
'label' => 'Form.department',
'class' => 'HotfloSystemCoreBundle:Department',
'choice_list' => $this->departmentChoiceList,
'multiple' => true,
'required' => false,
'attr' => array(
'class' => 'selectpicker',
'data-actions-box' => true,
'data-live-search' => true,
'data-selected-text-format' => 'count'
),
'horizontal' => false,
));
}
My ChoiceList
class DepartmentChoiceList extends LazyChoiceList
{
/**
* #var EntityManager
*/
protected $em;
public function __construct($em)
{
$this->em = $em;
}
/**
* Loads the choice list
* Should be implemented by child classes.
*
* #return ChoiceListInterface The loaded choice list
*/
protected function loadChoiceList()
{
$departments = $this->getDepartments();
$departmentChoices = [];
foreach ($departments as $department) {
$departmentChoices[$department->getId()] = $department;
}
// Return the choice list
return new SimpleChoiceList($departmentChoices);
}
/**
* Get the departments available in the poli appointment data
*
* #return Department[]
*/
protected function getDepartments()
{
// Get the used department out of the appointment table by using a group by SQL statement
/** #var $qb QueryBuilder */
$qb = $this->em->getRepository('MyBundle:PoliAnalyzeAppointment')
->createQueryBuilder('appointment');
$qb->select('DISTINCT IDENTITY(appointment.department)');
// Get the actual departments
/** #var $qb2 QueryBuilder */
$qb2 = $this->em->getRepository('MyBundle:Department')
->createQueryBuilder('department');
$qb2->where($qb2->expr()->in('department.id', $qb->getDQL()));
$qb2->orderBy('department.name', 'ASC');
return $qb2->getQuery()->getResult();
}
}
I'm using the entity type because it should be converted to an entity and back. If I use the choice type I have to do this myself (which I don't want).
How can I achieve this?
Use query_builder option to filter entity choice list. Something like:
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('a')
->select(array('DISTINCT IDENTITY(a.department)', 'd'))
->from('MyBundle:PoliAnalyzeAppointment', 'a')
->innerJoin('a.department', 'd')
->groupBy('a.department')
->orderBy('d.name', 'ASC');
}
In the buildForm method I would fill it with the standard options array:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('department', 'entity', array(
'label' => 'Form.department',
'choice_list' => $options['departmentChoiceList'],
'multiple' => true,
'required' => false,
'attr' => array(
'class' => 'selectpicker',
'data-actions-box' => true,
'data-live-search' => true,
'data-selected-text-format' => 'count'
),
'horizontal' => false,
));
}
And then in the same formType add another method:
public function setDefaultOptions(OptionsResolverInterface $resolver) {
/*
* Instantiate DepartmentChoiceList and
* Implement the logic to build your Array and then set it this way
*/
$resolver->setDefaults(array(
'departmentChoiceList' => $yourDepartamentChoiceArray,
'data_class' => 'HotfloSystemCoreBundle:Department'
));
}
Note: I declared also the data_class here for the whole form, but you can also leave it out of the setDefaults if you want.
I suggest taking a look into the Entity type so you don't have to use a Transformer to convert object to int -> int to object for choice fields.

Symfony2 ManyToMany multiple field form collection

Is there a way to save correctly in a relationship ManyToMany field entities in a form that is "multiple" and is part of a collection?
I looked everywhere but could not find a shred of example to make me understand how to do!
I cannot find the solution for this!
class Anagrafica
{
/**
* #ORM\ManyToMany(targetEntity="SubCategories", inversedBy="anagrafiche", cascade={"persist", "remove"})
* #ORM\JoinTable(name="AnCat")
**/
private $subCategories;
//..
public function __construct()
{
$this->subCategories = new \Doctrine\Common\Collections\ArrayCollection();
//..
}
/**
* Add subCategories
*
* #param \My\BusinessBundle\Entity\SubCategories $subCategories
* #return Anagrafica
*/
public function addSubCategory(\My\BusinessBundle\Entity\SubCategories $subCategories)
{
foreach ($subCategories as $subCategory) {
$subCategory->addAnagrafiche($this);
}
$this->subCategories = $subCategories;
}
*******
class SubCategories
{
/**
* #ORM\ManyToMany(targetEntity="Anagrafica", mappedBy="subCategories")
*/
private $anagrafiche;
public function __construct()
{
$this->anagrafiche = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add anagrafiche
*
* #param \My\BusinessBundle\Entity\Anagrafica $anagrafiche
* #return SubCategories
*/
public function addAnagrafiche(\My\BusinessBundle\Entity\Anagrafica $anagrafiche)
{
if (!$this->anagrafiche->contains($anagrafiche)) {
$this->anagrafiche->add($anagrafiche);
}
}
******
AnagraficaType:
//..
->add('subCategories', 'collection', array('type' => new SubCategoriesType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__categ__',
'by_reference' => false
))
*******
SubCategoriesType:
->add('subCategory', 'entity', array(
'class' => 'CoffeeBusinessBundle:SubCategories',
'property' => 'subCategory',
'label' => 'Sotto Categorie',
'multiple' => true
))
Partially solved
If I enter the field collection directly and do these changes:
AnagraficaType:
->add('subCategories', 'collection', array(
'type' => 'entity',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__categ__',
'by_reference' => false,
'options' => array(
'label' => 'Sotto Categorie',
'multiple' => true,
'class' => 'MyBusinessBundle:SubCategories',
'property' => 'subCategory'
)
))
Anagrafica entity:
public function addSubCategory(ArrayCollection $subCategories)
{
foreach ($subCategories as $subCategory) {
$subCategory->addAnagrafiche($this);
//var_dump($subCategory);
}
$this->subCategories = $subCategories;
}
Partially get the desired result, but if I add a field in the form subcategories (by collection) saves only the last field entered.
I did a var_dump ArrayCollection object received and in fact inside there are only data from the last field entered.
Any idea?

Resources