我正在使用实体字段类型查询生成器在下拉列表中仅显示这些不是父项的类型(父项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();
}
结果我没有错误,但是我的查询返回了所有结果,但是我说我需要在父项id==null的地方得到结果。什么是正确的查询?
最佳答案
获取实体的存储库$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');
关于php - 带有实体存储库和query_builder的实体字段类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25021493/