2013-02-22 59 views
2

我試圖拼命地用實體城市OneToMany Anagrafic保存表單。 我進入了「property_path」 CityType的形式和我返回錯誤,Symfony2預期的參數類型「對象或數組」,「字符串」給出

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

你有什麼錯誤? – cheesemacfly 2013-02-22 22:14:41

+2

這是一個測驗! 「我得到一個錯誤,並且不會顯示完整的代碼或錯誤點」 – Fusselchen 2013-02-22 22:21:29

+0

它是標題。 '預期的參數類型「對象或數組」,「字符串」給出了' – Lughino 2013-02-22 22:26:49

回答

0

貌似propertyPath期望一個對象或一個陣列。所以這條線是錯誤的:

$builder->add('comune', 'hidden', array('property_path' => 'city.id')) 

你在這裏通過city.id這不應該是一個字符串。我不是Symfony用戶,也不熟悉FormBuilder,因此您必須查找手冊中的如何使用property_path選項。

Btw。這與教條2沒有任何關係,所以刪除該標籤是合適的。

+0

事實是,如果你不使用「property_path」,我收到這個錯誤:'執行INSERT INTO城市(城市,郵編,region_id,state_id)時發生異常VALUES(?,?,?,?)'參數{「1」:「8094」,「2」:null,「3」:null,「4」:null}: SQLSTATE [23000]:完整性約束違規:1048'zip'列不能爲空'嘗試保存一個新的實體城市,但城市是已經預先定義的城市列表,並且城市一對多Anagrafic不應該創建一個新的實體! 有什麼不對? – Lughino 2013-02-22 23:19:40

相關問題