doutan1875 2016-03-23 10:02
浏览 41
已采纳

Symfony3与实体嵌入式表单刷新时出错

I created a "ClientType" with a "InformationType" and a "ContactType". Client can have multiple informations and contacts so I use a form that embedded Client, Information and Contact.

When I print the form it's okay but as soon as I flush:

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in C:\wamp\www\LinkWebCRM\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 605 and defined

I tried to use "CollectionType" of "InformationType" and "ContactType" but then I get a lot of form warning "This value is not valid.", "This form should not contain extra fields.", and 8 times the form for Information and Contact ...

ClientType:

class ClientType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('nom',            TextType::class);
        $builder->add('prenom',         TextType::class);
        $builder->add('informations',   InformationType::class);
        $builder->add('contacts',       ContactType::class);
        $builder->add('Enregistrer',    SubmitType::class);
    }

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

Information:

class InformationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('nomEntreprise',      TextType::class);
        $builder->add('sIRET',              TextType::class);
        $builder->add('typeSite',           TextType::class);
        $builder->add('adresseEntreprise',  TextType::class);
        $builder->add('dateSignature',      DateTimeType::class, array(
            'years' => range(date("Y"), date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 1825 day")))
            ));
        $builder->add('nomDomaine',         TextType::class);
        $builder->add('dateMiseEnLigne',    DateTimeType::class, array(
            'years' => range(date("Y"), date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 1825 day")))
            ));
        $builder->add('commentaire',        TextType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('data_class' => null));
    }
}

ContactType:

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('telephone',  TextType::class);
        $builder->add('fax',        TextType::class, array(
            'required' => false));
        $builder->add('email',      TextType::class);
    }

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

And then,

Client:

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use CommonBundle\Entity\Information;

/**
 * Client
 *
 * @ORM\Table(name="client")
 * @ORM\Entity(repositoryClass="CommonBundle\Repository\ClientRepository")
 */
class Client
{
    /**
     * @ORM\OneToMany(targetEntity="CommonBundle\Entity\Information", mappedBy="client")
     */
    public $informations;

    /**
     * @ORM\OneToMany(targetEntity="CommonBundle\Entity\Contact", mappedBy="client")
     */
    public $contacts;

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

    /**
     * Add information
     *
     * @param \CommonBundle\Entity\Information $information
     *
     * @return Client
     */
    public function addInformation(\CommonBundle\Entity\Information $information)
    {
        $this->informations[] = $information;

        return $this;
    }

    /**
     * Remove information
     *
     * @param \CommonBundle\Entity\Information $information
     */
    public function removeInformation(\CommonBundle\Entity\Information $information)
    {
        $this->informations->removeElement($information);
    }

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

    /**
     * Add contact
     *
     * @param \CommonBundle\Entity\Contact $contact
     *
     * @return Client
     */
    public function addContact(\CommonBundle\Entity\Contact $contact)
    {
        $this->contacts[] = $contact;

        return $this;
    }

    /**
     * Remove contact
     *
     * @param \CommonBundle\Entity\Contact $contact
     */
    public function removeContact(\CommonBundle\Entity\Contact $contact)
    {
        $this->contacts->removeElement($contact);
    }

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

Information:

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use CommonBundle\Entity\Client;

/**
 * Information
 *
 * @ORM\Table(name="information")
 * @ORM\Entity(repositoryClass="CommonBundle\Repository\InformationRepository")
 */
class Information
{
    /**
    * @ORM\ManyToOne(targetEntity="CommonBundle\Entity\Client", inversedBy="informations")
    * @ORM\JoinColumn(nullable=false)
    */
    public $client;

    /**
     * Set client
     *
     * @param \CommonBundle\Entity\Client $client
     *
     * @return Information
     */
    public function setClient(\CommonBundle\Entity\Client $client)
    {
        $this->client = $client;

        return $this;
    }

    /**
     * Get client
     *
     * @return \CommonBundle\Entity\Client
     */
    public function getClient()
    {
        return $this->client;
    }
}

And Contact:

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Contact
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity(repositoryClass="CommonBundle\Repository\ContactRepository")
 */
class Contact
{

    /**
    * @ORM\ManyToOne(targetEntity="CommonBundle\Entity\Client", inversedBy="contacts")
    * @ORM\JoinColumn(nullable=true)
    */
    public $client;

    /**
     * Set client
     *
     * @param \CommonBundle\Entity\Client $client
     *
     * @return Contact
     */
    public function setClient(\CommonBundle\Entity\Client$client = null)
    {
        $this->client = $client;

        return $this;
    }

    /**
     * Get client
     *
     * @return \CommonBundle\Entity\Client
     */
    public function getClient()
    {
        return $this->client;
    }
}
  • 写回答

1条回答 默认 最新

  • dongyou9818 2016-03-23 11:03
    关注

    So, because of the collection, I had to create an instance of Information. Then I hydrated it like that:

    $information->setNameVariable($client->getInformations()['nameVariable'])
    

    Notice that the 'information' from $client->getInformations() is NOT an object at the moment but the array of the form, which were the problem.

    This for all variables from the form oppositely as expected. Finally I added the var not nullable, persist the object, repeat the logic for "Contact" and then flush.

    I also had to add in the __constructor of Client:

    $this->contacts = new \Doctrine\Common\Collections\ArrayCollection();
    

    And for the ClientType:

    $builder->add('informations',   InformationType::class, array(
            'data_class' => null));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改