I have a ManyToOne relation between two entities ArticleLine and Artcile:
<?php
//ArticleLine.php
ManyToOne(targetEntity="Gain\Bundle\ArticleBundle\Entity\Article")
I'm trying to get the list of Ids based on some where conditions..
This my doctrine query dql:
SELECT tl.id FROM AppBundle\Entity\ArticleLine tl INNER JOIN tl.turnover t INNER JOIN tl.article a WHERE t.supplier = :supplier AND t.year = :year AND tl.significant = false ORDER BY tl.id asc
After calling $qb->getQuery()->getResult() I'm getting this result of arrays...
array:138 [
0 => array:1 [
"id" => 64624
]
1 => array:1 [
"id" => 64630
]
2 => array:1 [
"id" => 64631
]
3 => array:1 [
"id" => 64632
]
4 => array:1 [
"id" => 64633
]
5 => array:1 [
"id" => 64637
]
6 => array:1 [
"id" => 64638
Any idea how I can transform my result to a one dimensional array or playing on hydration mode to get something like this
[64624, 64630, 64631, 64633 ... ]
or
[0 => 64624, 1 => 64630, 2 => 64631 ...]
You can transform your result with an array_map function:
$result = $qb->getQuery()->getResult()
$result = array_map('current', $result);
You can also use array_column fucntion:
$result = array_column($result, "id")
If a solution exists with an hydration mode. I would like to know :)
Related
When using metric totalAdRevenue (admanager 360 link in GA4) the metric 'totalAdRevenue' is not giving any results. In GA4 Query explorer i do get the right data.
My Code:
$ar = [
'property' => 'properties/' . $PROP_ID,
'dateRanges' => [
new DateRange([
'start_date' => $date->format("Y-m-d"),
'end_date' => $date->format("Y-m-d"),
]),
],
'dimensions' => [new Dimension(
[
'name' => 'sessionCampaignName'
]
),
],
'metrics' => [new Metric(
[
'name' => 'sessions'
]
), new Metric(
[
'name' => 'totalAdRevenue'
]
)
]
];
$response = $analytics->runReport($ar);
I wrote a database selection like this (Laravel 8):
$users = collect(User::with(['roles','roles.permissions'])->find(21));
The result is the following
=> Illuminate\Support\Collection {#2051
all: [
"id" => 21,
"first_name" => "test",
"name" => "test",
"..." => "...",
"roles" => [
[
"id" => 9,
"name" => "Test",
"permissions" => [
[
"id" => 13,
"name" => "userReadList",
],
[
"id" => 11,
"name" => "userUpdate",
],
],
],
[
"id" => 4,
"name" => "responsible",
"permissions" => [
[
"id" => 10,
"name" => "userRead",
],
[
"id" => 9,
"name" => "userCreate",
],
],
],
],
],
}
Now: What I have to change in the query to get only an array, containing the permissions id as follow: [13,11,10,9]?
Or is my query principal wrong for getting the permissions id
I found the solution:
$userPermissions = collect(User::with(['roles','roles.permissions'])->find(21));
Arr::flatten(Arr::pluck($userPermissions['roles'] , 'permissions.*.id'));
//[13,11,1,4,]
But I think, the better way is the following
$userPermissions = User::with(['roles','roles.permissions'])->find(21);
$userPermissions->roles->pluck('permissions.*.id')->flatten()->toArray();
//[13,11,1,4,]
Also possible it the following approach
$userPermissions = User::with(['roles','roles.permissions'])->where('id',21)->get();
$userPermissions->pluck('roles.*.permissions.*.id')->flatten()->toArray();
//[13,11,1,4,]
The Image above is the structure of a part of my graph database.
What I'm trying to achieve here is that I'm trying to search for product vertices by using the name of the of their related detail vertices.
The Expected output is a list of products with their details that has a name of test3.
I am using choose step in my query to handle different values of my vertices.
g
.V()
.has('label',
within(
'data_computed_property',
'data_variable','recipe_ingredient',
'cost_setting_variable','benefit_analysis_variable',
'formula',
'detail'
)
)
.has('name', 'test3') // this is where i filter out all the detail vertices with a name of `test3`
.where(
__.in()
.has('label',
within(
'product',
'recipe',
'machine'
)
)
.in()
.has('id', '13eec000-78fa-41fd-856b-bbc37711b1f0')
)
.project('parent','property')
.by(
__.in()
.has('label',
within(
'product',
'recipe',
'machine'
)
)
.valueMap(true)
)
.by(
choose(values('label'))
.option(
'detail',
project('detail','value')
.by(valueMap(true))
.by(
choose(values('type'))
.option('string', values('value'))
.option('number', values('value'))
.option('date', values('value'))
.option('formula', out('value').valueMap(true))
.option('condition', out('value').valueMap(true))
.option('variable', out('value').valueMap(true))
.option('element', out('value').valueMap(true))
)
)
.option('data_computed_property', valueMap(true))
.option('data_variable', valueMap(true))
.option('recipe_ingredient', valueMap(true))
.option('cost_setting_variable', valueMap(true))
.option('benefit_analysis_variable', valueMap(true))
.option('formula', valueMap(true))
)
And this is the output of my query
array:2 [
0 => array:2 [
"parent" => array:3 [
"id" => "bee7eaa1-ad6c-436e-9000-7bf4665552d9"
"label" => "product"
"name" => array:1 [
0 => "Product 1"
]
"description" => array:1 [
0 => ""
]
"pk" => array:1 [
0 => "13eec000-78fa-41fd-856b-bbc37711b1f0"
]
"sku" => array:1 [
0 => "1235455"
]
]
"property" => array:2 [
"detail" => array:6 [
"id" => "5ad33864-25a7-424a-ba8d-a1b3e255d0a9"
"label" => "detail"
"name" => array:1 [
0 => "test3"
]
"type" => array:1 [
0 => "date"
]
"pk" => array:1 [
0 => "bee7eaa1-ad6c-436e-9000-7bf4665552d9/detail"
]
"value" => array:1 [
0 => 1583884800
]
]
"value" => 1583884800
]
]
1 => array:2 [
"parent" => array:3 [
"id" => "25c4cdfd-1845-4696-a427-5814d7adf71c"
"label" => "product"
"name" => array:1 [
0 => "Product 2"
]
"description" => array:1 [
0 => ""
]
"pk" => array:1 [
0 => "13eec000-78fa-41fd-856b-bbc37711b1f0"
]
"sku" => array:1 [
0 => "2569885"
]
]
"property" => array:2 [
"detail" => array:5 [
"id" => "db15ff37-4b13-4520-8e72-ae9f559f0fdc"
"label" => "detail"
"name" => array:1 [
0 => "test3"
]
"type" => array:1 [
0 => "variable"
]
"pk" => array:1 [
0 => "25c4cdfd-1845-4696-a427-5814d7adf71c/detail"
]
]
"value" => 1583884800
]
]
]
As you can see, the value property for the last element of list is a value that belongs to the first item.
This is how the output is expected to look like
array:2 [
0 => array:2 [
"parent" => array:6 [
"id" => "bee7eaa1-ad6c-436e-9000-7bf4665552d9"
"label" => "product"
"name" => array:1 [
0 => "Product 1"
]
"description" => array:1 [
0 => ""
]
"pk" => array:1 [
0 => "13eec000-78fa-41fd-856b-bbc37711b1f0"
]
"sku" => array:1 [
0 => "1235455"
]
]
"property" => array:2 [
"detail" => array:6 [
"id" => "5ad33864-25a7-424a-ba8d-a1b3e255d0a9"
"label" => "detail"
"name" => array:1 [
0 => "test3"
]
"type" => array:1 [
0 => "date"
]
"pk" => array:1 [
0 => "bee7eaa1-ad6c-436e-9000-7bf4665552d9/detail"
]
"value" => array:1 [
0 => 1583884800
]
]
"value" => 1583884800
]
]
1 => array:2 [
"parent" => array:3 [
"id" => "25c4cdfd-1845-4696-a427-5814d7adf71c"
"label" => "product"
"name" => array:1 [
0 => "Product 2"
]
"description" => array:1 [
0 => ""
]
"pk" => array:1 [
0 => "13eec000-78fa-41fd-856b-bbc37711b1f0"
]
"sku" => array:1 [
0 => "2569885"
]
]
"property" => array:2 [
"detail" => array:5 [
"id" => "db15ff37-4b13-4520-8e72-ae9f559f0fdc"
"label" => "detail"
"name" => array:1 [
0 => "test3"
]
"type" => array:1 [
0 => "variable"
]
"pk" => array:1 [
0 => "25c4cdfd-1845-4696-a427-5814d7adf71c/detail"
]
]
"value" => [
"id" => "63b66993-cb5e-4cdb-b7c4-5db069106ac3"
"label" => "cost_setting_variable"
"name" => array:1 [
0 => "07/24/2021-variable"
]
"value" => array:1 [
0 => "50"
]
"pk" => array:1 [
0 => "4837194c-fe11-485a-9ef2-54fc0d621059"
]
]
]
]
]
I am building a query throw querybuilder objects, something like
$qb = $em->createQueryBuilder()
->select('n, t, ns1m')
->from('App\Entity\Nuclei','n')
->leftJoin('n.statistiche1M', 'ns1m', 'WITH', 'DATE_PART(\'year\', ns1m.dataora) = YEAR(CURRENT_DATE())')
->leftJoin('n.codicitag', 't')
->where('n.comune = :id_comune')
->setParameter('id_comune', $this->user->getComune()->getId());
This query return an array of entity objects "nucleo":
array:3 [▼
0 => App\Entity\Nuclei { ... }
1 => App\Entity\Nuclei { ... }
2 => App\Entity\Nuclei { ... }
I want to add another column to select
->addSelect('SUM(ns1m.totale_peso_conferimenti_indifferenziata) AS total_test')
but now the result is:
array:2 [▼
0 => array:2 [▼
0 => App\Entity\Nuclei {#1671 ▶}
"total_test" => 1520
]
1 => array:2 [▶]
]
n.codicitag is a onetomany relations and is a collections of other objects
how can to handle this? Made many tests without success
What is your requested result? Result seems fine. You get sum for every Nuclei object.
If you need total sum without changing result structure, create extra query:
$qb = $em->createQueryBuilder()
->select('SUM(ns1m.totale_peso_conferimenti_indifferenziata) AS total_test')
->from('App\Entity\Statistiche1M','s1m')
->where('DATE_PART(\'year\', ns1m.dataora) = YEAR(CURRENT_DATE())');
This query return total sum filtered by current year.
You can try:
$qb = $em->createQueryBuilder()
->select('n, t, ns1m', 'SUM(ns1m.totale_peso_conferimenti_indifferenziata) AS total_test')
->from('App\Entity\Nuclei','n')
->leftJoin('n.statistiche1M', 'ns1m', 'WITH', 'DATE_PART(\'year\', ns1m.dataora) = YEAR(CURRENT_DATE())')
->leftJoin('n.codicitag', 't')
->where('n.comune = :id_comune')
->setParameter('id_comune', $this->user->getComune()->getId());
Because addSelect() create new array.
I have 2 entities with a one-to-many relationship. I need to select the name from both entity 1 and entity 2
$qb
->select(['f.name1', 'c.name2'])
->from('BundleOne:EntityOne', 'c')
->innerJoin('c.EntityTwo', 'f');
return $qb->getQuery()->getArrayResult();
With the above query, I get the following results:
1 => array:2 [
"name1" => "xyz"
"name2" => "n1"
]
2 => array:2 [
"name1" => "xyz"
"name2" => "n2"
]
3 => array:2 [
"name1" => "abc"
"name2" => "n3"
]
4 => array:2 [
"name1" => "abc"
"name2" => "n4"
]
As you can notice, since this is a one-to-many relationship, a name1 can have several name2 associated with it and instead of the above, I want to return the result as follows:
"xyz" => array:2 ["n1", "n2"]
"abc" => array:2 ["n3", "n4"]
that is the name1 as the key of the array that contains all name2
Is that possible?
You could do it like this (you didn't provide any real-world example of entities, so I created two on my own, just for the sake of simplicity):
#Category 1-N Product
#AppBundle/Repository/CategoryRepository.php
public function getProducts()
{
$qb = $this
->createQueryBuilder('c')
->select('c, p')
->leftJoin('c.products', 'p')
;
return $qb->getQuery()->getArrayResult();
}
Then in the controller:
#AppBundle/Controller/DefaultController.php
public function indexAction()
{
$categories = $this->getDoctrine()->getRepository('AppBundle:Category')->getProducts();
$results = [];
foreach($categories as $category) {
$results[$category['name']] = $category['products'];
}
return $this->render('...', [
'results' => $results,
]);
}
The results will be displayed like this:
array:2 [▼
"category1" => array:3 [▼
0 => array:2 [▼
"id" => 1
"name" => "product1"
]
1 => array:2 [▼
"id" => 2
"name" => "product2"
]
2 => array:2 [▼
"id" => 3
"name" => "product3"
]
]
"category2" => array:2 [▼
0 => array:2 [▼
"id" => 4
"name" => "product1"
]
1 => array:2 [▼
"id" => 5
"name" => "product2"
]
]
]