Search code examples
phpdoctrine-ormsymfony-2.5

Entity Field Type with Entity Repository and 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?


Solution

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