Symfony2 -从实体访问存储库


Symfony2 - Access of the Repository from Entity

我正在尝试学习Symfony,我发现它不能从实体访问学说。

我创建了一个实体

<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
 * Facility
 *
 * @ORM'Table()
 * @ORM'Entity
 * @ORM'Entity(repositoryClass="AppBundle'Entity'FacilityRepository")
 */
class Facility
{
    /**
     * @var integer
     *
     * @ORM'Column(name="id", type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORM'Column(name="label", type="string", length=200)
     */
    private $label;
    /**
     * @ORM'OneToOne(targetEntity="Facility")
     * @ORM'JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set label
     *
     * @param string $label
     * @return Facility
     */
    public function setLabel($label)
    {
        $this->label = $label;
        return $this;
    }
    /**
     * Get label
     *
     * @return string
     */
    public function getLabel()
    {
        return $this->label;
    }
    /**
     * Set parent
     *
     * @param 'AppBundle'Entity'Facility $parent
     * @return Facility
     */
    public function setParent('AppBundle'Entity'Facility $parent = null)
    {
        $this->parent = $parent;
        return $this;
    }
    /**
     * Get parent
     *
     * @return 'AppBundle'Entity'Facility
     */
    public function getParent()
    {
        return $this->parent;
    }
    public function __toString()
    {
        return $this->label;
    }
}

and for FacilityType.php

<?php
namespace AppBundle'Form;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolverInterface;
class FacilityType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('parent')
            ->add('label')
        ;
    }
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle'Entity'Facility'
        ));
    }
    /**
     * @return string
     */
    public function getName()
    {
        return 'appbundle_facility';
    }
}

我怎么能得到只是父母在$builder->add('parent')(下拉选择),而不是所有的数据。

您不需要从实体类访问存储库。实体应该是普通的php对象。如果你想限制下拉菜单中的选项,请使用query_builder属性。

$builder->add('parent', 'entity', array(
    'class' => 'AppBundle:Facility',
    'query_builder' => function (EntityRepository $er) {
        return $er->createQueryBuilder('f')
            ->where('/* ... */');
    },
));

这取决于你的情况。如果您的过滤条件是固定的,那么@b。第三个答案是最好的。如果条件取决于当前的设施,那么您可能希望使用表单事件侦听器。Symfony表单具有相当强大的体系结构,其中创建单个表单类型,然后为每个实例克隆。即使字段类型在页面上重复多次,buildForm()方法也只对每个表单调用一次。在某些情况下,字段只呈现一次,这很好,但最安全的方法是使用表单侦听器。而不是添加一个通用的parent字段到表单生成器,您添加一个特定的字段到表单实例。

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('label');
    $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
        $form = $event->getForm();
        /** @var Facility $facility */
        $facility = $event->getData();
        $form->add(
            'parent',
            'entity',
            array(
                'class' => 'AppBundle:Facility',
                'query_builder' => function (FacilityRepository $repository) use ($facility) {
                    return $repository->createQueryBuilder('f')->findParents($facility);
                },
            )
        );
    });
}