2015-06-17 33 views
0

即時通訊使用原則ODM + Zend表單並且遇到水分問題。 我使用像「storeName」這樣的元素和一個像「address」或「contactPerson」這樣的字段集來定義一個Collection「Stores」。 像「storeName」這樣的簡單元素將被很好地保存,但像「地址」這樣的字段總是會保存最後的入口數據。

所以我保存的JSON對象看起來像這樣

{ 
     "_id" : ObjectId("5541e8a50203ff4c1e00002a"), 
     ... 
     "stores" : [ 
      { 
       "_id" : "12345678998", 
       "storeName" : "test1", 
       "openingTimes" : "123", 
       "address" : { 
        "placeId" : "ChIJYbqFGmnKuEcRm-J84sWlfMY", 
        "street" : "Karolingerstraße 10", 
        "queryString" : "Karolingerstraße 10", ... 
       }, 
       "contactPerson" : { 
        "salutation" : "1", 
        "firstname" : "foo", 
        "lastname" : "bar", 
... 
       } 
      }, 
      { 
       "_id" : "557fe9a92d86d", 
       "storeName" : "test 3", 
       "openingTimes" : "hgfhgfhgf", 
       "address" : { 
        "placeId" : "ChIJYbqFGmnKuEcRm-J84sWlfMY", 
        "street" : "Karolingerstraße 10", 
        "queryString" : "Karolingerstraße 10", ... 
       }, 
       "contactPerson" : { 
        "salutation" : "1", 
        "firstname" : "foo", 
        "lastname" : "bar", 
... 
       } 
      } 
     ] 
    } 

我想通了,我的保溼基礎字段集CompanyStoresFieldset得到錯誤$數據水合物(數組$數據,$對象) 。 $對象是正確的。所以。它不是真正的水合作用錯誤,它的設置$形式的數據我認爲...但我不知道爲什麼。我只是將數據設置爲表格,如$ form-> setData($ form),我不會覆蓋任何表單方法。 所有其他Hydrators(在字段集)似乎得到正確的$數據,並正常工作。 有沒有人有想法?

所以這裏是我正在做的。 我的表單只需添加一個Fieldset CompanyStoresFieldset。 該字段集是「用作基本字段集」。此字段集都有一個收集要素,看起來像這樣:

CompanyStoresFieldset:

$this 
      ->setHydrator(new ClassMethods(false)) 
      ->setObject(new Company()) 
     ; 

$this->add(array(
      'type' => 'Zend\Form\Element\Collection', 
      'name' => 'stores', 
      'options' => array(
       'should_create_template' => true, 
       'allow_add' => true, 
       'allow_remove' => true, 
       'create_new_objects' => true, 
       'target_element' => array(
        'type' => 'Application\Form\Fieldset\StoreEntityFieldset', 
       ), 
      ), 
     )); 

StoreEntityFieldset增加了簡單的元素和字段集元素。

StoreEntityFieldset:

 $this 
      ->setHydrator(new ClassMethods(false)) 
      ->setObject(new Store()) 
     ; 


     $this->add(array(
      'name' => 'storeName', 
      ... 
     )); 

     //AddressFieldset 
     $this->add(array(
      'name' => 'address', 
      'type' => 'Application\Form\Fieldset\AddressFieldset', 
     )); 

     //ContactPersonFieldset 
     $this->add(array(
      'name' => 'contactPerson', 
      'type' => 'Application\Form\Fieldset\ContactPersonFieldset', 
     )); 

     $this->add(array(
      'name' => 'openingTimes', 
      'type' => 'Textarea', 
      ... 
     )); 

    } 

的形式和字段集我只設置的Zend \ STDLIB \保溼\ ClassMethods 我不修改任何Hydrators。

我的主文檔模型公司使嵌入$商店這樣

公司:

/** 
    * @var \Doctrine\Common\Collections\ArrayCollection 
    * @ODM\EmbedMany (targetDocument="\Application\Model\Mongo\Company\Store") 
    */ 
    private $stores = array(); 

我EmbeddedDocument商店添加元素這樣

商店:

class Store { 

    /** 
    * 
    * @var string 
    * @ODM\Id(strategy="NONE") 
    */ 
    private $id; 

    /** 
    * 
    * @var string 
    * @ODM\String 
    */ 
    private $storeName; 

    /** 
    * 
    * @var string 
    * @ODM\String 
    */ 
    private $openingTimes; 

    /** 
    * @var \Application\Model\Mongo\Company\Address 
    * @ODM\EmbedOne(targetDocument="\Application\Model\Mongo\Company\Address") 
    */ 
    private $address; 

    /** 
    * @var \Application\Model\Mongo\Company\ContactPerson 
    * @ODM\EmbedOne(targetDocument="\Application\Model\Mongo\Company\ContactPerson") 
    */ 
    private $contactPerson; 

模型聯繫人地址只包含類屬性setter和getter + getArrayCopy和exchangeArray。

getArrayCopy和exchangeArray看起來是這樣的:

public function getArrayCopy() { 
     $ref = new \Zend\Stdlib\Hydrator\Reflection(); 
     return $ref->extract($this); 
    } 

    public function exchangeArray($data = array()){ 
     $ref = new \Zend\Stdlib\Hydrator\Reflection(); 
     return $ref->hydrate($data, $this); 
    } 

回答

0

問題是我在商店模型構建添加新的實例爲ContactPerson和地址。這是錯誤的..