我正在拼命地用实体City OneToMany Anagrafic保存表格。 我在表单中输入了“property_path”CityType,然后返回错误,
Expected argument of type “object or array”, “string” given
我不明白我做错了什么!
class Anagrafic
{
/**
* @ORM\ManyToOne(targetEntity="City", inversedBy="anagrafics", cascade={"persist"})
* @ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
//..
//..
class City
{
/**
* @ORM\OneToMany(targetEntity="Anagrafic", mappedBy="city", cascade={"persist"})
*/
private $anagrafics;
//...
//...
class CityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('comune', 'hidden', array('property_path' => 'city.id'))
//..
//..
class AnagraficType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('city', new CityType())
编辑:对不完整的信息感到抱歉,这是个例外:
CRITICAL - Symfony\Component\Form\Exception\UnexpectedTypeException:
Expected argument of type "object or array", "string" given (uncaught exception) at
/var/www/MyBusiness0_1/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php line 342
/var/www/MyBusiness0_1/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php at line 342
for ($i = 0; $i <= $lastIndex; ++$i) {
if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
throw new UnexpectedTypeException($objectOrArray, 'object or array');
}
$property = $this->elements[$i];
答案 0 :(得分:2)
问题在于,当你传递字符串或数字时,symfony需要一个对象,所以我们需要实现一个DataTransformer来将一个字符串转换为一个对象,反之亦然。
http://symfony.com/doc/master/cookbook/form/data_transformers.html
问题解决了! ; - )
答案 1 :(得分:0)
看起来propertyPath
需要一个对象或一个数组。所以这一行是错误的:
$builder->add('comune', 'hidden', array('property_path' => 'city.id'))
你在这里传递city.id
,那不应该是一个字符串。我不是Symfony用户,不熟悉FormBuilder,所以你必须查阅手册如何使用property_path
选项。
顺便说一下。这与学说2无关,所以删除该标签是合适的。