V
V
Vampiro2012-06-18 22:41:21
Doctrine ORM
Vampiro, 2012-06-18 22:41:21

how to make self join in Doctrine 2 without specifying explicit links?

There is an abstract entity. For example, news. With each editing, we create a new news item in the database with a certain number, different text and a new date. Suppose we have three fields for this. (id, newsId, text, date).
without id we are nowhere, but it doesn't matter. All this works great.

And now I need to make a selection of such a plan: Voooot ... that is, pull out two news items with the same ID, but with consecutive dates. Naturally, since the relationship between news is not described in the ORM model, the Query Builder swears. " Identification Variable testBundle\Entity\news used in join path expression but was not defined before. "(c)
em->createQuery("select news1, news2 from
testBundle\Entity\news news1
left join
testBundle\Entity\news news2
on (news1.newsId=news2 .newsId)
where news1.date ='today' and news2.date ='1 day ago')
->setMaxResults(10)
->getResult();




I still smoke Versionable, but I don’t want to redo what I already have. The SQL query has been compiled, but I can’t figure out how to “squeeze” it into the ORM.

CHADNT?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2012-06-18
Protko @Fesor

Literally did exactly the same thing today. It's possible. There are several solutions to the problem. You can suffer with DQL, or you can write through Native SQL. The second option always works, but you will also have to describe the mapping of the results (it's not tricky, but tiring).
Although in your case it would be more logical to use some kind of entity versioning extension .

S
serjoga, 2012-06-19
@serjoga

So describe the connection of the table to itself, give some kind of alias for the connection to make it clear. I did this in Doctrine 1.2 for ID-ParentID tables.
It is not clear, of course, how can there be 2 news items with the same ID and different dates?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question