繁体   English   中英

Symfony从表单类型的实体中创建选择

[英]Symfony creating choice from entity in form type

我在数据库中有很多类别。

这是类别实体

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="categories")
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Category")
     */
    protected $rootCategory;

    /**
     * @ORM\Column(type="text")
     */
    protected $name;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set rootCategory
     *
     * @param \AppBundle\Entity\Category $rootCategory
     *
     * @return Category
     */
    public function setRootCategory(\AppBundle\Entity\Category $rootCategory = null)
    {
        $this->rootCategory = $rootCategory;

        return $this;
    }

    /**
     * Get rootCategory
     *
     * @return \AppBundle\Entity\Category
     */
    public function getRootCategory()
    {
        return $this->rootCategory;
    }
}

我想在我的编辑表格中获得所有类别

EditFormType:

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use AppBundle\Controller\CategoryController;

class EditPhotoFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $categoryController = new CategoryController();

        $builder->add('title', 'text');
    $builder->add('description', 'textarea');
        $builder->add('category', EntityType::class, array(
            'class' => 'AppBundle:Category',
            'choices' => $categoryController->getCategories(),
        ));
    }

    public function getName()
    {
        return 'app_photo_edit';
    }
}

getCategories() 

public function getCategories() {
        $em = $this->getDoctrine()->getManager();

    return $em->getRepository('AppBundle:Category')->findAll();
    }

我收到下一个错误:

错误:在null上调用成员函数has()

那是因为控制器对象中没有主义。 在这种情况下,我应该从哪里获得学说和存储库?
我应该如何正确地做呢?

首先,您永远不要自己实例化任何Controller类。 Symfony的内核使用控制器类来处理请求,并自动将它们与依赖项一起加载。

在这里,您甚至不需要FormType中的EntityManager,因为EntityType具有内置选项query_builder来执行您需要的操作:

$builder->add('category', EntityType::class, array(
    'class' => 'AppBundle:Category',
    'query_builder' => function (EntityRepository $er) {
         return $er->createQueryBuilder('c');
    },
);

这应该可以解决问题。 在这里查看更多详细信息)

但是,如果有一天您真的需要在Form内导入依赖项(无论是EntityManager还是其他服务),则应按以下步骤操作:

A.在您的构造函数中导入给定的依赖项:

private $dependency;

public function __construct(Dependency $dependency)
{
     $this->$dependency = $dependency;
}

B.以依赖项的id作为参数声明您的表单即服务:

<service id="app.form.type.edit_photo"
         class="AppBundle\Form\Type\EditPhotoFormType">
    <tag name="form.type" />
    <argument type="service" id="app.dependencies.your_dependency" />
</service>

然后在需要的地方在表单中使用$this->dependency

希望这可以帮助! :)

暂无
暂无

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

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