I have a entity Article20000Information, with a few fields like: id, description, manufacturer_id, supplier_id
I have another entity, Organisation. It has a list of companies (both manufacturers & suppliers) each with an id.
I also have a page which renders a list of Article20000Information data. Currently, it simply displays the data in the table so:
| id | Description | Manufacturer | Supplier | Price |
|----|-------------|--------------|----------|--------|
| 1 | thing1 | 2 | 5 | 34 |
| 2 | thing2 | 5 | 2 | 23 |
| 3 | thing3 | 3 | 4 | 25 |
What I need is for the manufacturer and supplier column to display the name value from the organisation table, based on the id shown.
What is the best way to go about this?
Got it!
I needed multiple aliases, which I'd guessed, but I also needed to give them AS so that they come out with different column names. This in turn lets twig render the tags.
<?php
namespace Regenerys\QMSBundle\Entity;
use Doctrine\ORM\EntityRepository;
class Article20000InformationRepository extends EntityRepository
{
public function findStuff()
{
return $this->getEntityManager()
->createQuery(
'SELECT
A.id,
A.articleNumber,
A.description,
B.name as manufacturer,
C.name as supplier
FROM
RegenerysQMSBundle:Article20000Information A
LEFT OUTER JOIN RegenerysQMSBundle:Organisation B WITH B.id = A.manufacturer
LEFT OUTER JOIN RegenerysQMSBundle:Organisation C WITH C.id = A.supplier '
)
->getResult();
}
}
Thanks to #Alexandru for his DQL help.
You need to join the two tables based on id condition.
select A.id, A.Description, B.ManufacturName, B.supplierName
from Article20000Information A
left outer join Organisation B
ON B.id = A.id
More info on table joins.
If you are using doctrine, a proper way is to create a Repository class, and there write your joined code proposed by #K139 but in DQL:
class Article20000InformationRepository extends EntityRepository
{
public function findAll()
{
return $this->getEntityManager()
->createQuery(
'SELECT A.id, A.Description, B.ManufacturName, B.supplierName FROM AppBundle:Article20000Information A
LEFT OUTER JOIN AppBundle:Organisation B ON B.id = A.id '
)
->getResult();
}
}
Then in your controller you will use it:
$articles = $em->getRepository('AppBundle:Article20000Information')->findAll();
Related
A query load and hydrate a main entity with it's joined entities, which are filtered.
Same query with a different filtering did not update the joined entities.
Some data:
Tiers:
| id | name |
| 1 | alpha |
| 2 | beta |
Container:
| id | tiers_id | category |
| 10 | 1 | A |
| 20 | 1 | A |
| 30 | 1 | B |
| 40 | 1 | B |
Execute 2 queries to get some tiers with theirs containers joined, category A first, then category B:
$dql = "select t, c
from Tiers t
join t.containers c
where t.id in (?1) and c.category = (?2)";
$result = $em->createQuery($dql)
->setParameter(1, array(1))
->setParameter(2, 'A')
->getResult();
$tiers = $result[0];
$containers = $tiers->getContainers(); // tiers 1 with containers 10 and 20, that's fine !
$result = $em->createQuery($dql)
->setParameter(1, array(1))
->setParameter(2, 'B')
->getResult();
$tiers = $result[0];
$containers = $tiers->getContainers(); // BAD HERE: still get containers 10 and 20, looking for containers 30 and 40.
After the 2nd query, tiers 1 preserve its containers loaded during first query. That's not what is expected.
So is there a way to get the containers 30 and 40 after 2nd query ?
Maybe a kind of "reset/detach" the containers of tiers entities after the first query ?
Or anything else...
Mutiple select in queries are used to hydrate tiers with required containers joined.
'getContainers' method gives the expected containers from each tiers.
And cost to BDD is only 1 SQL query, whatever the quantity of tiers searched.
I suppose tiers cannot be detach/reload because they are updated before, between and after the queries, it throw this kind of exception when flushing:
Uncaught Exception: Multiple non-persisted new entities were found through the given association graph
* A new entity was found through the relationship 'XXX' that was not configured to cascade persist operations for entity: XXX\Entity\Tiers#00000000257b87500000000018499b62.
Reset the tiers's containers before 2nd query:
foreach($result as $tiers)
$tiers->nullContainers();
Add method to Entity\Tiers:
public function nullContainers()
{
this->containers = null;
}
Then the 2nd query "refresh" tiers's containers.
I Have two tables : Post and Comment in a Many to One relation.
table post table comment
p_id | name | u_id c_id | p_id | comment | date
1 | post 1 | 1 1 | 1 | blah blah blah | 2017-01-01
2 | post 2 | 2 2 | 1 | blah blah blah | 2017-01-04
3 | post 3 | 1 3 | 2 | blah blah blah | 2017-01-07
... ...
I would like to retrieve all Posts by u_id with their 3 Last comments ordered by date.
I would do :
SELECT p, c
FROM p
LEFT JOIN p.comments c WITH c.date IN (SELECT c2.date) FROM App\Bundle\Entity\Comment as c2 WHERE c2.post = p.p_id ORDER BY date ASC LIMIT 3)
WHERE p.user = 1
But doctrine doesn't allow LIMIT, how i can do that ?
In my opinion the easiest way to to that is by using the function
setMaxResults($limit);
I give you an example :
$qb = $this->createQueryBuilder('a');
$qb
->innerJoin('a.advert', 'adv')
->addSelect('adv')
;
$qb->setMaxResults($limit);
return $qb
->getQuery()
->getResult()
I hope it will help you
As I known QueryBuilder::setMaxResult() work only on primary table not on subquery.
IMHO it's best to write it in classic SQL, where you have better control on subquery and what is joining. In this case you get plain array not array of objects with relation. But, this is better when you need only to show lists of posts and not interact with single entity. In this case getting entire object hydrating one by one populating from DQL, is quite slower then using plain array from SQL.
You could do this like that:
$conn = $this->getEntityManager()->getConnection();
$stmt = $conn->prepare('SELECT ...');
$stmt->execute($parameters);
$results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
I have a couple of tables:
table items: id, title
table properties: id_item, name, value
So an item has multiple properties (is an EAV).
Now i need to find which item has some properties, so i try to join the same relation multiple times:
$queryBuilder = $this->createQueryBuilder('i')
->join('i.properties', 'p');
$i = 0;
foreach ($properties as $name=>$value) {
$queryBuilder->join('i.properties', 'p'.$i)
->andWhere("p{$i}.name = :name".$i)
->setParameter(':name'.$i, $name)
->andWhere("p{$i}.value = :value".$i)
->setParameter(':value'.$i, $value);
$i = $i + 1;
}
return $queryBuilder->getQuery()->getResult();
But this doesn't work because the join is not repeated by doctrine, it uses always the same.
UPDATE:
To be more clear, if I join only once the table item and properties I get:
id | title | name | value
1 | t shirt | color| red
1 | t shirt | size | large
But if i need to search the items that have property color=red and size=large, i need to join it twice, so that i can make more where condition on different columns:
id | title | name1 | value1 | name2 | value2
1 | t shirt | color | red | size | large
The SQL generate actually is something like:
SELECT m0_.id AS id0
FROM item m0_
INNER JOIN properties m1_ ON m0_.id = m1_.item_id
WHERE m1_.name = 'Color' AND m1_.value = 'red' AND m1_.name = 'Size' AND m1_.value = 'Large'
But obviously m1_.name cannot be Color and Size at the same time.
When I search for how to compare two tables in SQLite, and see what's differ, I mostly find answers like this:
SELECT B.id FROM B LEFT JOIN A ON B.id = A.id WHERE A.id IS NULL
and yes, it's correct if you want do find all the elements (or values for keys named 'id' in this case) in table B that is not in table A, i.e. all the new elements in B if B is a later version of A.
But what if I want to find all the id:s in B where the value for a certain key (or keys) deviate from the corresponding value in A? For example, if I have two tables, A and B with id:s and positions, and I want to get the result id=3 in this case, because it is the element in B that has a value that differ. What would be the easiest way to do that?
Table A Table B
id | x_value | y_value id | x_value | y_value
----------------------- -----------------------
1 | 29.9563 | 12.6764 1 | 29.9563 | 12.6764
2 | 45.5843 | 7.6733 2 | 45.5843 | 7.6733
3 | 28.2313 | 15.6579 3 | 39.2003 | 15.6579
Result:
id
--
3
You can do it with a inner join with your condition in the where clause.
select a.id
from tableA a join tableB b on a.id = b.id
where ifnull(a.x_value, 0) <> ifnull(b.x_value, 0)
or ifnull(a.y_value, 0) <> ifnull(b.y_value, 0)
You can use INTERSECT:
LiveDemo
SqlFiddleDemo
SELECT tA.id
FROM TableA tA
JOIN TableB tB
ON tA.id = tB.id
WHERE NOT EXISTS( SELECT tA.x_value, tA.y_value
INTERSECT
SELECT tB.x_value, tB.y_value);
I like this solution, because it is easy to extend. Just add new column names. No need to handle NULL manually.
I agree with shawnt00 that you can read the question that the goal was to find all the id:s where values have changed between the two tables AND id:s of new instances inserted to the second table. Here is the select-statement to accomplish that, if anyone is interested:
select b.id
from b left join a on b.id = a.id
where ifnull(a.x_value, 0) <> ifnull(b.x_value, 0)
or ifnull(a.y_value, 0) <> ifnull(b.y_value, 0)
or a.id is null;
I read many questions here about mutual friends and following and followers but i can't solved my problem, I have two tables like these.
User_table ( UID, Name)
Relation_table (RID, UID,UIDF)
for example in user_table have 4 users
UID | Name
------------------
1 | Kim Tessman
2 | Nella Ohler
3 | Adria Larose
4 | Huey Errico
And Relation_table have these data
RID | UID | UIDF
------------------
1 | 1 | 2
2 | 2 | 1
3 | 1 | 4
4 | 4 | 3
5 | 4 | 1
my questions are :
how to get list of Mutual friends ?
how to get list of Following ?
how to get list of Followers ?
please who can solve this problem thank you :)
I am guessing that the UID column in the Relation_Table is the user and that the UIDF is the person they are following. Here is the SQL query that finds who a person is following. The who is being followed is the same query, you just focus on the other name column or switch the column order around.
Here is a list of who follows who. It is also the list of who is being followed.
SELECT FollowingUser.UID as FollowingUserID, FollowingUser.Name, BeingFollowed.UID AS BeingFollowedID, BeingFollowed.Name AS BeingFollowedName
FROM User_table AS FollowingUser INNER JOIN
Relation_Table AS r ON FollowingUser.UID = r.uid INNER JOIN
User_table AS BeingFollowed ON r.uidf = BeingFollowed.UID
And this query gives you the list of mutual friends - but it gives the inverse of the relationship. If this matters, add this to the end where FollowingUser.UID > BeingFollowed.UID
SELECT FollowingUser.UID AS FollowingUserID, FollowingUser.Name, BeingFollowed.UID AS BeingFollowedID, BeingFollowed.Name AS BeingFollowedName
FROM User_table AS FollowingUser INNER JOIN
Relation_Table AS r ON FollowingUser.UID = r.uid INNER JOIN
Relation_Table AS r1 ON r.uid = r1.uidf AND r.uidf = r1.uid INNER JOIN
User_table AS BeingFollowed ON r1.uid = BeingFollowed.UID