我想将一个对象从控制器传递给我的表单构建器,以便稍后可以将它用于我的ChoiceType字段。我该如何做到这一点?
这是我的控制者:
$choices = [];
$table2Repository = $this->getDoctrine()->getRepository('SwipeBundle:Company');
$table2Objects = $table2Repository->findAll();
foreach ($table2Objects as $table2Obj) {
$choices[$table2Obj->getId()] = $table2Obj->getId() . ' - ' . $table2Obj->getName();
}
$form = $this->createForm(SubAgentType::class, $choices, array(
'action'=>$this->generateUrl('backend_sub_agent_create'),
'method'=>'POST'
));
这是我的SubAgentType.php
class SubAgentType extends AbstractType {
protected $choices;
public function __construct (Choices $choices)
{
$this->choices = $choices;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('company_id', ChoiceType::class, array(
'mapped' => false,
'choices' => $choices,
));
问题是我收到以下错误。
捕获致命错误:参数1传递给 MyBundle \ Form \ SubAgentType :: __ construct()必须是。的实例 MyBundle \表格\选择,
答案 0 :(得分:0)
回答你的问题:
在services.yml文件中:
myform.type:
class: AppBundle\Form\MyFormType
arguments:
- '@doctrine.orm.entity_manager'
tags:
- { name: form.type }
你的MyFormType中的:
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* LicenseeType constructor.
*
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$choices = [];
$table2Repository = $this-em->getRepository('SwipeBundle:Company');
$table2Objects = $table2Repository->findAll();
foreach ($table2Objects as $table2Obj) {
$choices[$table2Obj->getId()] = $table2Obj->getId() . ' - ' . $table2Obj->getName();
}
...
但是,显然,如果您正确定义实体关系,则不需要这样做。
答案 1 :(得分:0)
尝试在SubAgentType中解析构造函数,$ choices不需要是Choices的子类型:
var aExcelData = [
{
"ProductId": "1239102",
"Name": "Power Projector 4713",
"Category": "Projector",
"SupplierName": "Titanium",
"Description": "A very powerful projector with special features for Internet usability, USB",
"WeightMeasure": 1467,
"WeightUnit": "g",
"Price": 856.49,
"CurrencyCode": "EUR",
"Status": "Available",
"Quantity": 3,
"UoM": "PC",
"Width": 51,
"Depth": 42,
"Height": 18,
"DimUnit": "cm"
},
{
"ProductId": "2212-121-828",
"Name": "Gladiator MX",
"Category": "Graphics Card",
"SupplierName": "Technocom",
"Description": "Gladiator MX: DDR2 RoHS 128MB Supporting 512MB Clock rate: 350 MHz Memory Clock: 533 MHz, Bus Type: PCI-Express, Memory Type: DDR2 Memory Bus: 32-bit Highlighted Features: DVI Out, TV Out , HDTV",
"WeightMeasure": 321,
"WeightUnit": "g",
"Price": 81.7,
"CurrencyCode": "EUR",
"Status": "Discontinued",
"Quantity": 10,
"UoM": "PC",
"Width": 34,
"Depth": 14,
"Height": 2,
"DimUnit": "cm",
}
];
// Consider above array of object as a dummy data
this.fnJSONToXLSXConvertor(aExcelData, <<put_here_xlsx_file_name>>);
}
fnJSONToXLSXConvertor : function(JSONData, ReportTitle) {
var aData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
if (aData.length) {
var aFinalXlsxData,
aXlsxHeaderData;
// Array variable to store header data in XLSX file
aXlsxHeaderData = [];
aFinalXlsxData = [];
// Below loop to extract header data
for ( var iIndex in aData[0]) {
switch (iIndex) {
case "ProductId":
aXlsxHeaderData.push(this.getText("ProductId"));
break;
case "Name":
aXlsxHeaderData.push(this.getText("Name"));
break;
...etc
<<specify/push your column header from i18n>>
}
// Adding column header data in final XLSX data
aFinalXlsxData.push(aXlsxHeaderData);
// Below loop to extract data
for (var i = 0; i < aData.length; i++) {
// Array variable to store content data in XLSX file
var aXlsxContentData = [];
for ( var iIndex in aData[i]) {
switch (iIndex) {
case "ProductId":
case "Name":
case "Status":
...
<<specify your column name, to extract only particular column data>>
aXlsxContentData.push(aData[i][iIndex]);
break;
}
}
// Adding content data in final XLSX data
aFinalXlsxData.push(aXlsxContentData);
}
var Workbook = function Workbook() {
if(!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
var wb = Workbook();
wb.SheetNames.push(ReportTitle);
var sheet_from_array_of_arrays = function sheet_from_array_of_arrays(data, opts) {
var ws = {};
var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
for(var R = 0; R != data.length; ++R) {
for(var C = 0; C != data[R].length; ++C) {
if(range.s.r > R) range.s.r = R;
if(range.s.c > C) range.s.c = C;
if(range.e.r < R) range.e.r = R;
if(range.e.c < C) range.e.c = C;
var cell = {v: data[R][C] };
if(cell.v == null) continue;
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});
if(typeof cell.v === 'number') cell.t = 'n';
else if(typeof cell.v === 'boolean') cell.t = 'b';
else if(cell.v instanceof Date) {
cell.t = 'n'; cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
}
else cell.t = 's';
ws[cell_ref] = cell;
}
}
if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}
var ws = sheet_from_array_of_arrays(aFinalXlsxData);
// Setting up Excel column width
ws['!cols'] = [
{wch:14},
{wch:12}
...
<<specify no. of character in cell to set column width if any>>
];
wb.Sheets[ReportTitle] = ws; // wb.Sheets[ReportTitle] -> To set sheet name
var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});
var s2ab = function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
};
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), ReportTitle + ".xlsx");
} else {
MessageBox.error(
"No data..!",
{
styleClass: bCompact? "sapUiSizeCompact" : ""
}
);
}
}
如果在控制器中注入一个数组,那么构造函数必须接受一个数组。