V
V
Vlad Sharikov2014-05-12 21:39:18
Doctrine ORM
Vlad Sharikov, 2014-05-12 21:39:18

How to work with Doctrine ObjectManager in the same way as with SQL query with JOIN? ZF2?

Hello.
Let's say there are 2 tables. There are news and there are categories. To get a response like this:

id_новости имя_категории название_новости текст_новости дата_создания_новости

In SQL we would write something like this:
select n.id, c.name, n.title, n.text, n.created from News as n inner join Category as c on c.id = n.category_id

In my case, we do not work directly with requests, but work with models - with the ObjectManager.
For example, right now I'm working with OM like this:
$news = $objectManager
            ->getRepository('\News\Entity\NewsItem')
            ->findBy(array(), array('created' => 'DESC'));

This will give a response like this:
id_новости, id_категории, заголовок, текст, дата_создания_новости

How to add a category name here? Normal JOIN in this case you will not write.
I assumed that I'm not the only one so "smart" and there must be some mechanism for this in Doctrine2. Relations tables: News M - 1 Categories. Googled and found this . It looks like my case. But before rushing into hell, I would like to clarify. What will it all look like in the end? I assume that I will have 3 models after all this. News, Categories, News-Categories. The first and second ones will be used for regular constructors (creating, deleting, etc. categories or news), and the third one only for output? Or only 2 models? There are big doubts about this assumption. Please explain what the final version should look like.
As a result, how to work with the resulting models? How to get what I wanted at the beginning of the article (same behavior as SQL query with JOIN statement)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-05-13
Protko @Fesor

Yes, read the Zend2/Doctrine documentation! Honestly, it's not even interesting anymore.
Read about DQL or QueryBuilder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question