Question

I'm starting developing with symfony. Unfortunately I wasting my time on writing simple SQL queries using Symfony2 Doctrine I don't know very well.

Here is my problem: I wan't to make a simple LEFT JOIN and get needed data.

My query:

$query = $this->getDoctrine()->getManager()
                ->createQuery(
                    'SELECT p, s FROM DemoProductBundle:Product p LEFT JOIN DemoProductBundle:SynchronizationSetting s WITH p.id = s.id_product ORDER BY p.id ASC'
                );

When there is only SELECT s everything works fine but if I add SELECT p, s query returns NULL. Maybe something is wrong with the SynchroniZationSetting entity? I've set IDENTYTY column here by myself.

Demo\ProductBundle\Entity\SynchronizationSetting:
    type: entity
    table: synchronization_setting
    id:
          id_product:
              type: integer
              nullable: false
              unsigned: true
              comment: ''
              id: true
              generator:
                  strategy: IDENTITY
    fields:
        sonia:
            type: string
            nullable: true
            length: 1
            fixed: true
            comment: ''
            default: '0'
        strefa:
            type: string
            nullable: true
            length: 1
            fixed: true
            comment: ''
            default: '0'
        open:
            type: string
            nullable: true
            length: 1
            fixed: true
            comment: ''
            default: '0'
        internet:
            type: string
            nullable: true
            length: 1
            fixed: true
            comment: ''
            default: '0'
    oneToOne:
        idProduct:
            targetEntity: Product
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                id_product:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }
Was it helpful?

Solution

The problem was caused by using improper column names. Symfony entity generator change names for example from id_product to idProduct.

So:

 SELECT p, s FROM DemoProductBundle:Product p 
 LEFT JOIN DemoProductBundle:SynchronizationSetting s WITH p.id = s.id_product ORDER BY p.id ASC

shoud be:

 SELECT p, s FROM DemoProductBundle:Product p 
LEFT JOIN DemoProductBundle:SynchronizationSetting s WITH p.id = **s.idProduct** ORDER BY p.id ASC

Now works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top