php - Doctrine 2: Combine Select with Select from another table with identical structure

482

In my Symfony application in which I use Doctrine 2 I have two tables: APPOINTMENTS and APPOINTMENTS_ARCHIVED. Both tables have an identical (!) structure with identical fields!

My function in my AppointmentsRepository:

private function getAppointments()
{
    $qb = $this->createQueryBuilder('a');
    $qb->leftJoin('a.instructor', 'i')
        ->leftJoin('i.contactdetails', 'c')
        ->join('a.area', 'ar');  

    return $qb;
}

This repository points to my table APPOINTMENTS.

How can I add a select of data in APPOINTMENTS_ARCHIVED to this query? My goal is that my query does not only consider data in my table APPOINTMENTS, but in addition data in my table with archived data APPOINTMENTS_ARCHIVED.

936

Answer

Solution:

I assume that yourAPPOINTMENTS_ARCHIVED table is an archived version of theAPPOINTMENTS table ?

This means that the archived table is not mapped to an entity, therefore impossible to get with DQL

Your best bet would be to to user Native SQL.

687

Answer

Solution:

You can select multiple entities without them been related. In this case:

$qb = $this->createQueryBuilder('a');
$qb->addSelect('aa')
   ->from(AppointmentArchived::class, 'aa'); // ->join(AppointmentArchived::class, 'aa'); will also work
$qb->leftJoin('a.instructor', 'i')
    ->leftJoin('i.contactdetails', 'c')
    ->join('a.area', 'ar');  
$qb->leftJoin('aa.instructor', 'aai')
    ->leftJoin('aai.contactdetails', 'aac')
    ->join('aa.area', 'aar');  

You have to join and select the relations separately in this case.

People are also looking for solutions to the problem: php - Laravel new user sign up email dynamic error

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.