I have 3 tables:
Post -> Type -> Category
I need to get the category on the Post entity passing per Type to use this on a filter form
This is possible?
like a join and subjoin
If I understand correctly, you want to be able to filter Post's by Category.
Like with any other field you wish to filter by, you have to add a Form to the filter's FormBuilder. The problem in this case is that the Entity bound to the form doesn't have the property category. It's its property type who does.
Thus, you need to tell the Form how to access the right property. This is achieved by using the property_path option. Here's the documentation for it.
You would do something like this in your filter's Type:
$builder
->add('category', 'entity', array(
'label' => 'Category',
'data_class' => 'Category',
'property_path' => 'type.category',
))
;
The property_path option is very powerful. It will accept any path that the PropertyAccess component does. Read its documentation here.
Multiple joins are possible in doctrine. Please, read this section in doctrine documentation.
Related
I am trying to set the label of an embedded form type (collection type).
These labels have to be different for the different collections.
For example I have two collections and an array with the labels ['label1','label2'] in die form type.
I think I need the index of the collection iteration to get the right label.
Like below i need the index for the entry_options label to get the right value of the $labels array.
Thats my buildForm Method
public function buildForm(FormBuilderInterface $builder, array $options)
{
$labels = ['label1', 'label2'];
$builder
->add('otherForms', CollectionType::class, [
'entry_type' => otherFormType::class,
'entry_options' => ['label' => $labels[$index]],
])
...
;
}
After reading your comments, I must agree that translation bundles are sometimes heavy weight and quite inconvenient.
The CollectionType usually ignores the index/key of collection items given to it, though.
Now I see two scenarios: you want to give the labels as an option to your form or the form shall be dependent on the data (using form events). Your code so far suggests, that languages are set globally and will not be added on the fly in some other manner.
In the first scenario it's very simple (see below), the second scenario requires some event listener in your form, but apart from that, it's quite similar.
The essential idea is to just create the forms you need (optionally inside the pre-submit handler), since you apparently already know which forms you need from the label array:
foreach($labels as $index => $label) {
$builder->add('otherForms'.$index, otherFormType::class, [
'label' => $label,
'property_path' => 'otherForms['.$index.']',
]);
}
The use of property_path will help you access the proper elements (however, changing the language (= key) of a translation might be difficult, but that should not happen, usually. And even if it happens, only some copy-pasta is necessary).
Your otherForms keys probably will be differently, but I guess you'll figure it out.
I have 2 related entities, e.g. Book and Publisher (Book has one publisher, publisher has many books).
When editing\adding a Book I want to present a select of the Publishers.
Publishers has a property 'isDefault' on of the Publisher records will be marked as isDefault TRUE.
How do I make use of this in my add/edit form to pre-select the default Publisher?
I would recommend injecting publisherRepository as a service into your form.
And then declare a field something like this:
$builder->add('publishers', 'choice', array(
'choices' => $this->publisherRepository->findAll(),
'data' => $this->publisherRepository->findOneBy(['isDefault' => true]),
));
This is an existing schema I'm working with and I'm trying to not make any changes for the time being. I have an entity property that represents a university semester, like "fall12", "spring11", etc.
When adding or editing this entity with a form, I want to split that property into two form fields: "Season" (fall or spring") and "Year" (2011, 2012, etc):
...
->add('formSemesterSeason', 'choice', array(
'label' => 'Season',
'mapped' => false,
'choices' => array('fall' => 'Fall', 'spring' => 'Spring'),
))
->add('formSemesterYear', 'choice', array(
'label' => 'Year',
'mapped' => false,
'choices' => $this->courseManager->getYearChoices(),
))
...
When submitting the form, the values need to be combined and saved to the "semester" property on the entity as a string
When viewing the edit form, the existing "semester" value needs to be split between the two.
I don't think data transformers help here, since they apply to transforming just one form item.
Right now I'm using a form event POST_SET_DATA to fill out the two form fields when editing an existing entity:
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($course_manager) {
$course = $event->getData();
$form = $event->getForm();
$semester = $course->getSemester();
$parsed_semester = $course_manager->parseSemesterMachineName($semester);
$form->get('formSemesterSeason')->setData($parsed_semester->season);
$form->get('formSemesterYear')->setData($parsed_semester->yearFull);
});
This works well, but how do I then combine the values back after the form has been submitted? I can do it easily in the controller, but I think I should be able to use form events, and have the data manipulation take place before form validation.
You can combine them back in a POST_SUBMIT listener.
The best way (reuseable) would be to create your own custom form type with a data transformer to split/combine the fields internally.
There are "recipes" in the cookbook but the best way that I found to create it was to rip apart the DateTime field type and associated transformers (DataTransformerChain, DateTimeToArrayTransformer & ArrayToPartsTransformer) for parts and build my own from that.
I want to pass a parameter value to the route with the content of the current cell.
$listMapper->add("parent_id", 'url', array(
'route' => array(
'name'=>'admin_xxxxx_news_news_show',
'absolute'=>true,
'identifier_parameter_name'=>"parentID"
)
);
I have a route admin_xxxxx_news_news_show and have to pass the parameter from the current column. I don't have a relation of the current field with the related entity. I am using two columns for association, one for the entity and second for that entity ID. With my current knowledge of Symfony2 I need to have a customized view.
I have a problem, I want to create a simple search form with a filter assembly. These filters are attributes that belong to attribute groups.
group 1
[] Attribute 1
[] Attribute 2
[] Attribute 3
group 2
[] Attribute 1
[] Attribute 2
[] Attribute 3
But the problem is that I can not do (graphic aspect)
$builder->add('attribut', 'entity', array(
'class' => 'RestoFrontBundle:Attribut',
'group_by' => 'groupeAttribut.id',
'expanded' => true,
'multiple' => true,
'query_builder' => function(AttributRepository $er) {
return $er->createQueryBuilder('a')
->join("a.groupeAttribut", 'g')
->where("a.statut = 1");
}
))
->getForm();
Also I can not manage the game if the checkbox has been checked.
You note that the graphic aspect is the hard part. That is due to the way checkboxes are used in HTML. Unlike select inputs there is no notion of an optgroup. The closest analog for checkboxes would be a fieldset with a legend.
You may want explore using a Choice Field type rather than an entity type. Provide your choices via some provider function within which you format the options array(s) whether or not you retrieved them from a database. ( For the record, that's exactly how I populate the select at http://stayattache.com which has multiple places to retrieve options from. ) You may even want to explore creating your own form field type and template to format the output. Checkout the documentation on creating your own field type.
I hope that helps a bit. There are probably plenty of other ways to approach this as well.