簡體   English   中英

Symfony2嵌入表單集合

[英]Symfony2 Embed a Collection of Forms

嗨,我有嵌入表格的問題。 我有3個與OneToMany有關的課程

庫存采取一對多庫存采取詳細ManyToOne硬件

我提交表格時出現錯誤。 我不知道我在哪里犯了錯誤。

錯誤:

可捕獲的致命錯誤:傳遞給AppBundle \\ Entity \\ MagazineStockTakingDetails :: setHardware()的參數1必須是AppBundle \\ Entity \\ Hardware的實例,已給定數組,在C:\\ Projekty \\ sla \\ vendor \\ symfony \\ symfony \\ src \\ Symfony中調用第442行的\\ Component \\ PropertyAccess \\ PropertyAccessor.php並已定義

下面我過去的代碼我的課程和形式。 請幫助我發現錯誤。

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * Class MagazineStockTaking
 * @package AppBundle\Entity
 * @ORM\Entity()
 * @ORM\Table(name="sla_stocktaking")
 */
class MagazineStockTaking
{

    /**
     * @ORM\Id
     * @ORM\Column(name="stockTakingId", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="stockinNumber", type="string", length=20, nullable=false)
     */
    protected $stockingnumber;

    /**
     * @ORM\Column(name="stockinDate", type="datetime", nullable=false)
     */
    protected $stockingdate;

    /**
     * @ORM\Column(name="stockingNote", type="string", length=1000, nullable=false)
     */
    protected $stockingnote;

    ////////////////////////////////////////////////
    // RELACJE
    ////////////////////////////////////////////////


    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Magazine", inversedBy="stocktaking")
     * @ORM\JoinColumn(name="magazine_id", referencedColumnName="magazineId", nullable=false)
     */
    protected $magazine;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="stocktaking")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="userId", nullable=false)
     */
    protected $user;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\MagazineStockTakingDetails", mappedBy="stocktaking", cascade={"persist"})
     */
    protected $details;

    ////////////////////////////////////////////////
    // GET SET
    ////////////////////////////////////////////////

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

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

    /**
     * Set stockingnumber
     *
     * @param string $stockingnumber
     * @return MagazineStockTaking
     */
    public function setStockingnumber($stockingnumber)
    {
        $this->stockingnumber = $stockingnumber;

        return $this;
    }

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

    /**
     * Set stockingdate
     *
     * @param \DateTime $stockingdate
     * @return MagazineStockTaking
     */
    public function setStockingdate($stockingdate)
    {
        $this->stockingdate = $stockingdate;

        return $this;
    }

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

    /**
     * Set stockingnote
     *
     * @param string $stockingnote
     * @return MagazineStockTaking
     */
    public function setStockingnote($stockingnote)
    {
        $this->stockingnote = $stockingnote;

        return $this;
    }

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

    /**
     * Set magazine
     *
     * @param \AppBundle\Entity\Magazine $magazine
     * @return MagazineStockTaking
     */
    public function setMagazine(\AppBundle\Entity\Magazine $magazine)
    {
        $this->magazine = $magazine;

        return $this;
    }

    /**
     * Get magazine
     *
     * @return \AppBundle\Entity\Magazine
     */
    public function getMagazine()
    {
        return $this->magazine;
    }

    /**
     * Set user
     *
     * @param \AppBundle\Entity\User $user
     * @return MagazineStockTaking
     */
    public function setUser(\AppBundle\Entity\User $user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \AppBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Add details
     *
     * @param \AppBundle\Entity\MagazineStockTakingDetails $details
     * @return MagazineStockTaking
     */
    public function addDetail(\AppBundle\Entity\MagazineStockTakingDetails $details)
    {
        $this->details[] = $details;

        return $this;
    }

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

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

二等

<?php


namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Class MagazineStockTakingDetails
 * @package AppBundle\Entity
 * @ORM\Entity()
 * @ORM\Table(name="sla_stocktakingdetails")
 */
class MagazineStockTakingDetails
{

    /**
     * @ORM\Id()
     * @ORM\Column(name="stackTakingDetailsId", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="hardwareCount", type="integer", nullable=false)
     */
    protected $count = 1;


    /////////////////////////////////////////////
    // RELACJE
    /////////////////////////////////////////////

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\MagazineStockTaking", inversedBy="details")
     * @ORM\JoinColumn(name="stacktaking_id", referencedColumnName="stockTakingId", nullable=false)
     */
    protected $stocktaking;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Hardware", inversedBy="stocktakingdetails",cascade={"persist"})
     * @ORM\JoinColumn(name="hardware_id", referencedColumnName="hardwareId", nullable=false)
     */
    protected $hardware;

    /////////////////////////////////////////////
    // GET SET
    /////////////////////////////////////////////



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

    /**
     * Set count
     *
     * @param integer $count
     * @return MagazineStockTakingDetails
     */
    public function setCount($count)
    {
        $this->count = $count;

        return $this;
    }

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

    /**
     * Set stocktaking
     *
     * @param \AppBundle\Entity\MagazineStockTaking $stocktaking
     * @return MagazineStockTakingDetails
     */
    public function setStocktaking(\AppBundle\Entity\MagazineStockTaking $stocktaking)
    {
        $this->stocktaking = $stocktaking;

        return $this;
    }

    /**
     * Get stocktaking
     *
     * @return \AppBundle\Entity\MagazineStockTaking 
     */
    public function getStocktaking()
    {
        return $this->stocktaking;
    }


    /**
     * Set hardware
     *
     * @param \AppBundle\Entity\Hardware $hardware
     * @return MagazineStockTakingDetails
     */
    public function setHardware(\AppBundle\Entity\Hardware $hardware)
    {

        $this->hardware = $hardware;

        return $this;
    }

    /**
     * Get hardware
     *
     * @return \AppBundle\Entity\Hardware 
     */
    public function getHardware()
    {
        return $this->hardware;
    }
}

三等

<?php


namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use JMS\Serializer\Annotation\Groups;


/**
 * Class Hardware
 * @package AppBundle\Entity
 * @ORM\Entity(repositoryClass="HardwareRepository", )
 * @ORM\Table(name="sla_hardwares")
 * @JMS\ExclusionPolicy("all")
 */
class Hardware
{

    /**
     * @ORM\Id()
     * @ORM\Column(name="hardwareId", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /**
     * @ORM\Column(name="hardwareSn", type="string", length=200, nullable=true)
     */
    protected $sn;

    /**
     * @ORM\Column(name="hardwareGwarantyDate", type="datetime", nullable=true)
     */
    protected $gwarantydate;

    /**
     * @ORM\Column(name="hardwareNote", type="string", length=750, nullable=true)
     */
    protected $note;

    /**
     * @ORM\Column(name="hardwareIsReturned", type="boolean", nullable=false)
     */
    protected $isreturned = false;

    ////////////////////////////////////////
    //// RELACJE
    ////////////////////////////////////////

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\HardwareProducent", inversedBy="hardware")
     * @ORM\JoinColumn(name="producent_id", referencedColumnName="producentId", nullable=false)
    */
    protected $producent;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\HardwareCategory", inversedBy="hardware")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="hardwareCategoryId", nullable=false)
     */
    protected $category;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\HardwareModel", inversedBy="hardware")
     * @ORM\JoinColumn(name="model_id", referencedColumnName="hardwareModelId", nullable=false)
     */
    protected $model;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\HardwareStatus", inversedBy="hardware")
     * @ORM\JoinColumn(name="status_id", referencedColumnName="statusId", nullable=false)
     */
    protected $status;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\HardwareStatusHistory", mappedBy="hardware")
     */
    protected $statushistory;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\MagazineDetails", mappedBy="hardware")
     */
    protected $magazine;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\MagazineShiftDetails", mappedBy="hardware")
     */
    protected $magazineshift;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\MagazineUtilizeDetails", mappedBy="hardware")
     */
    protected $utilize;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\MagazineStockTakingDetails", mappedBy="hardware", cascade={"persist"})
     */
    protected $stocktakingdetails;

    ////////////////////////////////////////
    //// GET SET
    ////////////////////////////////////////
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->statushistory = new \Doctrine\Common\Collections\ArrayCollection();
        $this->magazine = new \Doctrine\Common\Collections\ArrayCollection();
        $this->magazineshift = new \Doctrine\Common\Collections\ArrayCollection();
        $this->utilize = new \Doctrine\Common\Collections\ArrayCollection();
        $this->stocktakingdetails = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Set sn
     *
     * @param string $sn
     * @return Hardware
     */
    public function setSn($sn)
    {
        $this->sn = $sn;

        return $this;
    }

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

    /**
     * Set gwarantydate
     *
     * @param \DateTime $gwarantydate
     * @return Hardware
     */
    public function setGwarantydate($gwarantydate)
    {
        $this->gwarantydate = $gwarantydate;

        return $this;
    }

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

    /**
     * Set note
     *
     * @param string $note
     * @return Hardware
     */
    public function setNote($note)
    {
        $this->note = $note;

        return $this;
    }

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

    /**
     * Set isreturned
     *
     * @param boolean $isreturned
     * @return Hardware
     */
    public function setIsreturned($isreturned)
    {
        $this->isreturned = $isreturned;

        return $this;
    }

    /**
     * Get isreturned
     *
     * @return boolean 
     */
    public function getIsreturned()
    {
        return $this->isreturned;
    }

    /**
     * Set producent
     *
     * @param \AppBundle\Entity\HardwareProducent $producent
     * @return Hardware
     */
    public function setProducent(\AppBundle\Entity\HardwareProducent $producent)
    {
        $this->producent = $producent;

        return $this;
    }

    /**
     * Get producent
     *
     * @return \AppBundle\Entity\HardwareProducent 
     */
    public function getProducent()
    {
        return $this->producent;
    }

    /**
     * Set category
     *
     * @param \AppBundle\Entity\HardwareCategory $category
     * @return Hardware
     */
    public function setCategory(\AppBundle\Entity\HardwareCategory $category)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * Get category
     *
     * @return \AppBundle\Entity\HardwareCategory 
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     * Set model
     *
     * @param \AppBundle\Entity\HardwareModel $model
     * @return Hardware
     */
    public function setModel(\AppBundle\Entity\HardwareModel $model)
    {
        $this->model = $model;

        return $this;
    }

    /**
     * Get model
     *
     * @return \AppBundle\Entity\HardwareModel 
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * Set status
     *
     * @param \AppBundle\Entity\HardwareStatus $status
     * @return Hardware
     */
    public function setStatus(\AppBundle\Entity\HardwareStatus $status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return \AppBundle\Entity\HardwareStatus 
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Add statushistory
     *
     * @param \AppBundle\Entity\HardwareStatusHistory $statushistory
     * @return Hardware
     */
    public function addStatushistory(\AppBundle\Entity\HardwareStatusHistory $statushistory)
    {
        $this->statushistory[] = $statushistory;

        return $this;
    }

    /**
     * Remove statushistory
     *
     * @param \AppBundle\Entity\HardwareStatusHistory $statushistory
     */
    public function removeStatushistory(\AppBundle\Entity\HardwareStatusHistory $statushistory)
    {
        $this->statushistory->removeElement($statushistory);
    }

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

    /**
     * Add magazine
     *
     * @param \AppBundle\Entity\MagazineDetails $magazine
     * @return Hardware
     */
    public function addMagazine(\AppBundle\Entity\MagazineDetails $magazine)
    {
        $this->magazine[] = $magazine;

        return $this;
    }

    /**
     * Remove magazine
     *
     * @param \AppBundle\Entity\MagazineDetails $magazine
     */
    public function removeMagazine(\AppBundle\Entity\MagazineDetails $magazine)
    {
        $this->magazine->removeElement($magazine);
    }

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

    /**
     * Add magazineshift
     *
     * @param \AppBundle\Entity\MagazineShiftDetails $magazineshift
     * @return Hardware
     */
    public function addMagazineshift(\AppBundle\Entity\MagazineShiftDetails $magazineshift)
    {
        $this->magazineshift[] = $magazineshift;

        return $this;
    }

    /**
     * Remove magazineshift
     *
     * @param \AppBundle\Entity\MagazineShiftDetails $magazineshift
     */
    public function removeMagazineshift(\AppBundle\Entity\MagazineShiftDetails $magazineshift)
    {
        $this->magazineshift->removeElement($magazineshift);
    }

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

    /**
     * Add utilize
     *
     * @param \AppBundle\Entity\MagazineUtilizeDetails $utilize
     * @return Hardware
     */
    public function addUtilize(\AppBundle\Entity\MagazineUtilizeDetails $utilize)
    {
        $this->utilize[] = $utilize;

        return $this;
    }

    /**
     * Remove utilize
     *
     * @param \AppBundle\Entity\MagazineUtilizeDetails $utilize
     */
    public function removeUtilize(\AppBundle\Entity\MagazineUtilizeDetails $utilize)
    {
        $this->utilize->removeElement($utilize);
    }

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

    /**
     * Add stocktakingdetails
     *
     * @param \AppBundle\Entity\MagazineStockTakingDetails $stocktakingdetails
     * @return Hardware
     */
    public function addStocktakingdetail(\AppBundle\Entity\MagazineStockTakingDetails $stocktakingdetails)
    {
        $this->stocktakingdetails[] = $stocktakingdetails;
        $stocktakingdetails->setHardware($this);

        return $this;
    }

    /**
     * Remove stocktakingdetails
     *
     * @param \AppBundle\Entity\MagazineStockTakingDetails $stocktakingdetails
     */
    public function removeStocktakingdetail(\AppBundle\Entity\MagazineStockTakingDetails $stocktakingdetails)
    {
        $this->stocktakingdetails->removeElement($stocktakingdetails);
    }

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

頭等艙

<?php


namespace AppBundle\Form\Type;


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

class StockTakingFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('stockingnote','textarea',array(
                'label' => false,
                'attr' => array(
                    'class' => 'gui-textarea',
                    'placeholder' => 'Uwagi',
                    'data-text' => 'Uwagi'
                ),
                'required' => false
            ))
            ->add('details','collection',array(
                'type' => new StockTakingDetailFormType(),
                'allow_add' => true,
                'by_reference' => false
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\MagazineStockTaking',
            'attr' => array(
                'id' => 'form_stoking'
            )
        ));
    }

    public function getName() {
        return 'stocktaking';
    }

}

第二類

<?php

namespace AppBundle\Form\Type;


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

class StockTakingDetailFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ->add('count','number',array(
                'label' => false,
                'data' => '1',
                'required' => false
            ))
            ->add('hardware','collection',array(
                'type' => new HardwareFormType(),
                'allow_add' => true,
                'by_reference' => false
            ))
        ;

    }

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

    public function getName()
    {
        return 'stocktakingtetail';
    }

}

第三類

namespace AppBundle\Form\Type;


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

class HardwareFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('sn','text',array(
                'label' => false,
                'attr' => array(
                    'class' => 'gui-input',
                    'placeholder' => 'Numer Seryjny Urządzenia'
                ),
                'required' => true
            ))
            ->add('gwarantydate','number',array(
                'label' => false,
                'attr' => array(
                    'class' => 'gui-input',
                    'placeholder' => 'Ilość Gwarancji (miesięcy)'
                ),
                'required' => false
            ))
            ->add('isreturned','checkbox',array(
                'label' => 'Sprzęt Rotacyjny',
                'label_attr' => array(
                    'class' => 'option-primary'
                ),
                'required' => true
            ))
            ->add('note','textarea' ,array(
                'label' => false,
                'attr' => array(
                    'class' => 'gui-textarea',
                    'placeholder' => 'Opis',
                    'data-text' => 'Opis'
                ),
                'required' => false
            ))
            ->add('producent','entity',array(
                'class' => 'AppBundle\Entity\HardwareProducent',
                'property' => 'name',
                'label' => false,
                'attr' => array(
                    'class' => 'select',
                    'placeholder' => 'Producent'
                ),
                'required' => true
            ))
            ->add('category','entity',array(
                'class' => 'AppBundle\Entity\HardwareCategory',
                'property' => 'name',
                'label' => false,
                'attr' => array(
                    'class' => 'select',
                    'placeholder' => 'Kategoria'
                ),
                'required' => true
            ))
            ->add('model','entity',array(
                'class' => 'AppBundle\Entity\HardwareModel',
                'property' => 'name',
                'label' => false,
                'attr' => array(
                    'class' => 'select',
                    'placeholder' => 'Model'
                ),
                'required' => true
            ))
            ->add('status','entity',array(
                'class' => 'AppBundle\Entity\HardwareStatus',
                'property' => 'name',
                'label' => false,
                'attr' => array(
                    'class' => 'select',
                    'placeholder' => 'Status'
                ),
                'required' => true
            ))
        ;
    }

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

    public function getName()
    {
        return 'hardware';
    }

}

如果硬件上具有ManyToOne ,那么MagazineStockTakingDetails會引用“硬件MagazineStockTakingDetails類型的對象,因此,您只能設置一個“硬件”類型的對象,但是您嘗試設置ArrayCollection

這行不通:

->add('hardware','collection',array(
            'type' => new HardwareFormType(),
            'allow_add' => true,
            'by_reference' => false
        ))

您將需要執行以下操作:

->add('hardware', new HardwareFormType(),array(
            ...
        ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM