Symfony formbuilder self-reference entity grouped - symfony

I want a choice list (dropdown) grouped by parent categories (not selectable).
Is this even possible?
Example:
- Vehiculs (not selectable)
-- Car (selectable)
-- Boat (selectable)
- Computers (not selectable)
Entity Category
/**
* #ORM\OneToMany(targetEntity="Category", mappedBy="parent")
**/
private $children;
/**
* #ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
**/
private $parent;
Form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text',
[
'attr' => ['placeholder' => 'Titel', 'class' => 'form-control', 'autocomplete' => 'off'],
'label' => false
]
)
...
->add('category', 'entity',
[
'property' => 'name',
'class' => '***ArticleBundle:Category',
]
)
;
}
With the code above i only get the parents and they are selectable.
I would like to group the children of those parents (1 depth) and make only the children selectable options.

Just posting this because this seems to be the first google hit for this problem and I had to solve it.
You in fact can directly tell symfony to create a tree select in FormBuilder with a small workaround which is totally clean.
The fact that the "Entity" is a child of "Choice" helps there a lot. The solution consists of three parts: Controller, FormType and Entity Repository
Let's start with the Controller
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(new AcmeType(array(
'repository' => $em->getRepository('AcmeBundle:Category'),
)), $data, array(
'action' => *add your route here*
'method' => 'POST'
));
Straight forward form generation, except the adition of the repository as a parameter for your FormType (which we need there)
Your FormType includes the following
/** #var CategoryRepository */
protected $repository;
public function __construct($options = array()){
$this->repository = $options['repository'];
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
*add your other stuff here*
->add('category', 'entity', array(
'empty_value' => 'Please Select...',
'required' => true,
'choices' => $this->repository->getCategoryData(),
'class' => 'Acme\AcmeBundle\Entity\Category'
))
;
}
We create an Entity field type and add the "choices" and fill it with the response from the "getCategoryData".
In your Repository of your Data Entity (in this case Category Entity) you create the following function
public function getCategoryOptions()
{
$data = array();
$categories = $this
->createQueryBuilder('c')
->select('c')
->getQuery()
->getResult();
foreach( $categories as $category ){
/** #var Category $category */
if( !$category->getParent() ){
continue;
}
if(!array_key_exists($category->getParent()->getName(), $data) ){
$data[$category->getParent()->getName()] = array();
}
$data[$category->getParent()->getName()][$category->getId()] = $category;
}
return $data;
}
This simple function does a simple query to the Database to select your Categories and then runs a foreach through them and builds an array. As seen in the FormType above it parses that array directly to the 'choices' parameter of your Entity field type.
The Form Renderer of symfony is smart enough to add tags with labels.
Et voila, you have a grouped dropdown box with non-selectable "Parents".

You can not directly tell symfony to create a tree select in form builder.
First, you have to check here;
http://symfony.com/doc/current/reference/forms/types/entity.html
You can use query builder on your entity field to get parent - child relation but parents also would be selectable.
So you have to look another solutions like this:
How to disable specific item in form choice type?

Related

Symfony2 Nested CollectionType form w/ Dependency injection data not saving

I have a problem with the dependency injection, and saving it to the database, I have a following formType inside another form which i use in two different controller routes which handle the same entity: Creating a new entity and editing it. When i'm trying to create a new entity and set $user field for it, it will save user field as empty string.
class SomeClass extends AbstractType
{
private $tokenStorage;
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$user = $this->tokenStorage->getToken()->getUser();
$builder
->add('price', null, array(
'label' => 'price',
'required' => true,
'translation_domain' => 'some_translation',
'attr' => array(
'placeholder' => 'price'
)
))
->add('user', HiddenType::class, array(
'data' => $user
));
// dump($user);
// exit(0);
}
I have tried it out and dumped it and debugged and what i've found out is that when i dump $user i get the proper data i need firstName, lastName etc. , but when i submit the data it does not save it to the database instead i get empty string. Then i dumped out the whole form after submit in the controller and found out that the user field is null.
However it does work when i go to edit route and edit the existing entity, but if i create new entity on the edit form it does work on the one that exists, but not the newly made entity.
I have also checked the controller and the Entity class itself and those are not the problem.

symfony filter collection field type like entity field type

I have a form with a collection field type.
I'd like to filter it as we can do for entity field types but I'm not finding the solution.
i found other similar questions but no satisfiable answer so far. Can we do something like :
$builder
->add('userIngredients', 'collection', array(
'type' => new UserImportedIngredientType($this->userIngredients),
'query_builder'=>$this->queryBuilder,
))
;
If not, can we use form listener event to exclude some elements based on the object property ? How ?
This collection represents userIngredients that I want the user to be able to change if they have their property isImported set to true, hence the search for the query_builder solution.
Well, I figured I could do something as simple as build a regular form not attached to a parent entity.
In case this might help someone :
class UserImportedIngredientType extends AbstractType
{
protected $userIngredients;
protected $userImportedIngredients;
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($this->userImportedIngredients as $userImportedIngredient)
{
/**
* #var $userImportedIngredient UserIngredient
*/
$builder
->add($userImportedIngredient->getId(), 'genemu_jqueryselect2_entity', array(
'query_builder'=>$this->userIngredients,
'class' => 'AppBundle:FoodAnalytics\UserIngredient',
'multiple' => false,
'label' => $userImportedIngredient->getName(),
'required'=>false,
'mapped' => false,
'data' => $userImportedIngredient
))
;
}
}
/**
* #return string
*/
public function getName()
{
return 'appbundle_foodanalytics_user_imported_ingredient';
}
public function __construct($userIngredients, $userImportedIngredients)
{
$this->userIngredients=$userIngredients;
$this->userImportedIngredients=$userImportedIngredients;
}
}
I solved it like this in my case with additionally Sonata on top of Symfony. What I did was feeding the 'data' parameter the specifically doctrine queried array of entities result. (In Repository: return $queryBuilder->getQuery()->getResult();)
/** #var MyEntityRepository $myEntityRepository */
$myEntityRepository = $this->getEntityManager()->getRepository(MyEntity::class);
/** #var MyEntity[] $myEntities */
$myEntities = $myEntityRepository->findBySomeCriteriaFilter(
$parameter, Constants::specificConstant
);
$formMapper->add(
// html name=
'myEntityProperty',
\Symfony\Component\Form\Extension\Core\Type\CollectionType::class,
[
// specific form for MyEntity
'entry_type' => new Form\MyEntity\MyEntityType(),
'allow_add' => true,
'label' => false,
'entry_options' => [
'label' => false
],
// the filtered array of entities from doctrine repository query above
'data' => $myEntities,
]
);
Use this instead of sonata_type_model_list, I guess.
Also if you want to filter sonata_type_model, use EntityType instead and use the 'query_builder' option with a Closure and returning the queryBuilder instead of the array of entities. This is all very inconsistent, best don't use symfony and sonata at all.
As far as I know Collection does not have the query_builder option
So you cannot go this way.
Is hard to decipher what you are trying to do with 4 lines of formType.
Your code looks alright, except the unsuppported query_builder, and you are already passing the userIngredients to the constructor.
My though is that if you need to filter this, then you shouldn't do it in the collection Type, but in other place.
EDITED:
On a second though, you are trying to filter the collection in the wrong place. Is not on the base collection , but in :
class UserImportedIngredientType extends AbstractType {
function __construct( $userIngredients ) {
// Do your stuff
}
function buildForm( FormBuilderInterface $builder, array $options) {
// Add here your entity with the query_filter option :)
}
}
Check the other entries in SO over Collections, there are many of them

What should I do? Doctrine relations

I want to create a dynamic form Photo with these fields:
title
album
Album is a checkboxes field ( with 'multiple' attr ).
Photo has a manyToOne relationship with Album.
What I want to do is persist several times the photo with different album values, not persist an arrayCollection of albums in one photo.
I tried to do
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$data = $form->getData();
$listeAlbum = $data['album'];
foreach ($listeAlbum as $album) {
$em->persist($photo);
$em->flush();
}
I get the error ( at the line bind->($request) )
Catchable Fatal Error: Argument 1 passed to MySite\TestBundle\Entity
\Photo::setAlbum() must be an instance of MySite\TestBundle\Entity\Album,
instance of Doctrine\Common\Collections\ArrayCollection given, ...
Is there a way to do this?
EDIT : more code.
my form type
class PhotoType extends AbstractType {
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('file', 'file') // , array("attr" => array("multiple" => "multiple", ))
->add('titre');
$user = $this->securityContext->getToken()->getUser();
if (!$user) {
throw new \LogicException(
'The FriendMessageFormType cannot be used without an authenticated user!'
);
}
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($user) {
$form = $event->getForm();
$formOptions = array(
'multiple' => true, //several choices
'expanded' => true, // activate checkbox instead of list
'class' => 'EVeilleur\DefuntBundle\Entity\Album',
'property' => 'titre',
'query_builder' => function (EntityRepository $er) use ($user) {
// build a custom query
return $er->createQueryBuilder('u')->add('select', 'u')
->add('from', 'EVeilleurDefuntBundle:Album u');
// ->add('where', 'u.id = ?1')
// ->add('orderBy', 'u.name ASC');
},
);
// create the field, = $builder->add()
// field name, field type, data, options
$form->add('album', 'entity', $formOptions);
}
);
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'EVeilleur\DefuntBundle\Entity\Photo'
));
}
/**
* #return string
*/
public function getName()
{
return 'eveilleur_defuntbundle_photo';
} }
entity relation, in Photo.php
/**
* #ORM\ManyToOne(targetEntity="EVeilleur\DefuntBundle\Entity\Album")
* #ORM\JoinColumn(nullable=true)
*/
private $album;
I think the problem is that you are adding Album to your Photo form as a single entity field, but your options mean it gets translated into a set of checkboxes that is logically turned on submit by Symfony into an ArrayList. Your Photo entity expects only a single Album.
Could you post the rest of your Photo entity, specifically the setAlbum method (apologies if you don't actually need to create one in this case, I always create mine even if they're trivial!)?
If Photo is ManyToOne with Album, then many Photos can be in one Album, and the Album field should probably be a dropdown - you shouldn't be able to choose multiple Albums. This may be the root of the issue.
If many Photos can be in one Album, but also each Photo can be in many Albums, then you really have a ManyToMany relationship, doesn't seem to be modelled like that here.

Symfony2 Unidirectional ManyToMany Collection Form

I'm stuck :-)
Maybe I just have the wrong keywords in my research. But I don't get through. So I hope one of the crowd can help me!
I have a unidirectional ManyToMany Association. When I try to submit the form (and therefore persist), I get the error:
A new entity was found through the relationship 'TrainerInnenPool\AppBundle\Entity\Trainer#applicationFields' that was not configured to cascade persist operations for entity: TrainerInnenPool\AppBundle\Entity\ApplicationField#0000000022ef36c600000000087bcbc3.
When I do "cascade persist" a new Entity is created which actually already exists.
I have 2 Entities:
Trainer
ApplicationField
The Trainer has a unidirectional ManyToMany association to the ApplicationField:
class Trainer {
public function __construct()
{
$this->applicationFields = new ArrayCollection();
}
[...]
/**
* #ORM\ManyToMany(targetEntity="ApplicationField")
*/
protected $applicationFields;
The ApplicationField has a self-referencing OneToMany association:
class ApplicationField {
public function __construct() {
$this->children = new ArrayCollection();
}
[...]
/**
* #ORM\OneToMany(targetEntity="ApplicationField", mappedBy="parent")
*/
private $children;
/**
* #ORM\ManyToOne(targetEntity="ApplicationField", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
I want to create a form where I can add a Trainer - ApplicationField association.
Therefore I have an ApplicationFieldCollectionType:
class ApplicationFieldCollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('applicationFields', 'collection', array(
'type' => new ApplicationFieldType(),
'allow_add' => true,
'label' => false
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TrainerInnenPool\AppBundle\Entity\Trainer',
));
}
The embeded Type is as follows:
class ApplicationFieldType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('applicationFieldName', 'entity', array(
'class' => 'TrainerInnenPoolAppBundle:ApplicationField',
'label' => false,
'mapped' => false,
'property' => 'name',
'query_builder' => function(EntityRepository $repository) {
return $repository->createQueryBuilder('application_field')
->where('application_field.parent is NULL');
}
));
$builder->add('name', 'entity', array(
'class' => 'TrainerInnenPoolAppBundle:ApplicationField',
'label' => false,
'property' => 'name',
'query_builder' => function(EntityRepository $repository) {
return $repository->createQueryBuilder('application_field')
->where('application_field.parent = 1');
}
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TrainerInnenPool\AppBundle\Entity\ApplicationField',
));
}
Last missing part: The controller:
public function editApplicationField($id, Request $request)
{
$entityManager = $this->getDoctrine()->getEntityManager();
$trainer = $entityManager->getRepository('TrainerInnenPoolAppBundle:Trainer')->find($id);
$editForm = $this->createForm(new ApplicationFieldCollectionType(), $trainer);
if ($request->getMethod() == 'POST') {
$editForm->handleRequest($request);
$entityManager->flush();
}
When I fetch the ApplicationField entities from the Trainer and try to persist those,
$editForm->handleRequest($request);
$af = $trainer->getApplicationFields();
foreach ($af as $applicationField) {
$trainer->addApplicationField($applicationField);
$entityManager->persist($applicationField);
}
$entityManager->flush();
I'm not able to do so, since I get a "Duplicate Entry For Key PRIMARY" - Exception.
I think I terribly miss any obvious point. If someone could help me, gave me a hint or just mentioned to update my question with information, I would be so thankful.
Kind regards...
Your nested type setters are not called because of a missing property:
->add('applicationFields', 'collection', array(
'type' => new ApplicationFieldType(),
...
'by_reference' => false
...
http://symfony.com/doc/current/reference/forms/types/collection.html#by-reference
When you plan to have collection fields that are addable/deletable("allow_add", "allow_delete"), you should always provide the "by_reference" => false option, in order to call the setters directly on the related entities and then build the association, rather than chain methods from the original entity.
Hope this helps!
Well you need the cascade={"persist"} annotation because you want to persist the whole entity with its association to ApplicationField. If you don't use cascade={"persist"}, you'd have to manually persist the entities.
The entities are already added to the trainer, so if you want to manually persist the entities you should remove the line
$trainer->addApplicationField($applicationField);
and only execute the persist.
This should work. Try it. But I think the effect will be the same as if you use cascade persist. So it's not the final solution I think, but the first step to understand the problem, why the manual persist didn't work before.

Form widget not bound to a Doctrine2 association?

I have a form for creating a new BroadcastMessage entity and i need to display a widget of type <select multiple="multiple"> bound to excludedUsers property, not directly related to a Doctrine2 an association.
Inside my BroadcastMessageType class (inherits from AbstractType):
$builder->add('excludedUsers, 'entity', array(
'class' => 'Acme\MyBundle\Enrity\User',
'property' => 'username',
'multiple' => true
));
This of course works for creating a new BroadcastMessage; but on editAction i need a complex query to get excluded users. I need to compute excluded users looking for a record in a cross-reference table named broadcast_message_reference.
My question is fairly simple: where to add this "complex query" in order to get excludedUsers property correctly bound to the <select multiple="multiple"> widget? Inside my getExludedUsers method? If yes, how i'm supposed to access the entity repository for that query?
class BroadcastMessage
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private excludedUsers;
public function __costrunct()
{
$this->excludedUsers = new ArrayCollection();
}
public function addExcludedUser(Acme\MyBundle\Enrity\User $user)
{
$this->excludedUsers[] = $user;
return $this;
}
public function getExcludedUsers() { return $this->excludedUsers; }
}
Actually, I think, you need query_builder option within your form item definition:
$builder->add('excludedUsers', 'entity', array(
'class' => 'Acme\MyBundle\Enrity\User',
'property' => 'username',
'multiple' => true,
'query_builder' => function(EntityManager $em){
// you have an instance of EntityManager so you may build
// arbitrary QueryBuilder. Just remember to return it
// for example:
$qb = $em->createQueryBuilder()
->from('Acme\MyBundle\Enrity\User u')
->where('u.excluded = true');
return $qb;
},
'property' => 'username'
));
This is just rough example, but you can find more about entity form type here.
I think your problem can be solved by Form events. Create a service for EventSubscriber class, inject EntityManager and then subscribe for FormEvents::SET_DATA event.

Resources