Symfony2/Doctrine joined query - php

I need to create a simple query that produces a result set of a database entry plus the username of the person that posted it.
I've tried to setup the associations properly but I'm not sure if that's right either. I'm finding the whole idea of using these small string identifiers quite confusing. Surely there must be a simpler way of doing a join?
My two entities:
class Users
{
// ...
/**
* #ORM\Column(type="string")
* #ORM\OneToMany(targetEntity="Titles", mappedBy="addedBy")
*/
protected $username;
// ..
}
and
class Titles
{
// ....
/**
* #ORM\Column(type="string")
* #ORM\ManyToOne(targetEntity="Users", inversedBy="username")
*/
protected $addedBy;
// ....
}
with the following in the controller:
$titles = $em->createQueryBuilder()
->select('t.*', 'u.*')
->from('dvdLoggerdvdBundle:Titles', 't')
->leftJoin('t.addedBy', 'u')
->addOrderBy('t.title', 'DESC')
->getQuery()
->getResult();
I'm getting the following error:
[Semantical Error] line 0, col 69 near 'u ORDER BY t.title': Error: Class
dvdLogger\dvdBundle\Entity\Titles has no association named addedBy `
Update 1
I made all the changes suggested by Tom and did lots of reading!
It appears that in order to overcome the lazy loading feature I need to carry out a leftJoin. I have rewritten my query as follows:
public function getAllTitles()
{
// view all records in db
$titles = $this->createQueryBuilder('t')
->select('t, u')
->leftJoin('t.addedBy', 'u')
->addOrderBy('t.title', 'DESC');
return $titles->getQuery()->getResult();
}
I am getting a result set, but the addedBy is returning NULL when I dump the result set. As far as I'm aware shouldn't this pull the associated field in from the other table?

Best practice is to reference the entity by its id, you are trying to reference it using the username. The inversed field should also be a specific field not an existing one that holds data. And keep it mind this field is optional and defines the associations as bidirectional, for the specified use case you don't actually need it as you are joining from the Titles entity. I would advice reading the doc here http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations as well as here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html
Bidirectional association (w/ inversed field)
First get rid of that line:
#ORM\Column(type="string")
In your $addedBy annotations and change inverseBy="username" to inversedBy="titles" (note the typo)
You optionaly could add
#ORM\JoinColumn(name="user_id", referencedColumnName="id")
Then in your Users Entity add
/**
*
* #ORM\OneToMany(targetEntity="Titles", mappedBy="addedBy")
*/
protected $titles;
And get rid of
* #ORM\OneToMany(targetEntity="Titles", mappedBy="addedBy")
In your $username annotations
Last make sure you update the database schema
Then your query should return the expected result.
Unidirectional association (w/out inversed field)
Get rid of
* #ORM\OneToMany(targetEntity="Titles", mappedBy="addedBy")
In your $username annotations
Then get rid of that line in your $addedBy annotations:
#ORM\Column(type="string")
As well as inverseBy="username"
You optionaly could add
#ORM\JoinColumn(name="user_id", referencedColumnName="id")
Last make sure you update the database schema
Then your query should return the expected result.

Related

Doctrine join a table with two

Good morning, as seen in the image below, I have some tables linked.
Using Doctrine (in Symfony2) I'm trying to get an array of Objects Issue which itself contains all IssueMessages and IssueStatusChanged objects but can not.
I have no idea how I can do to join two tables (IssueMessage and IssueStatusChanged) to through their identifiers.
The most we've done is get all Issue with an account of the messages that have:
$dql = 'SELECT x, COUNT(im.id) FROM PanelBundle:Issue x LEFT JOIN PanelBundle:IssueMessages im WITH x.id = im.idIssue';
Does anyone could give me a hand?
THANKS!
You want to use assication mapping; this will have Doctrine manage all the joins for you.
Once in place, $issue will always have the other associated models available automatically without you having to worry about joins.
For the example below (assuming you use annotation), to get messages for an issue just get the issue objects and then use $issue->getMessages();.
<?php
/** #Entity */
class issue
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
// ...
/**
* #OneToMany(targetEntity="issueMessages", mappedBy="issue")
*/
private $messages;
// ...
public function __construct()
{
$this->messages = new Doctrine\Common\Collections\ArrayCollection();
}
}
/** #Entity */
class issueMessages
{
// ...
/**
* #ManyToOne(targetEntity="issue", inversedBy="messages")
* #JoinColumn(name="issue_id", referencedColumnName="id")
*/
private $issue;
// ...
}
If you using yml format for schema orm files than
first you need to write schema and mention oneToMany, manyToOne relationship with table fields & generate entity, repository class.
Than you can use join with two or more tables as below example:
Example of repository class file function:
----------------------------------------------------
public function getReportInfo($idUserDetail)
{
$query = $this->createQueryBuilder('UR')
->select("UR.report_period_start_date, UR.report_period_end_date")
->leftJoin('UR.UserReportDetail', 'URD')
->andWhere('UR.id_user_detail = :id')
->setParameter('id', $id)
->orderBy('UR.report_year', 'DESC')
->addOrderBy('UR.report_month', 'DESC')
->setMaxResults(1);
$resultArray = $query->getQuery()->getArrayResult();
return $resultArray;
}
You can call this function from controller action as below:
-------------------------------------------------------------
public function getUserDetailAction($idUserDetail)
{
$em = $this->getDoctrine()->getManager();
$userDetail = $em->getRepository(
'DemoBundle:UserDetail')
->getReportInfo($idUserDetail);
return $userDetail;
}
I hope this would be useful to you.
I think the problem reside in the DQL syntax (+ missing inverse relation?).
By writing this:
SELECT x, COUNT(im.id) FROM PanelBundle:Issue x
LEFT JOIN PanelBundle:IssueMessages im WITH x.id = im.idIssue
you are joining two "random" table based on the condition provided in the WITH clause. This should usually be ok, but it may confuse the Hydrator component.
In your case you should configure the OneToMany side of the relation in Issue entity, then write something like this:
SELECT x, COUNT(im.id) FROM PanelBundle:Issue x
LEFT JOIN x.issueMessages im
Hope it helps!

Symfony2 - Doctrine - Filtering arrayCollection to get only one object

I am new in Doctrine. I have entities and associations works fine.
My issue:
I have three entities User, Comment, Comment_status.
Comment is connected to main table Finding but this doesnt matter in this case.
Association: Comment <---1:n---> Comment_status <---n:1---> User
That should handle relation between users wathing list of comments and setting like/dislike for every of them.
I am using TWIG to show a list of comments on the page and thats enough for all data like "date", "like count" etc but if I want to get a comment_status for specified user (loged in wathing a page) I dont know how to get this.
if I return array to TWIG "comments" , then loop it for every one of them as "comment" i can access using {{ comment.content }} to data but with comment_status I have no idea bacause it is an array and I just want one element of this array which match to logged in user ID.
I suppose I have to make some DQL ask to DB but have no idea how to do this.
I read some about Criteria in Doctrine but still no idea how to use it.
Can anybody give an step by step solution?
parts of my code:
Controller
> $comments = $em->getRepository('MySpecialBundle:Comment')
> ->findBy(array('finding' => $finding));
>
> return array('comments' => $comments );
Comment entity (part which create association)
> /** * #ORM\OneToMany(targetEntity="Comment_status",
> mappedBy="comment", cascade={"persist", "remove"}, orphanRemoval=true)
> */ protected $commentStatuses;
Comment_status entity (part which create association)
> /** * #ORM\ManyToOne(targetEntity="Comment",
> inversedBy="commentStatuses") * #ORM\JoinColumn(name="comment_id",
> referencedColumnName="id", nullable=false) */ protected $comment;
/**
> * #ORM\ManyToOne(targetEntity="My\MainBundle\Entity\User",
> inversedBy="commentStatuses") * #ORM\JoinColumn(name="user_id",
> referencedColumnName="id", nullable=false) */ protected $user;
User entity (part which create association)
/**
* #ORM\OneToMany(targetEntity="My\SpecialBundle\Entity\Comment_status", mappedBy="user", cascade={"persist"})
*/
protected $commentStatuses;
I want to have a way to get (instead an array collection of all "commentStatuses") only one object which contains an user id of user logged in.
Thank you for help.
You can select and hydrate only the entities matching your criteria in a doctrine querybuilder query. This way you get an already filtered collection. E.g. Something like:
$qb = $this->getEntitymanager()->createQueryBuilder();
$user = $qb->select('u, cs, c')
->from('YourBundle:User', 'u')
->join('u.commentStatuses', 'cs')
->join('cs.comment', 'c')
->where($qb->expr()->eq('u', ':user')
->setParameter('user', $yourLoggedInUser)
->getQuery()->getResult();
This will get you your user and comments/statuses only applicable to that user.
Thanks to #Richard I found a way to solve this.
The right answer is here:
$qb = $this->getEntitymanager()->createQueryBuilder();
$user = $qb->select('c, cs')
->from('MyBundle:Comment', 'c')
->leftjoin('c.commentStatuses', 'cs', 'WITH', 'cs.user = :user')
->setParameter('user', $myLoggedInUser)
->getQuery()->getResult();
Leftjoin - gives all records from Comment not only that contains commentStatus
WITH - choose only right commentStatuses
If I would use #Richard version with second join and WHERE it would show only Comments where user and commentStatus exists.
Anyway thans for help

Doctrine2 join column

I have defined the follow entity in doctrine2 (with symfony).
/**
*
* #ORM\Table(name="order")
* #ORM\Entity
*/
class Order
/**
* #var integer
*
* #ORM\Column(name="personid", type="integer", nullable=false)
*/
private $personid;
/**
* #ORM\OneToOne(targetEntity="People")
* #ORM\JoinColumn(name="personid", referencedColumnName="personid")
*/
private $person;
public function getPersonId()
{
return $this->personid;
}
public function getPerson()
{
return $this->person;
}
}
I realize that if I call $order->getPersonId() it return always an empty value and I have to call the getPerson()->getId() method to get the correct personid.
Could anyone explain me why the variable $personid is not filled?
Should I to delete the column id used for the join if I defined one?
Thanks
Gisella
You should remove private $personid;, it's better to work with objects only in an ORM.
It's not a problem if you get the ID with $order->getPerson()->getId(), because Doctrine won't load the complete entity. The People entity will only be loaded if you call an other field than the join key.
You can still have a getter shortcut like this :
public function getPersonId()
{
return $this->getPerson()->getId();
}
Edit :
You can also still work with "ID" if you use Doctrine references, like this :
$order->setPerson($em->getReference('YourBundle:People', $personId));
With this way, Doctrine won't perform a SELECT query to load data of the person.
You don't need to have the $personid field when you already have the $person field.
$people contains the People object (with all People's attributes including the id).
Moreover, when doctrine translate your object into sql tables, he knows that he have to join with th id so it will create a field (in database) named personid. (It's the name that you defined in your ORM)
/**
* #ORM\OneToOne(targetEntity="People")
* #ORM\JoinColumn(name="personid", referencedColumnName="personid")
*/
private $person;
Sorry for bad english :p

Symfony2 and Doctrine: how to sort entity collection by dependant entity field

In my sf2 project I access entity collection by calling:
$user_payment_info_datas = $user->getUserPaymentInfoDatas();
In the User entity there is:
/**
* #ORM\OneToMany(targetEntity="UserPaymentInfoData", mappedBy="user")
* #ORM\OrderBy({"payment_info" = "ASC", "payment_info_data" = "ASC"})
*/
private $user_payment_info_datas;
So it's 1:n relation and user has many UserPaymentInfoData's. However, there is another entity called PaymentInfoData that contains the actually values for UserPaymentInfoData's. So the relation is
User -> UserPaymentInfoData -> PaymentInfoData.
So in terms of annotations in UserPaymentInfoData it is:
/**
* #ORM\ManyToOne(targetEntity="PaymentInfoData", inversedBy="user_payment_info_datas")
* #ORM\JoinColumn(name="payment_info_id", referencedColumnName="id")
* #ORM\OrderBy({"title"="ASC"})
*/
private $payment_info_data;
I need to sort the collection returned by
$user_payment_info_datas = $user->getUserPaymentInfoDatas();
ascending by a field from PaymentInfoData (let's call it 'title') and NOT UserPaymentInfoData.
Can I do this with Doctrine annotations? Or without writing DQL?
I know I can do it with:
$user_payment_info_datas = $em->getRepository('STMainBundle:UserPaymentInfoData')
->createQueryBuilder('upid')
->innerJoin ('upid.payment_info_data', 'pid')
->where('upid.user = :user')
->addOrderBy('upid.payment_info', 'ASC')
->addOrderBy('pid.title', 'ASC')
->setParameter('user', $user)
->getQuery()
->getResult();
but the question is, whether it's possible to stay with only annotations as I need to fix it in a few places and it would be convenient to just change annotations and not create query builder in two places.

doctrine2: Entity's association changes after first call

i am using the latest version of doctrine: 2.3
when you call a generated association function, the first time everything is fine:
$authors = $book->getBookToAuthors();
//$authors = array(5)
but the second time instead of returning the array of all associations it returns the last hydrated entity:
$authors = $book->getBookToAuthors();
//$authors = BookToAuthor entity
that happens even when there is nothing else happening:
$authors = $book->getBookToAuthors(); //will work
$authors = $book->getBookToAuthors(); //won't work
the function of getBookToAuthors() is:
public function getBookToAuthors()
{
return $this->bookToAuthors;
}
and the mapping is as follows:
/**
* #var BookToAuthor[]
*
* #OneToMany(targetEntity="BookToAuthor", mappedBy="book", cascade={"persist"})
* #JoinColumn(name="id", referencedColumnName="book_id", onDelete="cascade")
*/
private $bookToAuthors;
please advise. i don't know what to do... :-(
sorry sorry sorry
it was a mistake in the association target side.
the target had One-To-One association instead of Many-To-One
if you have this problem make sure the association type in both sides is matching

Categories