警告:非法字符串偏移量'city'Symfony表单搜索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了警告:非法字符串偏移量'city'Symfony表单搜索相关的知识,希望对你有一定的参考价值。
发送搜索表单时出现问题。
交响乐说
警告:非法字符串偏移量'city'
这是我处理表单的功能
public function searchTeam($option)
{
return $this->createQueryBuilder('t') // t = team
->leftJoin('t.cities','city')
->where('city.name = :cityName')
->setParameter("cityName", $option['city']->getName())
->andWhere('t.name = :name')
->setParameter("name", $option['name'])
->getQuery()
->getResult();
}
和我的TeamController:
$formSearchTeam = $this->createForm(SearchTeamType::class, $team);
$formSearchTeam->handleRequest($request);
if($formSearchTeam->isSubmitted() && $formSearchTeam->isValid()) {
$option = $formSearchTeam->getData()->getName();
$teamSearch = $repo->searchTeam($option);
dd($teamSearch);
}
我将dd($option)
用于查看要发送的表单的数据,然后恢复了该数据:
TeamController.php on line 56:
AppEntityTeam {#1068 ▼
-id: null
-name: "New"
-picture: null
-home_team: DoctrineCommonCollectionsArrayCollection {#1071 ▶}
-outside_team: DoctrineCommonCollectionsArrayCollection {#1085 ▶}
-city: AppEntityCity {#1095 ▼
-id: 7
-name: "Paris"
-zip_code: 75000
-teams: DoctrineORMPersistentCollection {#1093 ▼
-snapshot: []
-owner: AppEntityCity {#1095}
-association: array:15 [ …15]
-em: DoctrineORMEntityManager {#490 …11}
-backRefFieldName: "city"
-typeClass: DoctrineORMMappingClassMetadata {#801 …}
-isDirty: false
#collection: DoctrineCommonCollectionsArrayCollection {#1170 ▶}
#initialized: false
}
}
-team_asking: DoctrineCommonCollectionsArrayCollection {#1129 ▶}
-accepting_team: DoctrineCommonCollectionsArrayCollection {#1125 ▶}
-Team_lead: null
-User: DoctrineCommonCollectionsArrayCollection {#1080 ▶}
}
答案
我发现我的错误,
城市是一个对象,而不是数组。在我的代码中,我更改了
->setParameter("cityName", $option['city']->getName())
那个:
->setParameter("cityName", $option->getCity()->getName())
以上是关于警告:非法字符串偏移量'city'Symfony表单搜索的主要内容,如果未能解决你的问题,请参考以下文章
Drupal 8 警告:非法字符串偏移 'name' DrupalBootstrapStyles.php