How to order related propel model in symfony2 form - symfony

I am using symfony2 form along with propel. I am having trouble on how to order the related entity from propel's model.
The abstracted code below, is taken from http://www.propelorm.org/cookbook/symfony2/mastering-symfony2-forms-with-propel.html, which show how to relate them together. I am wondering how do I order them.
$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
));
For Example, If in my author table there are columns name, age,.... How should make it to order by name.

$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
'query' => \Acme\LibraryBundle\Mode\AuthorQuery::create()->orderByName()
));

Related

WP_Query - Inherit meta_query and tax_query value from associated post if value doesn't exists or it's empty

I have two custom post types: venue and event.
Lets say that both post types have the _location meta key (custom field) and both of them also have music-genre taxonomy.
Event also has _association meta key which contains ID of the associated venue.
Now Querying events by mentioned taxonomy and meta field is very easily done like this:
$query = new WP_Query([
'post_type' => 'event',
'tax_query' => [
[
'taxonomy' => 'music-genre',
'field' => 'slug',
'terms' => ['house', 'jazz', 'rock']
]
],
'meta_query' => [
[
'key' => '_location',
'value' => ['Miami', 'Ibiza', 'Zrce'],
'compare' => 'IN'
]
]
]);
$posts = $query->posts;
But I want to also get events with empty '_location' field IF his parent/associated post (ID stored under _association meta key) has expected value. Same thing for taxonomies - Music Genre in this example. If event doesn't have associated "Jazz" music genre, but his associated post has it then it should be returned by WP_Query.
So basically I want to inherit meta values and taxonomies from associated post if they doesn't exists or are empty.
I hope you can understand what I want to achieve well... otherwise please ask for further explanation.
In case anyone is wondering im using Carbon Fields library for custom fields.
Also my project is much more complicated, but everything was simplified for this question, so performance matters a lot.
Trying to understand... are you looking for this 'hide_empty' => false?

symfony2 FromBuilder multiple checkboxes from entity instead of using "choice"

How can I get multiple Cehckboxes instead a choice field in Symfony? Actually, I'm using:
->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true))
This will output a select-Field..
In this case, it would be better to output checkboxes as it is easier to handle for the user.
http://symfony.com/doc/current/reference/forms/types/checkbox.html at, there is nothing helpful about multiple select....
Do I have to build an array and place it by myself?
Use the expanded option for choice and entity fields.
->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true, 'expanded' => true,))

ManyToMany nullable not allowed

I have a scenario like this:
tracks ManyToMany instruments
I create the form in this way:
$builder
->add('instruments', 'entity_hidden', array(
'attr' => array('class' => 'one_req'),
'label' => 'form.instrument',
'translation_domain' => 'label',
'required' => false,
'class' => 'Acme\DemoBundle\Entity\Instrument'
))
"hidden_entity" is given by a custom transformer that you can find here: gist
although I believe that is not the problem.
The problem is that the user may not even choose any instrument (such asamong other specified with "required" => false).
When I go to save my returns this error:
Neither the property 'instruments' nor one of the methods 'setInstruments()',
'__set()' or '__call()' exist and have public access in
class 'Acme\DemoBundle\Entity\SoundtrackVersion'.
I do not understand why ..
Where am I doing wrong?
It sounds very obvious, but error can not lie. Check that Acme\DemoBundle\Entity\SoundtrackVersion entity has property instruments and methods setInstruments() and getInstruments().

How to validate two instances of the same entity?

Use case
I am learning Symfony2 and am creating a Table Tennis tracking app to learn the framework. I have configured my entities as follows.
Player 1..n Result n..1 Match
On my form I'd like to validate that the scores for a match are correct.
Implementation
Match has an ArrayCollection() of results.
My MatchType and ResultType forms contain the following.
// Form\MatchType
$builder->add('matchType', 'entity', array(
'class' => 'PingPongMatchesBundle:MatchType',
'property' => 'name',
)
)
->add('results', 'collection', array(
'type' => new ResultType(),
'allow_add' => true,
'by_reference' => false,
)
)
->add('notes');
// Form\ResultType
$builder->add('player', 'entity', array(
'class' => 'PingPongPlayerBundle:Player',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.firstName', 'ASC');
},
))
->add('score');
Issue
I need to be able to validate the scores. However I'm not sure how to approach this type of validation as I need to compare two instances of my Result#score in order to know if they are valid or not.
Is anyone able to suggest a method or approach that I could use in order to be able to compare Result#score between the two different instances? Can I validate the ArrayCollection in the Match entity for example?
You could creat a custom validator constraint on Match entity.
http://symfony.com/doc/2.0/cookbook/validation/custom_constraint.html
Take a look in Callback constraint:
http://symfony.com/doc/2.1/reference/constraints/Callback.html

WordPress get user by meta data

How can I retrieve all users registered in my WordPress blog having a particular meta data?
For example I have made an option to add a custom meta data for every registering users having meta key as parent_id. If I want to list all users having parent_id as 2 , then how can I do this?
Since WP v3.1 it's ridiculously easy to search for a user by his/her meta key.
Use the function
get_users($args)
(WP Documentation)
The function takes an array of parameters, in your case you need
get_users(array(
'meta_key' => 'parent_id',
'meta_value' => '42'
))
Simple way how to get one user by his metadata is:
$user = reset(
get_users(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'number' => 1
)
)
);
Here is how you can get users based on a custom role and multiple metadata keys,
$available_drivers = get_users(
array(
'role' => 'driver',
'meta_query' => array(
array(
'key' => 'approved',
'value' => true,
'compare' => '=='
),
array(
'key' => 'available',
'value' => true,
'compare' => '=='
)
)
)
);
Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.
If you have additional metadata to test, add another array element to the meta_query array.
Meanwhile checkout my open source at github.com/patrickingle
Here is the codex page from Wordpress detailing how to use the get_users($arg); function.
It contains examples how how to build custom functions to fetch various parts of user data. You'll have to naturally build and make some of your own changes to get it how you want.
Additionally here is a link to a function somebody built that will fetch user data based on roles within wordpress. You can configure it in many different ways with some tweeking, but this will allow you to filter your results in a more powerful manner.

Resources