Group checkboxes for entities in a Symfony form - symfony

How can I group checkboxes for entities in Symfony forms?
->add(
'products',
EntityType::class,
[
'class' => Product::class,
'choice_label' => 'titel',
'multiple' => true,
'expanded' => true,
]
)
Im my domain model a Product belongs to exactly one ProductFamily. Every ProductFamily belongs to exactly one ProductSuperFamily. In my form the Product checkboxes should be grouped like this:
Product Super Family 1
Product Family 1
[ ] Product 1
[ ] Product 2
Product Family 2
[ ] Product 3
Product Family 3
[ ] Product 4
[ ] Product 5
Product Super Family 2
Product Family 4
[ ] Product 6
[ ] Product 7
[ ] Product 8
Product Family 5
[ ] Product 9
Product Super Family 3
Product Family 6
[ ] Product 10
[ ] Product 11
How can I achive this?

It is allowed by the choice type, you must use the group_by option:
'group_by' => function(Product $product) {
return $product->getFamily()->getLabel()
},

Related

Count results for group by query

I have a complex query which group bys multiple columns, this is an example
$queryBuilder
->join('e.something', 's')
->where('...')
->addGroupBy('e.id s.someField');
Using ->getQuery()->getResults() returns me an array of entities like I'd expect.
[
0 => App\Entity\ExampleEntity,
1 => App\Entity\ExampleEntity
]
If I try to count the results returned using a select like so:
$queryBuilder
->select('COUNT(e.id)')
->join('e.something', 's')
->where('...')
->addGroupBy('e.id s.someField');
I am returned an array of arrays, inside each array is the count. This isn't what I want. Removing the group by I'm given the correct result however the group by is required.
[
[
1 => '11',
],
[
1 => '4',
]
]
I'm stuck on how I can count the results of the group by. I have tried using distinct and I also do not want to count in PHP.

How to sort a dictionary according to it's keys in Julia?

I want to sort the following dictionary in Julia in way that the key values are printed in alphabetical order of the key names.
fruits = Dict("Mangoes" => 5, "Pomegranates" => 4, "Apples" => 8);
On executing the above code and entering fruits, I get the output as:
fruits
Dict{String,Int64} with 3 entries:
"Pomegranates" => 4
"Apples" => 8
"Mangoes" => 5
However, I expect the following result:
Apples => 8
Mangoes => 5
Pomegranates => 4
This is because dictionaries in Julia (Dict) are not ordered: each dictionary maintains a set of keys. The order in which one gets keys when one iterates on this set is not defined, and can vary as one inserts new entries. There are two things that one can do to ensure that one iterates on dictionary entries in a specific order.
The first method is to get the set of keys (using keys) and sort it yourself, as has been proposed in another answer:
julia> fruits = Dict("Mangoes" => 5, "Pomegranates" => 4, "Apples" => 8);
julia> for key in sort!(collect(keys(fruits)))
val = fruits[key]
println("$key => $val")
end
Apples => 8
Mangoes => 5
Pomegranates => 4
That being said, if the order of keys is important, one might want to reflect that fact in the type system by using an ordered dictionary (OrderedDict), which is a data structure in which the order of entries is meaningful. More precisely, an OrderedDict preserves the order in which its entries have been inserted.
One can either create an OrderedDict from scratch, taking care of inserting keys in order, and the order will be preserved. Or one can create an OrderedDict from an existing Dict simply using sort, which will sort entries in ascending order of their key:
julia> using OrderedCollections
julia> fruits = Dict("Mangoes" => 5, "Pomegranates" => 4, "Apples" => 8);
julia> ordered_fruits = sort(fruits)
OrderedDict{String,Int64} with 3 entries:
"Apples" => 8
"Mangoes" => 5
"Pomegranates" => 4
julia> keys(ordered_fruits)
Base.KeySet for a OrderedDict{String,Int64} with 3 entries. Keys:
"Apples"
"Mangoes"
"Pomegranates"
Try this:
fruits = Dict("Mangoes" => 5, "Pomegranates" => 4, "Apples" => 8);
for key in sort(collect(keys(fruits)))
println("$key => $(fruits[key])")
end
It gives this result:
Apples => 8
Mangoes => 5
Pomegranates => 4

Fetch distinct rows without doubled property values on MySql DB with Symfony 3.4 and Doctrine

In a Symfony 3.4 Application I have an Entity with 4 properties plus the id.
It is managed by doctrine on a mySql DB.
Say the properties where named p1, p2, p3 and q.
An example DB table could be like this:
id p1 p2 p3 q
------------------
1 x y z 1
2 x y n 2
3 x z 1
4 x z 2
5 x y z 3
6 x y z 4
7 x n z 1
What I need to do is to request all entities from the DB that have different combinations of p1, p2, p3.
So using the given sample DB table the result I need would be
id p1 p2 p3 q
------------------
1 x y z 1
2 x y n 2
3 x z 1
7 x n z 1
So the rows with id's 4, 5 and 6 wouldn't be in the set because the have "doubled" combinations of p1, p2, p3.
Now - how can I do this using the methods on the Repository class of Symfony 3.4 like described here:
https://symfony.com/doc/3.4/doctrine.html
Or are there any other ways to achieve what I am looking for?
Any hints are very welcome.
EDIT:
Basically I am looking for a list of all existing combinations of p1, p2, p3 without having a combination doubled in the list. It doesn't matter which row is returned in the result set (regarding the properties id and q) as long as one (and only one) row of each combination of p1, p2, p3 is included.
A "group by" subquery to eliminate duplicated data, and a main query to search from ids:
$dql = 'SELECT r FROM myTable r
WHERE r.id IN (
SELECT min(s.id) FROM myTable s
GROUP BY s.p1, s.p2, s.p3
)';
$rows = $em->createQuery($dql)
->getResult();
If you only want the differents uniques combination of p1, p2 and p3 and don't care about id and q, you can exclude them from your query and use a distinct clause.
There is no built-in method of the Repository class for your need, you might want to create a custom repository class (I personnally do it every time I have to write custom queries for my entities)
Let's consider your entity (located in example in src/AppBundle/Entity/MyEntity.php) declared such way :
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="AppBundle\Repository\MyEntityRepository")
*/
class MyEntity
{
//declarations of $id, $p1 and so on...
}
You can create a custom repository class (in example src/AppBundle/Repository/MyEntityRepository.php) with a method to get the needed result :
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class MyEntityRepository extends EntityRepository
{
public function findAllUniqueCombinations()
{
$result = $this->createQueryBuilder('m')
->select('m.p1 p1', 'm.p2 p2', 'm.p3 p3')
->distinct()
->getQuery()
->getResult();
return ($result);
}
}
And, in some controller action :
$MyCombinationList = $this->getDoctrine()
->getManager()
->getRepository(MyEntity::class)
->findAllUniqueCombinations();
I used quite the same query for testing (only the select() changed)
Input datas
id | name | firstname
---+------------+----------
1 | Smith | John
2 | Smith | John
3 | Smith | John
4 | Smith | John
5 | Doe | Jane
6 | Connor | Sarah
7 | The Sponge | Bob
8 | Marley | Bob
The select was ->select('c.name name', 'c.firstname firstname')
The output var_dump($result); gave :
array (size=5)
0 =>
array (size=2)
'name' => string 'Smith' (length=5)
'firstname' => string 'John' (length=4)
1 =>
array (size=2)
'name' => string 'Doe' (length=3)
'firstname' => string 'Jane' (length=4)
2 =>
array (size=2)
'name' => string 'Connor' (length=6)
'firstname' => string 'Sarah' (length=5)
3 =>
array (size=2)
'name' => string 'The Sponge' (length=9)
'firstname' => string 'Bob' (length=3)
4 =>
array (size=2)
'name' => string 'Marley' (length=6)
'firstname' => string 'Bob' (length=3)

WP_Query orderby multiple parameters not working

I am trying to sort by my WP_Query results by two meta fields, first date and then time. So effectively
Date A 17:00
Date B 18:00
Date C 07:00
Date B 10:00
Date A 9:00
would be come
Date A 9:00
Date A 17:00
Date B 10:00
Date B 18:00
Date C 07:00
This is my current code:
"post_type" => "event",
"post_status" => "publish",
"posts_per_page" => 25,
"ignore_sticky_posts" => 0,
"show_sticky" => true,
"meta_query" => array(
array(
"key" => "_meta_event_start_date",
"compare" => ">=",
"value" => 0
),
array(
"key" => "_meta_event_start_time",
"compare" => ">=",
"value" => 0
),
),
"order" => "ASC",
"orderby" => "_meta_event_start_date _meta_event_start_time"
It is currently sorting my results appropriately by the date, but not the time (time format is stored hh:mm (24hr)). How might I amend the query to make this work?
I have seen other responses recommending using foreach loops, sql queries or even filters. I can't use these unfortunately as the site I'm working on uses the search plugin Facet WP, and as search requires WP_Query to query the database and output content.
Thanks in advance.
As I see you are trying to order by meta keys - you have to use in this case a quite more complicated syntax according to changes in WordPress 4.2 - something like below:
"post_type" => "event",
"post_status" => "publish",
"posts_per_page" => 25,
"ignore_sticky_posts" => 0,
"show_sticky" => true,
"meta_query" => array(
"event_start_date" => array(
"key" => "_meta_event_start_date",
"compare" => ">=",
"value" => 0
),
"event_start_time" => array(
"key" => "_meta_event_start_time",
"compare" => ">=",
"value" => 0
)
),
"orderby" => array(
"event_start_date" => "ASC",
"event_start_time" => "ASC"
)
Additionally - for single meta_key ordering it is worth to remember that you have to use in the orderby clause values meta_value and meta_value_num

Having an issue doing a WP_Query with post_content and category__and

This is the array for my arguments for the WP_Query.
Array
(
[showposts] => 4
[paged] => 1
[post_type] => post
[post_content] => tree
[category__and] => Array
(
[0] => 6
[1] => 15
[2] => 28
)
)
I want to return the posts where they are in the categories 6, 15, 28 and where the post_content has the word tree.
My problem is that I'm returning several duplicated results for the posts that have the word tree in it. Ideally I would like to return one.
anyone have any idea how I can fix or improve this?
I found the issue, I shouldn't have been trying to filter on post_content, but rather 's'
e.g.
Array
(
[showposts] => 4
[paged] => 1
[s] => post
[post_content] => tree
[category__and] => Array
(
[0] => 6
[1] => 15
[2] => 28
)
)

Resources