So I have a query that gets distance to a site. The query returns the entities sites correctly, and it calculates the distance. This is my query.
$query = $this->getEntityManager()->createQuery('SELECT s, ROUND( ( :milesOrKm * acos( cos( radians(:myLat) ) * cos( radians( s.latitude ) ) * cos( radians( s.longitude ) - radians(:myLong) ) + sin( radians(:myLat) ) * sin( radians( s.latitude ) ) ) )) AS distance FROM AppBundle:Site s ORDER By distance ASC')->setParameter('milesOrKm', $units)
When the results come in, they are an array with my site, but also a distance. This is close to what I need, but I want the distance field added to my entity even though it's not a stored doctrine field.
array:2 [▼
0 => array:2 [▼
0 => Site {#512 ▶}
"distance" => "1"
]
1 => array:2 [▼
0 => Site {#521 ▶}
"distance" => "2649"
]
]
Basically I want
array:2 [▼
0 => Site {#512 ▶}
1 => Site {#521 ▶}
]
How do I add the distance to my entity? I have a distance protected property (with no annotation so it's not loaded and saved).
I suspect I need a custom hydration, but I'm having trouble figuring that out. Any suggestions?
What you need is your own custom hydrator.
Here is good example of how to do it
Related
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 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()
},
Table1(identity1, a1,b1)
Table2(identity, foreign(identity1),foreign(identity1))
Now when I use this query
$query = $qb->select('a1', ' b1')
->from('table2', 'm1')
->join('ApiMapBundle:tabl1, 'u1', 'WITH', $qb->expr()->orX('m1. foreign(a1) =u1.identity', 'm1. foreign(b1) = u1.identity '))
->andWhere('m1.identity=:tt')
->setParameter('tt', $cn)
->getQuery()
->getResult();
Now the problem with this query is that sometimes it gives id let's tt:5 so it gives me a value like this
Array(
0 => Array(id => 1, b1=> 8000225),
1 => Array(id => 9, b1 => 8000234)) given).
Basically in table the values structure is like this
Table2(Identity=5,foreign1=9,foreign2=1)
Any idea that how can I exactly get the given structure? Because in some cases it is fine they give me proper foreign 1 and foreign 2 but in other cases it make it alternative. Any idea?
I'm hoping someone can help with a problem I can't wrap my limited knowledge around. I have two SQL queries like:
SELECT
SUM(Matrix.[ITEM1]) AS [ITEM1],
SUM(Matrix.[ITEM2]) AS [ITEM2]
FROM StoreList
INNER JOIN Matrix ON StoreList.[Store Number] = Matrix.[Store Number]
WHERE (StoreList.Region = '#Value')
SELECT
ITEMTYPES.type_description,
ITEMS.Quantity
FROM ITEMS
INNER JOIN ITEMTYPES ON ITEMS.type_id = ITEMTYPES.id
Now, what these do is return a list of values that I want to multiply together (for example, to get a final quantity for [ITEM1], multiply [ITEM1] by [Items.Quantity]. However the ITEMTYPES table holds a list of descriptions of ITEM (and primary key id), which is used for entering new items. The STORELIST table uses the descriptors in ITEMTYPES as column names(ie. The ItemTypes table has records called [Item1], [Item2]; the Column names in Matrix are also [Item1], [Item2]).
The question is this - from my limited experience (and I do mean limited - < 6months with SQL) there is no way to perform a calculation based upon column names - I would have to abstract out the totals in some way to another table, with additional fields for Region (this explains the sum() statements in query 1) and perform calculations that way - or does some have a more elegant idea?
Table Structures as per Laurence's request:
For Table [MATRIX]:
[Store Number] (PK, int, not null)
[ITEM1] (int, default 0)
[ITEM2] (int, default 0)
For Table [ITEMTYPES]:
[id] (PK, int, not null),
[ITEMTYPE] (nvarchar(50), not null)
Now, Example data (excuse any formatting issues!):
[MATRIX]
Store Number ITEM1 ITEM2
------------------------------
123456 1 15
678920 31 9
[ITEMTYPE]
id ITEMTYPE
------------------------------
1 ITEM1
2 ITEM2
Appreciate the response!
Ok so you have 2 results returned from these 2 queries into 2 different ordered pair lists or arrays... so you have the first result in the format of
//FROM THE MATRIX QUERY
array(
[0] => array(
"Store Number" => 123456,
"ITEM1" => 1,
"ITEM2" => 15
),
[1] => array(
"Store Number" => 678920,
"ITEM1" => 31,
"ITEM2" => 9
),
ETC...
)
//FROM YOUR ITEM QUERY
array(
[0] => array(
"type_description" => "ITEM1",
"quantity" => ???
),
[1] => array(
"type_description" => "ITEM2",
"quantity" => ???
)
ETC......
)
ok now you can do 2 foreach loops where you iterate through the 2 arrays
foreach(arrayMatrix as store){
foreach(arrayItems as item){
print "Store: "+ store['Store Number']+" ITEM: " + item['type_description']+" total: "+ (store[item['type_description']] * item['quantity']);
}
}
now I wrote this in psuedo code and Im not sure if you can translate this well into VB. With those 2 foreach or while loops or whatever you can databind the results to the grid or put them in another array or list... I hope this helps at all.
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
)
)