我使用实体字段类型query_builder只在下拉列表中显示这些不是父类型(parent_id == null)的类型。我的ProductionType实体:
<?php
namespace RFQ\IronilBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ProductionType
*
* @ORM\Table(name="production_type")
* @ORM\Entity(repositoryClass="RFQ\IronilBundle\Entity\ProductionTypeRepository")
*/
class ProductionType
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="ProductionType", mappedBy="parent")
**/
protected $children;
/**
* @ORM\ManyToOne(targetEntity="ProductionType", inversedBy="children")
**/
protected $parent;
// setters, getters and constructors...
ProductionType存储库:
<?php
namespace RFQ\IronilBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* ProductionTypeRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ProductionTypeRepository extends EntityRepository
{
public function findAllParents($name)
{
$query = $this->createQueryBuilder('a')
->from('RFQIronilBundle:ProductionType', 'a')
->where('a.parent_id = null');
return $query;
}
}
和我的表单构建器方法:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null, array('label' => 'Name', 'attr' => array('class'=>'form-control login-input', 'placeholder'=>'Name')))
->add('parent', 'entity', array('label' => 'Parent',
'class' => 'RFQ\IronilBundle\Entity\ProductionTypeRepository',
'query_builder' => function(EntityRepository $er) {
return $er->queryOwnedBy($name);},
'attr' => array('class'=>'form-control login-input')))
;
}
结果我有这个错误:
Class "RFQ\IronilBundle\Entity\ProductionTypeRepository" seems not to be a managed Doctrine entity. Did you forget to map it?
我花了很多时间来做这件事,但我根本不知道为什么我失败了......
谢谢。
更新
我刚刚将表单构建器下拉字段代码更改为:
->add('parent', 'entity', array('label' => 'Parent',
'class' => 'RFQ\IronilBundle\Entity\ProductionType',
'query_builder' => function(ProductionTypeRepository $repository) {
return $repository->createQueryBuilder('s')->orderBy('s.id', 'ASC');},
'attr' => array('class'=>'form-control login-input')))
和存储库方法:
public function findAllParents()
{
return $this->_em->createQuery('SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id = null')
->getResult();
}
结果我没有错误,但我的查询返回所有结果,但我怎么说我需要得到parent_id == null的结果。什么是正确的查询?
答案 0 :(得分:1)
获取实体的存储库
$results = $this->getDoctrine()
->getRepository('RFQ\IronilBundle\Entity\ProductionType')
->findAllParents();
为空
从
改变 SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id = null
到
SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id IS NULL
findAllParents()
:->where('a.parent_id IS NULL');