简体   繁体   English

Symfony2 - 从实体访问存储库

[英]Symfony2 - Access of the Repository from Entity

I'm trying to learn Symfony, and I found it don't access the doctrine from the entity. 我正在努力学习Symfony,我发现它不能从实体中获取学说。

I created an Entity 我创建了一个实体

<?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 并为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';
    }
}

How can I get just the parents in the $builder->add('parent') (dropdown select) and not all the data. 如何在$ builder-> add('parent') (下拉选择)中获取父项,而不是所有数据。

Thanks in advance. 提前致谢。

You don't need to access repository from an entity class. 您不需要从实体类访问存储库。 Entities should be plain php objects. 实体应该是纯PHP对象。 If you want to limit options in your dropdown use query_builder property. 如果要限制下拉列表中的选项,请使用query_builder属性。

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

It depends on your case. 这取决于你的情况。 If your filter criteria is fixed, then @b.b3rn4rd's answer is best. 如果你的过滤条件是固定的,那么@ b.b3rn4rd的答案是最好的。 If the criteria depends on the current facility then you probably want to use a form event listener. 如果条件取决于当前工具,那么您可能希望使用表单事件侦听器。 Symfony forms have a fairly powerful architecture, where a single form type is created and then cloned for each instance. Symfony表单具有相当强大的体系结构,其中创建单个表单类型,然后为每个实例克隆。 The buildForm() method is only called once for each form even if the field type is repeated multiple times on the page. 即使字段类型在页面上重复多次, buildForm()方法也只为每个表单调用一次。 In some cases the field is only rendered once, and that is fine but the safest way is to use a form listener. 在某些情况下,字段只渲染一次,这很好,但最安全的方法是使用表单侦听器。 Rather than adding a generic parent field to the form builder, you are adding a specific field to the form instance. 您不是将通用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);
                },
            )
        );
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM