繁体   English   中英

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

并为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.b3rn4rd的答案是最好的。 如果条件取决于当前工具,那么您可能希望使用表单事件侦听器。 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);
                },
            )
        );
    });
}

暂无
暂无

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

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