I want to use the query_builder in a Symfony2 FormType, I tried to make a simple request in my Postgresql shema using Doctrine DQL. It return me an error and i don't really understand how fix it :
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_
This is the Windfarm class :
<?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;
}
The DQL request in the WindFarmRepository:
public function getWindFarmsAndTurbines(){
$qb = $this->createQueryBuilder()
->select('wft')
->from('BaseCoreBundle:WindFarm', 'wft')
->leftJoin('wft.turbines', 't')
->addSelect('t')
->orderBy('t.alias');
return $qb;
}
The FormType :
<?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';
}
}
apparently, the problem is situated in the quote, I tried many solutions, but nothing work.
All of your Doctrine annotations are incorrect and have extra double-quotes.
@ORM\Column(name="""id""", type="string", length=32)
should be
@ORM\Column(name="id", type="string", length=32)
and so on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.