简体   繁体   中英

Symfony form and entity constructor

I want create entity (Doctrine) from Symfony form.

But I can only create an entry as follows:

$country = new Country($iso2Code = 'US');
$country->setIso3Code('USA');
$country->setName('United States of America');

Set the properties in the following way, I can not:

$country = new Country();
$country->setIso2Code('US');
$country->setIso3Code('USA');
$country->setName('United States of America');

Link on this class: https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/AddressBundle/Entity/Country.php

I can not change the source code of this class (of course I can, but it is bad practice - to change the third-party code).

I do not want to extend this class, as it creates a lot of other problems (in this case I can not use the elements of which depend on this class).

And!!! Setting value through the constructor - not a bad!

But how I can create Symfony 2 form for this entity?

If the source code doesn't fit your needs, just change it AND create a fork of the repository on github : https://github.com/orocrm/platform/fork

and then :

     public function __construct()
     {
         $this->regions  = new ArrayCollection();
     }

     /**
      * Get iso2_code
      *
      * @return string
      */
     public function getIso2Code()
     {
         return $this->iso2Code;
     }

     public function setIso2Code($iso2Code)
     {
         $this->iso2Code = $iso2Code;

         return $this;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM