S
S
symnoob2019-11-05 22:19:30
symfony
symnoob, 2019-11-05 22:19:30

Can anyone help with SQL DQL?

Hello everyone,

please help with sql/dql, there are two tables / entities.

I need to get all the data from artikel.art_text, provided that art.id =3

if there is no connection with "artikel.art", then the fields that came from "artikel.art" should be null, but all fields from artikel.art_text.

I hope that it is clear what I want to do...

at the moment only one data line is displayed:

SELECT * FROM artikel.art_text txt
LEFT JOIN artikel.art art ON txt.art_id = art.id

WHERE art.id =3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Nizhny Novgorod, 2019-11-06
@symnoob

1) Doctrine Mapping is a bad thing for large projects, because it doesn't always work correctly. Therefore, it is better to work with composite keys.
2) @ORM\GeneratedValue() - this is also not the best practice, then you are tormented by sharding the project and changing the table schema. Usually projects on Symfony are not blogs (but this is so for the future)
3) In general, everything looks something like this:
In the repository of the parent class, we write a method that looks something like this. In the same place we write Joint to the class that you need - we do the mapping and through where / andWhere we add the selection conditions you need. This is an example on innerJoin, on leftJoin it will look the same, just the method is different.

public function GetUsers(int $offset = 0, int $limit = 1000): array
    {
        $users = $this->createQueryBuilder('a')
            ->select('a.email')
            ->join(Secret::class, 's', Join::WITH, 'a.site = s.siteId')
            ->where('s.active = :active')
            ->setParameter('active', 1)
            ->setFirstResult($offset)
            ->setMaxResults($limit)
            ->getQuery()
            ->getScalarResult();

        return $users;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question