Doctrine DQL with multiple joined tables - symfony

i have entities **target,citytarget,city,cityplace,place
citytarget & cityplace are pivot tables that connect t&c and c&p
i need the places for given city name and a target id
i tried the following DQL:
SELECT t,c,p FROM MulticatorBundle:Target t
join t.cities ct
join ct.cities c
join c.places cp
join cp.places p
where c.name like '%Stahmeln%'
But i receive:
result: The parent object of entity result with alias 'c' was not found. The parent alias is 'ct'.
i dont know any further....
a plain SQL could be:
select * from target
left join citytarget on citytarget.target_id = target.id
left join city on citytarget.city_id = city.id
left join cityplace on cityplace.city_id = city.id
left join place on cityplace.id = place.id
where target.id = 1
and city.name like \'%Stahmeln%\'
Adrian

You always need your parent entity in your select. In this case:
SELECT ct, c, cp, t, p
is needed

You need to do
SELECT t,ct, c, cp ,p FROM MulticatorBundle:Target t
join t.cities ct
join ct.cities c
join c.places cp
join cp.places p
where c.name like '%Stahmeln%'
Hope this help you

Related

sqlite query to get instances were no items returned on join

I am querying the static backend db for a game I play (trying to keep up on my coding), and I am having an issue getting the full results that I want.
So the query that I have so far is:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID == mc.constellationID
join mapRegions as mr on ms.regionID == mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
join invTypes as it on it.typeID == ii.typeID
where it.groupID = 9
group by solarSystemName
the problem comes when there are no rows where it.groupID == 9. What I need is for the count to return 0 and I can't for the life of me figure out how to get this to work.
I tried doing left outer join on the final join statement, but no joy.
Change the last join to a left join and set the condition in the on clause istead of the where clause:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID = mc.constellationID
join mapRegions as mr on ms.regionID = mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
left join invTypes as it on it.typeID = ii.typeID and it.groupID = 9
group by solarSystemName

SQLite: replace result values using bijective table

In SQLite, I have the following query
SELECT x.nameIndex, y.nameIndex
FROM relation x, relation y
WHERE x.label=y.label AND x.feature=1 AND y.feature=0;
which returns all pairs of x.nameIndex,y.nameIndex with the same label where x has feature 1 and y has feature 0.
Now I have another table index2name where I store the name for each index, where I could do like:
SELECT name FROM index2name WHERE nameIndex=...;
How can I change the top query such that it looks up the name for the respective indeces and returns pairs of names instead?
Use a CTE which returns the name instead of the indexes and the group for each row in relation (by a join to index2name) and do a self join on that:
WITH cte AS (
SELECT i.name, r.label, r.feature
FROM relation r INNER JOIN index2name i
ON i.nameIndex = r.nameIndex
)
SELECT c1.name, c2.name
FROM cte c1 INNER JOIN cte c2
ON c2.label = c1.label
WHERE c1.feature=1 AND c2.feature=0;
Or without the CTE:
SELECT i1.name, i2.name
FROM relation r1 INNER JOIN relation r2
ON r2.label = r1.label
INNER JOIN index2name i1 ON i1.nameIndex = r1.nameIndex
INNER JOIN index2name i2 ON i2.nameIndex = r2.nameIndex
WHERE r1.feature=1 AND r2.feature=0;

Ax 2012 component existence in all layers and models

Can anybody help me how to find a component existence in all layers and models installed in that layer. i.e find all the layers and models in which a particular component(ax 2012) exists.
For example how to find all the layers and models in which CustTable exists.
Is there any class or table that stores this information.
please help me. Thanks in advance.
Comment is limited :(
Do you need to search through source code? Are you searching for merge candidates? Tell me your business requirement.
Here is the query returning elements that contains CustTable within selected models:
SELECT
me.Name ElementName
, Layer.Name Layer
, mm.Name ModelName
, ElementTypes.ElementTypeName
FROM ModelElement me
JOIN ModelElementData med
ON med.ElementHandle = me.ElementHandle
AND me.ParentHandle = 0 -- only root elements
JOIN ModelManifest mm
ON mm.ModelId=med.ModelId
JOIN Layer
ON Layer.Id = med.LayerId
JOIN ElementTypes
ON me.ElementType = ElementTypes.ElementType
WHERE EXISTS (
SELECT null
FROM ModelElement child
JOIN Sources s
ON s.SourceHandle = child.ElementHandle
AND s.LayerId = med.LayerId
AND child.RootHandle = med.ElementHandle
WHERE cast([SourceText] as nvarchar(max)) like '%CustTable%'
)
AND mm.Name IN ('Foundation','I4C')
You can try search within _model databese with next query (this searcehs all code references - table, class, form, dataset, ...):
SELECT TOP 1000 [SourceHandle]
, met.ElementTypeName
, ro.Name object
, me.Name method
, l.Name Layer
, mm.Name Model
, cast([SourceText] as nvarchar(max)) sourceTxt
FROM [Sources] s
join ModelElement me on me.ElementHandle=s.SourceHandle
join ElementTypes met on met.ElementType=me.ElementType
join ModelElement ro on ro.ElementHandle=me.RootHandle
left join ModelElementData med on med.ElementHandle=s.SourceHandle and med.LayerId=s.LayerId and med.ElementHandle=me.ElementHandle
left join Layer l on l.Id=s.LayerId
left join ModelManifest mm on mm.ModelId=med.ModelId
WHERE cast([SourceText] as nvarchar(max)) like '%CustTable%'
If you need all records, remove TOP 1000.
SELECT DISTINCT
ro.Name object,
ro.ElementType type,
l.Name Layer,
mm.Name Model
FROM Sources s
join ModelElement me on
me.ElementHandle = s.SourceHandle
join ElementTypes met
on met.ElementType = me.ElementType
join ModelElement ro
on ro.ElementHandle = me.RootHandle
left join ModelElementData med
on med.ElementHandle = s.SourceHandle
and med.LayerId = s.LayerId
and med.ElementHandle = me.ElementHandle
left join Layer l
on l.Id = s.LayerId
left join ModelManifest mm
on mm.ModelId = med.ModelId
where ro.Name = 'InventDim' order by mm.Name, ro.ElementType
In addition to the query suggested by the authors, a small change was made according to my requirement is giving the right results.

Full Outer Join in sqlite on 4 tables

I need to join 4 tables based on a common primary key. If sqlite implemented full outer joins it might look something like this (with optimization not taken into account).
SELECT S.pair, C.ball, P.bluejeans, B.checkered
FROM Socks S
FULL OUTER JOIN Caps C
FULL OUTER JOIN Pants P
FULL OUTER JOIN Boxers B
WHERE S.color = C.color AND S.color = P.color AND S.color = B.color;
I've looked long and hard and the best I found was this 2 table sqlite full join implemented with left joins and union alls:
SELECT employee.*, department.*
FROM employee LEFT JOIN department
ON employee.DepartmentID = department.DepartmentID
UNION ALL SELECT employee.*, department.*
FROM department LEFT JOIN employee
ON employee.DepartmentID = department.DepartmentID
WHERE employee.DepartmentID IS NULL;
I'm trying to modify this to work for more than 2 tables but I'm new to SQL and I'm not getting too far. Is it possible to get this result in a reasonable amount of time?
I think I have a correct implementation for 3 tables (it might not be correct) but I still can't seem to get it for 4. Here's what I have for 3:
SELECT S.pair, C.ball, P.bluejeans
FROM Socks S LEFT JOIN Caps C LEFT JOIN Pants P
ON C.color = S.color AND P.color = S.color
UNION ALL
SELECT S.pair, C.ball, P.bluejeans
FROM Socks S LEFT JOIN Caps C LEFT JOIN Pants P
ON S.color = C.color AND S.color = P.color
WHERE S.color IS NULL;
Any help is much appreciated
The general construction for a full outer join between two tables A and B in SQLite indeed is:
SELECT ... FROM A LEFT JOIN B ON ...
UNION ALL
SELECT ... FROM B LEFT JOIN A ON ... WHERE A.key IS NULL
Now create a view SocksCaps for the full outer join between Socks and Caps:
CREATE VIEW SocksCaps AS
SELECT ... FROM Socks LEFT JOIN Caps ON ...
UNION ALL
SELECT ... FROM Caps LEFT JOIN Socks ON ... WHERE Socks.color IS NULL
Do the same for Pants and Boxers.
Then treat these views just like tables and do a full outer join with the same construction:
SELECT ... FROM SocksCaps LEFT JOIN PantsBoxers ON ...
UNION ALL
SELECT ... FROM PantsBoxers LEFT JOIN SocksCaps ON ... WHERE SocksCaps.color IS NULL

problem with nested inner joins in SQLIte

The sql statement below will not run in SQLite:
select *
from A
left join (B inner join C on B.fkC = C.pk) on A.optionalfkB = B.pk
I get a sqlException "unknown column B.pk"
According to the documentation # http://www.sqlite.org/lang_select.html this should work, and it will work in all other sql implementations. Am I doing something wrong?
It doesn't work because the "outer" query doesn't know what B is.
select *
from A
left join (B inner join C on B.fkC = C.pk) B on A.optionalfkB = B.pk
The (B inner join C on B.fkC = C.pk) is weird without any select, but the specification does say that it is valid.

Resources