Тип поля сущности с репозиторием сущностей и query_builder

Я использую тип поля сущности 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. Каким будет правильный запрос?


person RydelHouse    schedule 29.07.2014    source источник


Ответы (1)


получить репозиторий объекта

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

нуль

Меняться от

SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id = null

to

SELECT * FROM RFQIronilBundle:ProductionType WHERE parent_id IS NULL

То же самое в findAllParents(): ->where('a.parent_id IS NULL');

person Jens A. Koch    schedule 29.07.2014
comment
Ты был прав! Большое спасибо! Вы сэкономили мое время! - person RydelHouse; 29.07.2014