Symfony2嵌入表单集

时间:2016-01-20 19:26:29

标签: php forms symfony doctrine-orm

嗨我有表格嵌入的问题。 我有3个关系OneToMany的课程

StockTaking OneToMany StockTakingDetail ManyToOne Hardware

我提交表单时收到错误。 我不知道我犯了什么错误。

  

错误:

     
    

捕获致命错误:参数1传递给     AppBundle \ Entity \ MagazineStockTakingDetails :: setHardware()必须是     AppBundle \ Entity \ Hardware的实例,给定的数组,调用     C:\ Projekty \ SLA \厂商\ symfony的\ symfony的\ SRC \的Symfony \元器件\ PropertyAccess \ PropertyAccessor.php     在第442行并定义

  

在我之后的代码我的课程和表格。请帮我找错。

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';
    }

}

1 个答案:

答案 0 :(得分:1)

如果硬件上有read()ManyToOne会引用硬件类型的对象,因此您只能设置硬件类型的一个对象,但是您试图设置MagazineStockTakingDetails

这无效:

ArrayCollection

你需要做这样的事情:

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