douzhanlie9209 2014-07-29 17:31
浏览 71
已采纳

具有实体存储库和query_builder的实体字段类型

I am using entity field type query_builder to show in drop down list only these types which aren't parents (parent_id == null). My ProductionType entity:

<?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 repository:

<?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;
    }
}

and my form builder method:

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')))
    ;
}

In result I have this error:

Class "RFQ\IronilBundle\Entity\ProductionTypeRepository" seems not to be a managed Doctrine entity. Did you forget to map it? 

I have spent a lot of hours for this, but I simply doesn't see why I have failed...

Thank you.

UPDATE

I just changed form builder drop down field code to:

    ->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')))

and repository method to:

public function findAllParents()
{
    return $this->_em->createQuery('SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id = null')
        ->getResult();
}

in result I don't have an error, but my query returns all results, but how I said I need to get results where parent_id==null. What will be correct query?

  • 写回答

1条回答 默认 最新

  • dsvbtgo639708 2014-07-29 17:57
    关注

    get repository of an entity

    $results = $this->getDoctrine() ->getRepository('RFQ\IronilBundle\Entity\ProductionType') ->findAllParents();

    is null

    Change from

    SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id = null

    to

    SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id IS NULL

    Same in findAllParents(): ->where('a.parent_id IS NULL');

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题