Symfony2 entity field type alternatives to "property" or "__toString()"? - forms

Using Symfony2 entity field type one should specify property option:
$builder->add('customers', 'entity', array(
'multiple' => true,
'class' => 'AcmeHelloBundle:Customer',
'property' => 'first',
));
But sometimes this is not sufficient: think about two customers with the same name, so display the email (unique) would be mandatory.
Another possibility is to implement __toString() into the model:
class Customer
{
public $first, $last, $email;
public function __toString()
{
return sprintf('%s %s (%s)', $this->first, $this->last, $this->email);
}
}
The disadvances of the latter is that you are forced to display the entity the same way in all your forms.
Is there any other way to make this more flexible? I mean something like a callback function:
$builder->add('customers', 'entity', array(
'multiple' => true,
'class' => 'AcmeHelloBundle:Customer',
'property' => function($data) {
return sprintf('%s %s (%s)', $data->first, $data->last, $data->email);
},
));

I found this really helpful, and I wound a really easy way to do this with your code so here is the solution
$builder->add('customers', 'entity', array(
'multiple' => true,
'class' => 'AcmeHelloBundle:Customer',
'property' => 'label',
));
And in the class Customer (the Entity)
public function getLabel()
{
return $this->lastname .', '. $this->firstname .' ('. $this->email .')';
}
eh voila :D the property get its String from the Entity not the Database.

Passing a closure does not work yet, but will be added to Symfony soon: https://github.com/symfony/symfony/issues/4067

It seems this can be achievable by adding following block after elseif ($this->labelPath) block in ObjectChoiceList.php.
elseif (is_callable($this->labelPath)) {
$labels[$i] = call_user_func($this->labelPath, $choice);
}
Haven't tried it though :).

Related

QueryBuilder in the BuildForm

I have An entity Equipe that have OneToMany relation with entity Employe. It means that a team have many employees. So in EquipeType I tried to show list of employees and a chekckbox infront each of them if I want to add an employee in that team I only have to check it. That works but my problem is how to show the name, id and all other properties and put them in table. I need a for statement but what to put in it? thanks this is how I get it in my twig
My FormBuilder
->add('date')
->add('nom')
->add('employes', 'entity', array(
'class' => 'OCUserBundle:Employe',
'property' => 'username',
'multiple' => true,
'expanded' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
},
My twig
{{form_widget(form.employes)}}
Since Symfony 2.7 property in options is changed to choice_label and could be a callback like this
'choice_label' => function ($employee) {
return $employee->getName().'/'.$employee->getId();
}
This is explained in the EntityType field documentation
If you use Symfony earlier than the 2.7 version you should declare a method in Employee, something like this:
public function getDisplayName()
{
return $this->getName().'/'.$this->getId();
}
and then in options declare property as
'property' => 'display_name'
See the EntityType field documentation for Symfony 2.6 for more information.

Sonata Admin MongoDB DataGrid filter

Situation
We are running Symfony 2.8 and the latest version of Sonata Admin along with Mongo as a data store. Please consider the following object which has been simplified for the sake of this question; it does work.
class Entry
{
/* #ID */
protected $id;
/* #String */
protected $type;
/* #String */
protected $content;
}
With the above, there will be lots of entries and from the Admin itself, we would like to filter by type.
Here's an example of the dataset
Problem
We can't create a set of selectable filters in the dataGrid function which are UNIQUE for type.
Attempts
Note that where needed, the EntryRepository is included as a namespace at the start of the file
NUMBER 1
With the below, we get the type duplicated many times
->add('type', null, array(), 'document', array(
'expanded' => true,
'class' => 'Application:Entry',
'query_builder' => function(EntryRepository $dr) {
return $dr->createQueryBuilder();
}
))
NUMBER 2
With the below, we get a 500 error with only the message "string". I think this is because when using distinct, Mongo prepares a set of arrays instead of unexecuted QueryBuilder object?
->add('type', null, array(), 'document', array(
'expanded' => true,
'class' => 'Application:Entry',
'query_builder' => function(Entryepository $dr) {
return $dr->createQueryBuilder()
->distinct('type');
}
))
NUMBER 3
The attempt below is to use Map reduce to perform the equivalent of an SQL "GROUP BY" however, the same STRING error as above is provided.
->add('type', '', array(), 'document', array(
'expanded' => true,
'class' => 'Application:Entry',
'query_builder' => function(EntryRepository $dr) {
return $dr->createQueryBuilder()
->group(array(), array('type'))
->reduce('function (obj, prev) { prev.type; }');
}
))
CRUDE WORKAROUND...discouraged
The below is a demonstration using the filter (as listed in the Sonata documentation) and it DOES work...for one type at a time.
->add('type', 'doctrine_mongo_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (!$value || $value['value'] == false) {
return true;
}
$queryBuilder
->field('type')->equals('fds');
return true;
},
'field_type' => 'checkbox'
))
Taken this approach, I think I'd have to go ahead and query the whole dataset getting the distinct values for type and then loop around each constructing the filter. This would work but be horribly messy.
QUESTION
What is the "best practise" way of performing this without turning the code in to a ruddy mess? Putting the query in a repository will still go and create a similar effect?
Thanks for reading
I'm posting the answer for anyone else who may be facing the same issue. I realised I was approaching the situation from the wrong angle and instead of any of the above, did the following
/* Prepare the options to be used in the filter */
$tagRepo = $this->getConfigurationPool()->getContainer()->get('repository.tag');
$types = $tagRepo->findTypes();
$choices = array();
/* Create a simple choice compatible array */
foreach ($types as $type) { $choices[$type] = $type; }
/* In the actial filter itself */
$datagridMapper
->add('type', 'doctrine_mongo_choice', array(), 'choice',
array('choices' => $choices))

Symfony2 form type nested too many queries

I am developing a webshop system and currently I am working at the admin tools. I've got 4 related entities
Articles
stores main article data (name, description)
ArticleSuppliers
stores variants data (articleNumber, price..)
ArticleAttributesValues
stores attributes for each variant (value e.g. red, 40cm)
ArticleAttributes
stores names of attributes (color, height...)
Since it's much easier to edit a product, I would like to merge the forms together which is working.
ArticlesType binds ArticleSuppliersType binds ArticleAttributesValuesType
My FormType: ArticleAttributesValues contains an entity choice of ArticleAttributes
This is working! But there is a huge problem. I display each variant with their attributes so there is a query for each attribute (imagine a product with 20 variants and 10 attributes).
The solution would be easy: I just need to give an array of attributeNames + id to my FormType, but I do not know how this is done.
I would be grateful for every other solution though.
Thank you in advance!
EDIT:
I will try to explain my problem with code:
// controller
$article = $em->getRepository('MyBundle:Articles')->find($id);
$form = $this->createForm(new ArticleType(), $article);
This is my article type:
// articleType
$builder->add('shortName', 'text',
array('label' => false))
->add('shortDescription', 'text',
array('label' => false))
->add('longDescription', 'textarea',
array('label' => false))
->add('variants', 'collection', array('type' => new VariantsType()))
->add('save', 'submit', array('label' => 'Save'));
This relates to VariantsType:
// variantsType
$builder->add('supplierArticleNumber', 'text',
array('label' => false))
->add('price', 'text',
array('label' => false))
->add('variantvalues', 'collection', array('type' => new VariantsvaluesType()));
This relates to VariantsvaluesType, where my choice field is.
// variantsvaluesType
$builder->add('attributeValue', 'text',
array('label' => false))
->add('attributeUnit', 'text',
array('label' => false, 'required' => false))
->add('attrName', 'entity', array(
'class' => 'MyBundle:ArticleAttributes',
'property' => 'attributeName',
));
This choice field is the same (of course there are changes sometimes), so it would be unnecessary to query it X-times...
My idea was to load all attributeNames in my controller and pass it via $options to variantsvaluesType, but this is not working...
I see, well maybe you can try the next idea. Create a service with a get function for each collection to load, then in the constructor of the service you can load all the list only one time. Then you can use that service wherever you needed. It must work like a singleton. The inconvenience is that all those list should be loaded all the time in memory, but nothing is for free. Will be something like this:
use Doctrine\ORM\EntityManager;
class CollectionsService
{
private $em;
private $collectionOne;
private $collectionTwo;
public function __construct(EntityManager $entityManager)
{
$this->em = $entityManager;
$this->collectionOne= $this->em->getRepository('AppBundle:CollectionOne')->findAll();
$this->collectionTwo= $this->em->getRepository('AppBundle:CollectionTwo')->findAll();
}
public function getCollectionOne(){
return $this->collectionOne;
}
public function getCollectionTwo(){
return $this->collectionTwo;
}
}
Also must work something in the functions like next one , and don't be necessary do the load in the constructor.
public function getCollectionOne(){
if($this->collectionOne == null){
$this->collectionOne= $this->em->getRepository('AppBundle:CollectionOne')->findAll();
}
return $this->collectionOne;
}
Then expose the class as a service in services.yml
parameters:
collection.controller: AppBundle\Services\CollectionsService
services:
collections.service:
class: "%collection.controller%"
arguments:
entityManager: "#doctrine.orm.entity_manager"
And finally just use the service in the controller or the form to update the data $options['data'].
$collectionOne = $this->get('collections.service')->getCollectionOne();
I hope this help you.

Add Entity field type to form with event subscriber class

I am doing something very similar to this cookbook example http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html#adding-an-event-subscriber-to-a-form-class
The main difference is that my field type is an entity and not a text type.
So my field subscriber preSetData method looks like this:
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
if(!$data->getIsCategorized()){
$form->add(
$this->factory->createNamed('categories', 'entity', array(
'class' => 'My\PostBundle\Entity\Category',
'property' => 'name',
'multiple' => true,
'label' => 'Category'
)
)
);
}
}
This is giving the following error
Class does not exist
500 Internal Server Error - ReflectionException
If I add the entity directly in my form type with $builder->add('categories, 'entity', array(... it works fine
Is it possible to attach an entity field type to a form using a field event subscriber in this fashion?
I ran into the same problem, and actually it's because the factory->createNamed() method has more argument than the builder->add
The third argument isn't the options array, but a "data" argument.
So here's what you should do :
$form->add(
$this->factory->createNamed('categories', 'entity', null, array(
'class' => 'My\PostBundle\Entity\Category',
'property' => 'name',
'multiple' => true,
'label' => 'Category'
)
)
);
(add null before the options array)
Whether you attach a field in the type or by means of an event listener/subscriber should make no difference. Either you have a small mistake somewhere (likely), or that's a bug, in which case you should submit it to the issue tracker.

Symfony2 Setting a default choice field selection

I am creating a form in the following manner:
$form = $this->createFormBuilder($breed)
->add('species', 'entity', array(
'class' => 'BFPEduBundle:Item',
'property' => 'name',
'query_builder' => function(ItemRepository $er){
return $er->createQueryBuilder('i')
->where("i.type = 'species'")
->orderBy('i.name', 'ASC');
}))
->add('breed', 'text', array('required'=>true))
->add('size', 'textarea', array('required' => false))
->getForm()
How can I set a default value for the species listbox?
Thank you for your response, I apologise, I think I should rephrase my question. Once I have a value that I retrieve from the model, how do I set that value as SELECTED="yes" for the corresponding value in the species choice list?
So, that select option output from the TWIG view would appear like so:
<option value="174" selected="yes">Dog</option>
You can define the default value from the 'data' attribute. This is part of the Abstract "field" type (http://symfony.com/doc/2.0/reference/forms/types/field.html)
$form = $this->createFormBuilder()
->add('status', 'choice', array(
'choices' => array(
0 => 'Published',
1 => 'Draft'
),
'data' => 1
))
->getForm();
In this example, 'Draft' would be set as the default selected value.
If you use Cristian's solution, you'll need to inject the EntityManager into your FormType class. Here is a simplified example:
class EntityType extends AbstractType{
public function __construct($em) {
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('MyEntity', 'entity', array(
'class' => 'AcmeDemoBundle:Entity',
'property' => 'name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e')
->orderBy('e.name', 'ASC');
},
'data' => $this->em->getReference("AcmeDemoBundle:Entity", 3)
));
}
}
And your controller:
// ...
$form = $this->createForm(new EntityType($this->getDoctrine()->getManager()), $entity);
// ...
From Doctrine Docs:
The method EntityManager#getReference($entityName, $identifier) lets you obtain a reference to an entity for which the identifier is known, without loading that entity from the database. This is useful, for example, as a performance enhancement, when you want to establish an association to an entity for which you have the identifier.
the solution: for type entity use option "data" but value is a object. ie:
$em = $this->getDoctrine()->getEntityManager();
->add('sucursal', 'entity', array
(
'class' => 'TestGeneralBundle:Sucursal',
'property'=>'descripcion',
'label' => 'Sucursal',
'required' => false,
'data'=>$em->getReference("TestGeneralBundle:Sucursal",3)
))
I think you should simply use $breed->setSpecies($species), for instance in my form I have:
$m = new Member();
$m->setBirthDate(new \DateTime);
$form = $this->createForm(new MemberType, $m);
and that sets my default selection to the current date. Should work the same way for external entities...
If you want to pass in an array of Doctrine entities, try something like this (Symfony 3.0+):
protected $entities;
protected $selectedEntities;
public function __construct($entities = null, $selectedEntities = null)
{
$this->entities = $entities;
$this->selectedEntities = $selectedEntities;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('entities', 'entity', [
'class' => 'MyBundle:MyEntity',
'choices' => $this->entities,
'property' => 'id',
'multiple' => true,
'expanded' => true,
'data' => $this->selectedEntities,
]);
}
I don't think you should use the data option, because this does more than just setting a default value.
You're also overriding any data that's being passed to the form during creation. So basically, you're breaking
support for that feature. - Which might not matter when you're letting the user create data, but does matter when you
want to (someday) use the form for updating data.
See http://symfony.com/doc/current/reference/forms/types/choice.html#data
I believe it would be better to pass any default data during form creation. In the controller.
For example, you can pass in a class and define the default value in your class itself.
(when using the default Symfony\Bundle\FrameworkBundle\Controller\Controller)
$form = $this->createForm(AnimalType::class, [
'species' => 174 // this id might be substituted by an entity
]);
Or when using objects:
$dog = new Dog();
$dog->setSpecies(174); // this id might be substituted by an entity
$form = $this->createForm(AnimalType::class, $dog);
Even better when using a factory:
(where dog probably extends from animal)
$form = $this->createForm(AnimalType::class, DogFactory::create());
This will enable you to separate form structure and content from each other and make
your form reusable in more situations.
Or, use the preferred_choices option, but this has the side effect of moving the default option to the top of your form.
See: http://symfony.com/doc/current/reference/forms/types/choice.html#preferred-choices
$builder->add(
'species',
'entity',
[
'class' => 'BFPEduBundle:Item',
'property' => 'name',
'query_builder' => ...,
'preferred_choices' => [174] // this id might be substituted by an entity
]
);
I'm not sure what you are doing wrong here, when I build a form using form classes Symfony takes care of selecting the correct option in the list. Here's an example of one of my forms that works.
In the controller for the edit action:
$entity = $em->getRepository('FooBarBundle:CampaignEntity')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find CampaignEntity entity.');
}
$editForm = $this->createForm(new CampaignEntityType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('FooBarBundle:CampaignEntity:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
The campaign entity type class (src: Foo\BarBundle\Form\CampaignEntityType.php):
namespace Foo\BarBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;
class CampaignEntityType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('store', 'entity', array('class'=>'FooBarBundle:Store', 'property'=>'name', 'em'=>'my_non_default_em','required' => true, 'query_builder' => function(EntityRepository $er) {return $er->createQueryBuilder('s')->orderBy('s.name', 'ASC');}))
->add('reward');
}
public function getName()
{
return 'foo_barbundle_campaignentitytype';
}
}
From the docs:
public Form createNamed(string|FormTypeInterface $type, string $name, mixed $data = null, array $options = array())
mixed $data = null is the default options. So for example I have a field called status and I implemented it as so:
$default = array('Status' => 'pending');
$filter_form = $this->get('form.factory')->createNamedBuilder('filter', 'form', $default)
->add('Status', 'choice', array(
'choices' => array(
'' => 'Please Select...',
'rejected' => 'Rejected',
'incomplete' => 'Incomplete',
'pending' => 'Pending',
'approved' => 'Approved',
'validated' => 'Validated',
'processed' => 'Processed'
)
))->getForm();
Setting default choice for symfony2 radio button
$builder->add('range_options', 'choice', array(
'choices' => array('day'=>'Day', 'week'=>'Week', 'month'=>'Month'),
'data'=>'day', //set default value
'required'=>true,
'empty_data'=>null,
'multiple'=>false,
'expanded'=> true
))
The form should map the species->id value automatically to the selected entity select field. For example if your have a Breed entity that has a OnetoOne relationship with a Species entity in a join table called 'breed_species':
class Breed{
private $species;
/**
* #ORM\OneToOne(targetEntity="BreedSpecies", mappedBy="breed")
*/
private $breedSpecies;
public function getSpecies(){
return $breedSpecies->getSpecies();
}
private function getBreedSpecies(){
return $this->$breedSpecies;
}
}
The field 'species' in the form class should pick up the species->id value from the 'species' attribute object in the Breed class passed to the form.
Alternatively, you can explicitly set the value by explicitly passing the species entity into the form using SetData():
$breedForm = $this->createForm( new BreedForm(), $breed );
$species = $breed->getBreedSpecies()->getSpecies();
$breedForm->get('species')->setData( $species );
return $this->render( 'AcmeBundle:Computer:edit.html.twig'
, array( 'breed' => $breed
, 'breedForm' => $breedForm->createView()
)
);
You can either define the right default value into the model you want to edit with this form or you can specify an empty_data option so your code become:
$form = $this
->createFormBuilder($breed)
->add(
'species',
'entity',
array(
'class' => 'BFPEduBundle:Item',
'property' => 'name',
'empty_data' => 123,
'query_builder' => function(ItemRepository $er) {
return $er
->createQueryBuilder('i')
->where("i.type = 'species'")
->orderBy('i.name', 'ASC')
;
}
)
)
->add('breed', 'text', array('required'=>true))
->add('size', 'textarea', array('required' => false))
->getForm()
;
You can use "preferred_choices" and "push" the name you want to select to the top of the list. Then it will be selected by default.
'preferred_choices' => array(1), //1 is item number
entity Field Type