Sonta Admin Bundle List Url Customization - symfony

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.

Related

Symfony2 - related entity contains 'isDefault' property how to use in related entity form

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]),
));

doctrine join column joined on other entity

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.

Using two entities to create a form

I use one general form type class to create all forms in my application. From controllers I only pass some parameters to it so that it knows which fields to render. For example:
$form = $this->createForm(new CommonType($repository, $queryResultSet), new UsersEntity(),
[
'action' => $this->generateUrl('user_edit'),
'attr' => ['class' => 'stdform'],
'repository_name' => 'AcmeBundle:Users'
]);
And then CommonType class generates fields based on arrays from repository class.
However I need to add two entities now - the form has to contain fields from user and shop repositories/entities.
I thought about creating two forms here and connecting them, but I wouldn't be able to persist it to the database later.
How can I solve this problem?
I can't use form embedding because I have CommonType class here in my case.

Symfony2: Using two form fields for one entity property?

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.

Wordpress custom field search

I want to filter posts with respect to custom fields added to a post.Now I added two custom fields city,zip for each post. I want to filter posts with respect to these two fields.
How can write a custom query for it.
In the where clause I wrote meta_key='City' and meta_value='myval'. It works and returns the post with custom field City and value 'myval'. But I want to check both City and Zip.How can I do that.
I believe you use meta_query for this - just going through an old project now, looks like meta_query can take in an array of filters:
array( 'posts_per_page' => 10,
'meta_query' => array(
array('key'=>'key', 'value'=>'value', 'compare'=>'='),
array('key'=>'key2', 'value'=>'value2', 'compare'=>'=')
)
)
Obviously completely untested IRL, but looks like it works from my end.

Resources