编辑:细分
一天记录两次或更多次
有日期实体和时间实体
日期实体oneToMany:时间实体。
时间实体manyToOne:日期实体。
表单字段由javascript添加。
到目前为止,这么好。它确实可行。
问题是类别是随时间注册的。
$form = $this->createForm(new DateType(), new Date());
结构现在看起来像这样。
DateFormType collection with TimeFormType
TimeFormType collection with CategoryFormType
TimeFormType collection with SubCategoryFormType
Category Entity oneToMany: Time Entity.
SubCategory Entity oneToMany: Time Entity.
Time Entity manyToOne: Category Entity.
Time Entity manyToOne: SubCategory Entity.
如果出现这种情况,控制器会发生表单绑定错误。
$form->bind($request); // => Error: Category, array given
Catchable Fatal Error: Argument 1 passed to ...\Entity\Time::setCategory() must be an instance of ...\Entity\Category, array given
通过“manyToOne”收集进行关系的“类别”吗? 如何使用“表格集”错了?
详情如下。
我想实现日复一日的录音时间 一天记录两次或更多次 表单字段由javascript添加 以下概述虽然很难解释它不会变长。
实体:
日期,类别,子类别,时间
Date.orm
Date:
oneToMany:
times:
targetEntity: Time
mappedBy: date
cascade: ["persist", "remove"]
Time.orm
Time:
// ...
manyToOne:
date:
targetEntity: Date
inversedBy: times
joinColumn:
name: date_id
referencedColumnName: id
category:
targetEntity: Category
inversedBy: times
joinColumn:
name: category_id
referencedColumnName: id
sub_category:
targetEntity: SubCategory
inversedBy: times
joinColumn:
name: sub_id
referencedColumnName: id
Category.orm
Category:
// ...
oneToMany:
times:
targetEntity: Time
mappedBy: categoy
cascade: ["persist", "remove"]
manyToMany:
subs:
targetEntity: SubCategory
inversedBy: main
joinTable:
name: main_and_sub
joinColumns:
joinColum:
name: main_id
referencedColumnName: id
onDelete: CASCADE
inverseJoinColumns:
joinColum:
name: sub_id
referencedColumnName: id
onDelete: CASCADE
SubCategory.orm
SubCategory:
// ...
oneToMany:
times:
targetEntity: Time
mappedBy: sub_category
cascade: ["persist", "remove"]
manyToMany:
main:
targetEntity: Category
mappedBy: subs
TimeType
// ...
$builder->add('category', 'collection', array(
'type' => new CategoryType(),
'options' => array(
'required' => true,
),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__cat_prot__'
));
$builder->add('subcategory', 'collection', array(
'type' => new SubCategoryType(),
'options' => array(
'required' => false,
),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__cat_prot__'
));
$builder->add('time', 'time', array(
'input' => 'datetime',
'widget' => 'choice',
));
DateType
// ...
$builder->add('date', 'date', array(
'input' => 'datetime',
'widget' => 'choice',
));
$builder->add('times', 'collection', array(
'type' => new TimeType(),
'options' => array(
'required' => true,
),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
'prototype_name' => '__times_prot__'
));
查看:
{{ form_widget(form.date) }}
<ul id="TIMES" data-prototype="
<div>
{{ form_widget(form.times.vars.prototype.children['category'].vars.prototype.name) | e }}
<label>Category 1</label>
</div>
<div>
{{ form_widget(form.times.vars.prototype.children['subcategory'].vars.prototype.name) | e }}
<label>Category 2</label>
</div>
<div>
{{ form_widget(form.times.vars.prototype.time) | e }}
</div>
<input type='button' value='Remove' class='remove' />">
</ul>
<input type="button" value="add times" class="add-form-field" />
的javascript
var nums = 0;
$(document).on('click', '.add-form-field', function(e){
var field = $("#TIMES"),
data = field.attr("data-prototype"),
widget = data.replace(/__cat_prot__/g, 0).replace(/__times_prot__/g, nums),
newLi = $('<li></li>').html(widget);
newLi.appendTo("#TIMES");
nums += 1;
return false;
});
$(document).on('click', '#TIMES .remove', function(){
$(this).parent().remove();
});
控制器:
$_date = new Date();
$form = $this->createForm(new DateType(), $_date);
if ($request->getMethod() == 'POST') {
$form->bind($request); // => Error: Category, array given
$形式 - &GT;绑定($请求); // =&gt;错误:类别,给定的数组
捕获致命错误:参数1传递给... \ Entity \ Time :: setCategory()必须是... \ Entity \ Category的实例,给定数组
如果在这种情况下,“$ form-&gt; bind($ request)”有一个错误“Category,array given”。
是不是设计得很好?
最好不要使用表单构建器吗?
日期有时间 时间有一个类别和一个子类别 时间必须有一个类别 时间没有子类别。 (不是必须可以为空)
表单数据
date[date][day]
date[times][0][category][0][name]
date[times][0][subcategory][0][name]
date[times][0][time][hour]
date[times][0][time][minute]
date[times][1][category][0][name]
date[times][1][subcategory][0][name]
date[times][1][time][hour]
date[times][1][time][minute]
我认为“date [times] [0] [category] [0] [name]”必须是“date [times] [0] [category] [name]”。
我认为整合表格的方式是错误的。