Sql Symfony2添加字段以选择断开左连接

Sql Symfony2添加字段以选择断开左连接,sql,symfony,doctrine-orm,dql,Sql,Symfony,Doctrine Orm,Dql,我开始和symfony一起发展。不幸的是,我把时间浪费在使用Symfony2原则编写简单的SQL查询上,我不太清楚 这是我的问题:我不想做一个简单的左连接来获取所需的数据 我的问题是: $query = $this->getDoctrine()->getManager() ->createQuery( 'SELECT p, s FROM DemoProductBundle:Product p LEFT JO

我开始和symfony一起发展。不幸的是,我把时间浪费在使用Symfony2原则编写简单的SQL查询上,我不太清楚

这是我的问题:我不想做一个简单的左连接来获取所需的数据

我的问题是:

$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'
                );
当只有selects时,一切正常,但如果我添加selectp,s查询将返回NULL。SynchroniZationSetting实体可能有问题?我自己在这里设置了同一列

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: {  }

问题是由于使用了不正确的列名造成的。Symfony实体生成器更改名称,例如从id_产品更改为idProduct

因此:

应该是:

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

现在可以正常工作。

显示错误消息,请注意错误是我试图从我的DemoProductBundle:Product Entity获取Twig中NULL的属性(id、名称等)。也许您需要使用
内部联接
?因为左联接可以在联接的表中返回空行。不幸的是,否,因为根据定义,此联接的表可能没有匹配项。我试着从“from”表中查看数据,这样就不应该有空值。听起来您需要在控制器或细枝中使用{%if entity is defined and entity not empty%}entity.property{%endif%}处理空值实体,或者使用相同的if语句检查属性。
 SELECT p, s FROM DemoProductBundle:Product p 
LEFT JOIN DemoProductBundle:SynchronizationSetting s WITH p.id = **s.idProduct** ORDER BY p.id ASC