Php Symfony从表单类型中的实体创建选项
我在数据库中有很多类别 这里是类别实体Php Symfony从表单类型中的实体创建选项,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我在数据库中有很多类别 这里是类别实体 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\Colu
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();
}
我得到下一个错误:
错误:对成员函数的调用具有空值的()
这是因为控制器对象中没有原则。在这种情况下,我应该在哪里获得条令和存储库?我应该如何正确地执行它?首先,您应该不要自己实例化任何控制器类。Symfony的内核使用控制器类来处理请求,并且会自动加载控制器类以实现此目的 在这里,您甚至不需要在FormType中使用EntityManager,因为
EntityType
有一个内置选项query\u builder
来执行您需要的操作:
$builder->add('category', EntityType::class, array(
'class' => 'AppBundle:Category',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c');
},
);
这应该能奏效。(查看更多详细信息)
但是,如果有一天您真的需要在表单中导入依赖项(无论是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
希望这有帮助!:) 尝试使用query\u builder,获得错误预期参数类型为“条令\ORM\QueryBuilder”,“数组”。我编辑了我的答案,您必须在
query\u builder
可捕获致命错误:类AppBundle\Entity\Category的对象无法转换为字符串。我仍然不知道如何让它工作……现在的问题是Symfony不知道如何在选择中显示您的类别。在Category(类别)中定义\u toString
方法,或在EntityType(cf)中使用optionchoice\u label
。无论如何,您似乎在表单方面遇到了很多困难,因此我建议您阅读表单和EntityType的完整文档,这样做只会让您做得更好。