2016-10-12 11 views
1

私は次のエラーを取得する:期待値「SourcingBundle エンティティ RequestForEstimate」

Expected value of type "SourcingBundle\Entity\RequestForEstimate" for association field "SourcingBundle\Entity\RequestForEstimateDetail#$detail", got "Doctrine\Common\Collections\ArrayCollection" instead.

私は複数のRequestForEstimateDetailsノードを有することができるRequestForEstimate実体がある構造を作成しようとしています。 RequestForEstimateDetails内の各行は、RequestForEstimateが共通情報のみの場合、異なる製品とその詳細の要求を持つだけです。私はSymfonyを初めて使っています。私はDB内の子供たちと一緒に親要素を保存したいところで苦労しています。私はこのsymfonyのチュートリアルを次されています。ここではHow to Embed a Collection of Forms

は私のコードです:

SourcingBundle \エンティティ\ RequestForEstimate:

<?php 

namespace SourcingBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* RequestForEstimate 
* 
* @ORM\Table(name="request_for_estimate") 
* @ORM\Entity(repositoryClass="SourcingBundle\Repository\RequestForEstimateRepository") 
*/ 
class RequestForEstimate 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="request_id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 

    private $id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="status", type="integer") 
    */ 
    private $status; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="create_time", type="datetime") 
    */ 
    private $createTime; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="update_time", type="datetime") 
    */ 
    private $updateTime; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $name; 
    /** 
    * @ORM\OneToMany(targetEntity="RequestForEstimateDetail", mappedBy="detail", cascade={"persist", "remove"}, orphanRemoval=true) 
    */ 
    private $details; 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->details = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set status 
    * 
    * @param integer $status 
    * 
    * @return RequestForEstimate 
    */ 
    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    /** 
    * Get status 
    * 
    * @return integer 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set createTime 
    * 
    * @param \DateTime $createTime 
    * 
    * @return RequestForEstimate 
    */ 
    public function setCreateTime($createTime) 
    { 
     $this->createTime = $createTime; 

     return $this; 
    } 

    /** 
    * Get createTime 
    * 
    * @return \DateTime 
    */ 
    public function getCreateTime() 
    { 
     return $this->createTime; 
    } 

    /** 
    * Set updateTime 
    * 
    * @param \DateTime $updateTime 
    * 
    * @return RequestForEstimate 
    */ 
    public function setUpdateTime($updateTime) 
    { 
     $this->updateTime = $updateTime; 

     return $this; 
    } 

    /** 
    * Get updateTime 
    * 
    * @return \DateTime 
    */ 
    public function getUpdateTime() 
    { 
     return $this->updateTime; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return RequestForEstimate 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Add detail 
    * 
    * @param \SourcingBundle\Entity\RequestForEstimateDetail $detail 
    * 
    * @return RequestForEstimate 
    */ 
    public function addDetail(\SourcingBundle\Entity\RequestForEstimateDetail $detail) 
    { 
     $this->details[] = $detail; 
     return $this; 
    } 

    /** 
    * Remove detail 
    * 
    * @param \SourcingBundle\Entity\RequestForEstimateDetail $detail 
    */ 
    public function removeDetail(\SourcingBundle\Entity\RequestForEstimateDetail $detail) 
    { 
     $this->details->removeElement($detail); 
    } 

    /** 
    * Get details 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getDetails() 
    { 
     return $this->details; 
    } 

} 

SourcingBundle \エンティティ\ RequestForEstimateDetail:

<?php 

namespace SourcingBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* RequestForEstimateDetail 
* 
* @ORM\Table(name="request_for_estimate_detail") 
* @ORM\Entity(repositoryClass="SourcingBundle\Repository\RequestForEstimateDetailRepository") 
*/ 
class RequestForEstimateDetail 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="request_detail_id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 
    /** 
    * @ORM\ManyToOne(targetEntity="RequestForEstimate", inversedBy="details") 
    * @ORM\JoinColumn(name="request_id", referencedColumnName="request_id") 
    */ 
    private $detail; 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="product_id", type="integer") 
    */ 
    private $productId; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="quantity", type="integer") 
    */ 
    private $quantity; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="price_per_unit", type="decimal", precision=2, scale=0) 
    */ 
    private $pricePerUnit; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="shipping_cost", type="decimal", precision=2, scale=0) 
    */ 
    private $shippingCost; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="other_fees", type="decimal", precision=2, scale=0) 
    */ 
    private $otherFees; 


    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set product 
    * 
    * @param integer $product 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setProduct($product) 
    { 
     $this->product = $product; 

     return $this; 
    } 

    /** 
    * Get product 
    * 
    * @return int 
    */ 
    public function getProduct() 
    { 
     return $this->product; 
    } 

    /** 
    * Set quantity 
    * 
    * @param integer $quantity 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setQuantity($quantity) 
    { 
     $this->quantity = $quantity; 

     return $this; 
    } 

    /** 
    * Get quantity 
    * 
    * @return int 
    */ 
    public function getQuantity() 
    { 
     return $this->quantity; 
    } 

    /** 
    * Set pricePerUnit 
    * 
    * @param string $pricePerUnit 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setPricePerUnit($pricePerUnit) 
    { 
     $this->pricePerUnit = $pricePerUnit; 

     return $this; 
    } 

    /** 
    * Get pricePerUnit 
    * 
    * @return string 
    */ 
    public function getPricePerUnit() 
    { 
     return $this->pricePerUnit; 
    } 

    /** 
    * Set shippingCost 
    * 
    * @param string $shippingCost 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setShippingCost($shippingCost) 
    { 
     $this->shippingCost = $shippingCost; 

     return $this; 
    } 

    /** 
    * Get shippingCost 
    * 
    * @return string 
    */ 
    public function getShippingCost() 
    { 
     return $this->shippingCost; 
    } 

    /** 
    * Set otherFees 
    * 
    * @param string $otherFees 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setOtherFees($otherFees) 
    { 
     $this->otherFees = $otherFees; 

     return $this; 
    } 

    /** 
    * Get otherFees 
    * 
    * @return string 
    */ 
    public function getOtherFees() 
    { 
     return $this->otherFees; 
    } 

    /** 
    * Set requestId 
    * 
    * @param \SourcingBundle\Entity\RequestForEstimate $requestId 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setRequestId(\SourcingBundle\Entity\RequestForEstimate $requestId = null) 
    { 
     $this->requestId = $requestId; 

     return $this; 
    } 

    /** 
    * Get requestId 
    * 
    * @return \SourcingBundle\Entity\RequestForEstimate 
    */ 
    public function getRequestId() 
    { 
     return $this->requestId; 
    } 

    /** 
    * Set productId 
    * 
    * @param \SourcingBundle\Entity\Product $productId 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setProductId(\SourcingBundle\Entity\Product $productId = null) 
    { 
     $this->productId = $productId; 

     return $this; 
    } 

    /** 
    * Get productId 
    * 
    * @return \SourcingBundle\Entity\Product 
    */ 
    public function getProductId() 
    { 
     return $this->productId; 
    } 

    /** 
    * Set detail 
    * 
    * @param \SourcingBundle\Entity\RequestForEstimate $detail 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function setDetail(\SourcingBundle\Entity\RequestForEstimate $detail = null) 
    { 
     $this->detail = $detail; 

     return $this; 
    } 

    /** 
    * Get detail 
    * 
    * @return \SourcingBundle\Entity\RequestForEstimate 
    */ 
    public function getDetail() 
    { 
     return $this->detail; 
    } 
    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->detail = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Add detail 
    * 
    * @param \SourcingBundle\Entity\RequestForEstimate $detail 
    * 
    * @return RequestForEstimateDetail 
    */ 
    public function addDetail(\SourcingBundle\Entity\RequestForEstimate $detail) 
    { 
     $this->detail[] = $detail; 

     return $this; 
    } 

    /** 
    * Remove detail 
    * 
    * @param \SourcingBundle\Entity\RequestForEstimate $detail 
    */ 
    public function removeDetail(\SourcingBundle\Entity\RequestForEstimate $detail) 
    { 
     $this->detail->removeElement($detail); 
    } 
} 

私のコントローラ:

<?php 

namespace SourcingBundle\Controller; 

use SourcingBundle\Entity\RequestForEstimate; 
use SourcingBundle\Entity\RequestForEstimateDetail; 
use SourcingBundle\Form\Type\RequestForEstimateType; 
use SourcingBundle\Form\Type\RequestForEstimateDetailType; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\Extension\Core\Type\DateType; 
use Symfony\Component\Form\Extension\Core\Type\CollectionType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

class RequestForEstimateController extends Controller 
{ 
/** 
    * @Route("sourcing/request-for-estimate", name="request_for_estimate") 
    */ 
    public function addRequest(Request $request) 
    { 

     $RequestForEstimate = new RequestForEstimate(); 
     $form = $this->createForm(RequestForEstimateType::class, $RequestForEstimate); 

     $form->handleRequest($request); 
     if ($form->isSubmitted() && $form->isValid()) { 

      $request = $form->getData(); 

      foreach($form->get('details')->getData() as $detail) 
      { 
       $request->addDetail($detail); 
      } 

      print_r($form->get('details')->getData()); 
      $request->setStatus(0); 
      $request->setupdateTime(new \DateTime()); 
      $request->setcreateTime(new \DateTime()); 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($request); 
      $em->flush(); 

      return $this->redirectToRoute('RequestsForEstimate'); 
     } 


     return $this->render('sourcing/requestforestimate/create.html.twig', array(
      'form' => $form->createView(), 
     )); 



    } 
} 

マイフォーム:コレクション自体管理するためRequestForEstimateDetail

<?php 
namespace SourcingBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Form\Extension\Core\Type\TextareaType; 
use Symfony\Component\Form\Extension\Core\Type\EmailType; 
use Symfony\Component\Form\Extension\Core\Type\DateTimeType; 
use Symfony\Component\Form\Extension\Core\Type\CollectionType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 

class RequestForEstimateType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('name'); 
     $builder->add('details', CollectionType::class, array(
      'entry_type' => RequestForEstimateDetailType::class, 
      'allow_add'  => true, 
      'allow_delete' => true, 
      'by_reference' => false, 

     )); 
     $builder->add('save', SubmitType::class, array('label' => 'Create Request')); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'SourcingBundle\Entity\RequestForEstimate', 
     )); 
    } 
} 



<?php 
namespace SourcingBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class RequestForEstimateDetailType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('quantity'); 
     $builder->add('pricePerUnit'); 
     $builder->add('shippingCost'); 
     $builder->add('otherFees'); 
     // $builder->add('product'); 


    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'SourcingBundle\Entity\RequestForEstimateDetail', 
     )); 
    } 
} 

答えて

0

フィールド詳細のゲッター/セッターメソッドは、(アド/方法は、コレクションの単一の要素のためのものである削除)ので、変更されていますgetter/setterメソッドを使用してArrayCollectionオブジェクトを処理します。例として

RequestForEstimateDetail

/** 
* Set detail 
* 
* @param \Doctrine\Common\Collections\ArrayCollection an array of $detail 
* 
* @return RequestForEstimateDetail 
*/ 
public function setDetail(\Doctrine\Common\Collections\ArrayCollection $detail = null) 
{ 
    $this->detail = $detail; 

    return $this; 
} 

/** 
* Get detail 
* 
* @return \Doctrine\Common\Collections\ArrayCollection 
*/ 
public function getDetail() 
{ 
    return $this->detail; 
} 

・ホープ、このヘルプ

+0

ありがとうございましたあなたの応答のためにそれhasn何も変えなかった。それでも同じエラー。あなたが見たものから、教義の注釈を修正しました。 – user1029829

+0

こんにちは私は、ArrayCollectionオブジェクトを受け入れるために、メソッドのシグネチャを変更することをお勧めします。 – Matteo

+0

例を教えてください。 – user1029829

0

はまあ、RequestForEstimateエンティティ内の既存のaddDetail方法は、トリックをした:

public function addDetail(\SourcingBundle\Entity\RequestForEstimateDetail $detail) 
{ 
    $this->details->add($detail); 
    $detail->setDetail($this); 
    return $this; 
} 
関連する問題