[英]Array to String conversion error in Symfony2
I have a form from which i am fetching the data as array in controller from $request->get()
method and want to submit that data to database.. 我有一种形式,可以从
$request->get()
方法中将数据作为控制器中的数组获取,并希望将该数据提交到数据库。
This is the array i am getting when i print the post variable: 这是我打印post变量时得到的数组:
Array
(
[country] => Array
(
[0] => 24
[1] => 4
)
[state] => Array
(
[0] => Cuando Cubango
[1] => Badakhshan
)
[activity] => Array
(
[0] => 3
[1] => 5
)
[activity_0] => Array
(
[0] => 3
)
)
I want them to insert in database in this format: 我希望他们以这种格式插入数据库:
country state activity
24 Cuando Cubango 3
24 Cuando Cubango 5
4 Badakhshan 3
What i have tried so far is this: 到目前为止,我尝试过的是:
$active = $request->get('activity');
if (!empty($active)) {
foreach ($active as $activity) {
$states = $request->get('state');
$c = $request->get('country');
$Bidactivity = $active;
foreach ($states as $state) {
foreach ($Bidactivity as $orgact) {
$activity_to_db = new Activity();
$activity_to_db->setActivityid($active);
$activity_to_db->setState($states);
$activity_to_db->setBidOrganiser($organiserDetails);
$activity_to_db->setCountry($c);
$em->persist($activity_to_db);
$em->flush();
}
}
}
}
But i am getting the array to string conversion error. 但是我正在将数组转换为字符串转换错误。 Please guide.
请指导。
You get this error, because you pass an array to every field of your activity: 您会收到此错误,因为您将数组传递给活动的每个字段:
$activity_to_db->setActivityid($active); /* is this [activity] => Array
(
[0] => 3
[1] => 5
)*/
$activity_to_db->setState($states); /* is this [state] => Array
(
[0] => Cuando Cubango
[1] => Badakhshan
)*/
$activity_to_db->setBidOrganiser($organiserDetails); // doesn't occure, so whats that
$activity_to_db->setCountry($c); /* is this [country] => Array
(
[0] => 24
[1] => 4
)*/
So anywhere there is the error. 所以任何地方都有错误。
Loop between the array with the highest number of elements as $active = $request->get('activity');
在元素数量最多的数组之间循环:
$active = $request->get('activity');
Try the smt like 尝试像
$states = $request->get('state');
$c = $request->get('country');
if (!empty($active)) {
foreach ($c as $index=> $country) {
foreach ($states as $state) {
foreach ($active as $activity) {
$activity_to_db = new Activity();
$activity_to_db->setActivityid($activity);
$activity_to_db->setState($state);
$activity_to_db->setCountry($country);
$em->persist($activity_to_db);
}
}
}
}
$em->flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.