2014-11-04 94 views
0

我正在研究項目,實施與ZF2和Doctrine的ERP Web應用程序。ZF2 + Doctrine ORM - >對象類(類型)無法轉換爲字符串時持續

現在我有一個問題,當我嘗試堅持一個實體。

它失敗,此消息: 開捕致命錯誤:類應用程序\實體\範圍的對象無法轉換爲字符串... \供應商\原則\ ORM \ LIB \原則\ ORM \的UnitOfWork。 PHP的在線2791

我發現與此消息類似的問題,但答案沒有幫助我: Doctrine: Object of class User could not be converted to string

我試圖堅持實體產品具有關係ScopedName並且此實體與範圍實體有關係。

當我添加一個__toString()方法到範圍實體它保存一切正確,所以我猜這個問題是不是在我的形式或控制器。如果我的映射沒問題,我會猜測它是一個不支持正確組合主鍵的Doctrine Bug。正如你在下面看到的,我的實體ScopedName有一個組合鍵。

這些都是我的相關理論映射

範圍

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping 
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 
    <entity name="Application\Entity\Scope" table="application_scope">  
     <id name="id" type="integer"> 
      <generator strategy="AUTO"/> 
      <sequence-generator sequence-name="tablename_seq" allocation-size="100" initial-value="1" />    
     </id> 
     <field name="code" type="string" /> 
     <unique-constraints> 
      <unique-constraint columns="code" /> 
     </unique-constraints>     
    </entity> 
</doctrine-mapping> 

ScopedName

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping 
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 

    <entity name="Catalog\Entity\Product\ScopedName" table="catalog_product_scoped_name"> 
     <id name="scope" column="scope_id" association-key="true" />   
     <id name="product" column="product_id" association-key="true" />      
     <field name="value" type="string" />   
     <many-to-one field="scope" target-entity="Application\Entity\Scope"> 
      <join-column name="scope_id" referenced-column-name="id" on-delete="CASCADE" on-update="CASCADE" nullable="false" /> 
      <cascade> 
       <cascade-persist /> 
      </cascade>            
     </many-to-one> 
     <many-to-one field="product" target-entity="Catalog\Entity\Product" inversed-by="scopedNames" nullable="false"> 
      <join-column name="product_id" referenced-column-name="id" on-delete="CASCADE" on-update="CASCADE" /> 
      <cascade> 
       <cascade-persist /> 
      </cascade> 
     </many-to-one> 
    </entity> 
</doctrine-mapping> 

更新

難道是因爲我在我的範圍映射中沒有一對一的映射嗎?但我只需要一個單向關聯在這裏,因爲我產品實體會得到更多的協會,如ScopedSkuScopedDescription,......我不想定義他們都在我的範圍映射,因爲這另一個模塊,不應該知道的這一切......

更新2

好吧,我試圖添加一個一對多到另一側,但沒有幫助。有人知道我爲什麼會遇到這個錯誤嗎?

回答

0

對自己感到羞恥。這是我自己的失敗,我的控制器,對不起。

我的editAction看起來像這樣...註釋行是造成這個錯誤

public function editAction() {       
    $id = $this->params()->fromRoute('id'); 
    $form = $this->getServiceLocator()->get('FormElementManager')->get('\Catalog\Form\ProductForm');   
    $form->addAdditional(); 
    $product = $this->getEntityManager()->find('Catalog\Entity\Product', $id); 
    $productOld = clone($product);      
    $form->bind($product); 
    if($this->request->isPost()) { 
     $form->setData($this->request->getPost()); 

     if($form->isValid()) { 
      foreach($product->getScopedNames() as $scopedName) { 
       $scopedName->setProduct($product); 
       //next line was the failure   
       //$scopedName->setScope($this->getEntityManager()->getReference('Application\Entity\Scope', $scopedName->getScope())); 
       //$this->getEntityManager()->persist($scopedName); 
      } 
      foreach($productOld->getScopedNames() as $scopedName) { 
       if(!$product->getScopedNames()->contains($scopedName)) { 
        $this->getEntityManager()->remove($scopedName);      
       } 
      }    
      $this->getEntityManager()->persist($product); 
      $this->getEntityManager()->flush(); 
      $this->flashMessenger()->addSuccessMessage("Product has been saved successfully."); 
      $this->redirect()->toRoute('catalog/product');               
     } else { 
      $this->flashMessenger()->addErrorMessage("Form invalid"); 
     }    
    } else { 
     $form->bind($product); 
    }    

    return array(
     'form' => $form, 
     'id' => $id 
    ); 
} 

我需要這些行之前,我利用了DoctrineObject保溼的。現在它自動工作。但是,爲了將產品本身設置爲關係並刪除新的發佈請求中未包含的舊關係,我仍然需要前一行和下一個foreach。

也許有人對此有更好的解決方案?

相關問題