postgresql - 使用 Doctrine2 和 Postgresql 的简单 DQL 请求

标签 postgresql symfony doctrine-orm doctrine doctrine-orm-postgres

我想在 Symfony2 FormType 中使用 query_builder,我尝试使用 Doctrine DQL 在我的 Postgresql shema 中发出一个简单的请求。它返回一个错误,我真的不明白如何解决它:

PDOException: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "" AS id0 FROM ""
LINE 1: SELECT "0_."id" AS id0 FROM "DATA_WAREHOUSE"."WindFarm" "0_

这是 Windfarm 类:

<?php

namespace Base\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * WindFarm
 *
 * @ORM\Table(name="""DATA_WAREHOUSE"".""WindFarm""")
 * @ORM\Entity(repositoryClass="Base\CoreBundle\Repository\WindFarmRepository")
 */
class WindFarm
{
    /**
     * @ORM\OneToMany(targetEntity="Base\CoreBundle\Entity\Turbine", mappedBy="windFarm")
     */
    private $turbines;

    /**
     * @var string $id
     *
     * @ORM\Column(name="""id""", type="string", length=32)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\SequenceGenerator(sequenceName="""DATA_WAREHOUSE"".""WindFarm_id_seq""", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="""name""", type="text", nullable=true)
     */
    private $name;

    /**
     * @var string $alias
     *
     * @ORM\Column(name="""alias""", type="string", length=32, nullable=true)
     */
    private $alias;

    /**
     * @var string $location
     *
     * @ORM\Column(name="""location""", type="text", nullable=true)
     */
    private $location;

    /**
     * @var string $sncNbr
     *
     * @ORM\Column(name="""sncNbr""", type="string", length=8, nullable=true)
     */
    private $sncNbr;
}

WindFarmRepository 中的 DQL 请求:

public function getWindFarmsAndTurbines(){

        $qb = $this->createQueryBuilder()
                   ->select('wft')
                   ->from('BaseCoreBundle:WindFarm', 'wft')
                   ->leftJoin('wft.turbines', 't')
                   ->addSelect('t')
                   ->orderBy('t.alias');

        return $qb;
    }

表单类型:

<?php

namespace Base\CoreBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Base\CoreBundle\Repository\WindFarmRepository;

class TurbineStatusCodeType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('exportBegin', 'date', array('input'  => 'datetime',
                                                   'widget' => 'single_text',
                                                   'format' => 'yyyy-MM-dd',))
                ->add('exportEnd', 'date', array('input'  => 'datetime',
                                                 'widget' => 'single_text',
                                                 'format' => 'yyyy-MM-dd',))
                ->add('arrayId', 'textarea')
                ->add('turbines', 'entity', array('class' => 'BaseCoreBundle:WindFarm',
                                                             'property' => 'name',
                                                             'multiple' => true,
                                                             'expanded' => true,
                                                   'query_builder' => function(WindFarmRepository $repo){
                                                    return $repo->getWindFarmsAndTurbines();
                                                   }))
                ->add('save', 'submit');
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'base_corebundle_turbineStatusCodeType';
    }
}

显然,问题出在引用中,我尝试了很多解决方案,但没有任何效果。

最佳答案

您所有的 Doctrine 注释都不正确,并且有额外的双引号。

@ORM\Column(name="""id""", type="string", length=32)

应该是

@ORM\Column(name="id", type="string", length=32)

等等

关于postgresql - 使用 Doctrine2 和 Postgresql 的简单 DQL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356788/

相关文章:

php - 左连接 ON 条件和 Doctrine 中的其他条件语法

symfony - FOSElasticaBundle 和 Doctrine Hydration

sql - PostgreSQL:如何将多列中的每个条目乘以固定数字?

php - Symfony2 : How to persist a new User in an admin-interface with FOSUserBundle?

python - 要使用 psycopg2 在 python 中使用部门名称显示部门 ID?

mysql - Symfony2 应用程序如何使用 MySQL 内存存储引擎?

symfony2 Doctrine 选择IFNULL

doctrine - 为什么 doctrine 2.0 不支持默认列值?

sql - 如何为 psql 添加内部查询

postgresql - 如何将 Odoo 旧二进制字段迁移到附件=True 的新 odoo 版本?