构建表单与单表继承 [英] Build Form with Single Table Inheritance
问题描述
我有我的实体文章和一个单独的表继承如下:
I have my entity Article and one single table inheritance like this :
/**
* Article
*
* @ORM\Table(name="article")
* @ORM\Entity(repositoryClass="PM\PlatformBundle\Repository\ArticleRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="media", type="string")
* @ORM\DiscriminatorMap({"article" = "Article", "movie" = "Movie", "image" = "Image", "text" = "Text"})
*/
class Article
{
protected $id;
protected $title;
protected $description;
protected $author;
//other attributes and setters getters
}
class Image extends Article
{
private $path;
//getter setter
}
class Movie extends Article
{
private $url;
//getter setter
}
所以我的文章的对象类型是Image或电影或文字。好的,现在我想建立一个表单,用户可以发布一个新的文章:在这种形式,用户必须选择树型(3无线电按钮):图像或电影或文本,当然还有其他领域:标题和描述。我该怎么办?因为使用命令
So my article's object type is either Image or movie or text only. Ok now I would like build a form wherein users can post a new article : in this form, the user has to choice between tree type (3 radios button) : image OR movie OR text only and of course the other fields : title and description. How I can do that ? Because with the command
php bin / console doctrine:generate:form myBundle:Article
php bin/console doctrine:generate:form myBundle:Article
呈现的表单为:
class ArticleType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('description', TextareaType::class)
->add('save', SubmitType::class);
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PM\PlatformBundle\Entity\Article'
));
}
}
我不知道实施我的STI的方式这种形式的关系。因为我没有在我的文章实体/对象中的字段(仅在我的表中)。
我必须添加一个Custom ChoiceType()字段,但它需要一个属性。
当我尝试以下列形式添加它:
I don't know the way to implement my STI relation in this form. Because I have not field in my Article entity/object for the type (only in my table). I have to add a Custom ChoiceType() field but it require a attribute. When I try to add this in the form :
->add('path', SearchType::class)
->add('url', UrlType::class)
我收到这个错误:
属性path也不是方法getPath()之一,path(),isPath(),hasPath(),__get()存在,并在类PM\PlatformBundle\Entity\Article中具有公共访问权限。
Neither the property "path" nor one of the methods "getPath()", "path()", "isPath()", "hasPath()", "__get()" exist and have public access in class "PM\PlatformBundle\Entity\Article".
因为我创建了一个Article的实例,而不是Image或Movie的实例。最初我创造了一个STI思维文章的新例子,也允许我定义文章的类型。但不是 ?
Because I have create an instance of Article, not an instance of Image or Movie. Initially I created a STI thinking a new instance of Article would allow me also to define the "type" of article. But not ? Right ?
推荐答案
您将需要制作三种表格(一种用于文章
,一个用于 Movie
,另一个用于 Image
)。然后,在你的控制器中你必须选择处理它们:
You will have to make three forms (one for an Article
, one for a Movie
and one for an Image
). Then, in your controller, you have to options to deal with them:
- 你可以使用一个动作来处理三种形式通过使用
$ form-> isSubmitted()
) - 通过表单创建一个操作,并且将每个表单的表单操作网址设置为正确的控制器。
- Either you use one action to handle the three forms (you can check wich one is submitted by using
$form->isSubmitted()
) - You create one action by form, and set the form action URL for each form to the correct controller.
最后,在您的模板中,将表单封装在div中,并使用我上一篇文章中的示例。
Finally, in your template, you encapsulate your forms in a div, and use the example in my previous post.
{% extends "CoreBundle::layout.html.twig" %}
{% block title %}{{ parent() }}{% endblock %}
{% block btn_scrollspy %}
{% endblock %}
{% block bundle_body %}
<div class="well">
<div class="selector">
<input type="radio" name="form-selector" value="article-form"> Article
<input type="radio" name="form-selector" value="movie-form"> Movie
<input type="radio" name="form-selector" value="image-form"> Image
</div>
<div class="form article-form" style="display: none;">
{{ form(articleForm) }}
</div>
<div class="form movie-form" style="display: none;">
{{ form(movieForm) }}
</div>
<div class="form image-form" style="display: none;">
{{ form(imageForm) }}
</div>
</div>
{% endblock %}
这篇关于构建表单与单表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!