Doctrine orm 动态添加的字段集/集合未添加到数据库Zend 2 Doctrin 2

Doctrine orm 动态添加的字段集/集合未添加到数据库Zend 2 Doctrin 2,doctrine-orm,zend-framework2,fieldset,Doctrine Orm,Zend Framework2,Fieldset,在浏览了几篇关于Zend2/Doctrine 2和Fieldset的教程之后,我终于找到了filedset/Collection 但是新的“字段”不会添加到数据库表中。将存储现有图元中的任何更改。主要类别组织称为fieldset ActBusinessCountry: 主要班级组织 class Organization { protected $inputFilter; /** * @ORM\Id * @ORM\Column(type="i

在浏览了几篇关于Zend2/Doctrine 2和Fieldset的教程之后,我终于找到了filedset/Collection

但是新的“字段”不会添加到数据库表中。将存储现有图元中的任何更改。主要类别组织称为fieldset ActBusinessCountry:

主要班级组织

class Organization  {

    protected $inputFilter;

       /**
       * @ORM\Id
       * @ORM\Column(type="integer");
       */
      protected $id;

      /**
      * @ORM\Column(type="string")
      */
      protected $organizational;

      /**
      * @ORM\Column(type="string")
      */
      protected $structure;

    /**
     * @param \Doctrine\Common\Collections\ArrayCollection
     * @ORM\OneToMany(targetEntity="People\Entity\ActBusinessCountry",mappedBy="company",cascade={"persist", "merge", "refresh", "remove"})
     */
    protected $organizaton_opcountry;


    public function __construct()
    {
     $this->organizaton_opcountry = new \Doctrine\Common\Collections\ArrayCollection();
    }

     /**
     * @param \Doctrine\Common\Collections\ArrayCollection $organizaton_opcountry
     */
    public function addOrganizaton_opcountry(Collection $organizaton_opcountry)
    {
        foreach ($organizaton_opcountry as $opcountry) {
            $this->organizaton_opcountry->add($opcountry);
        }
        return $this->organizaton_opcountry;
    }

    public function removeOrganizaton_opcountry(Collection $organizaton_opcountry)
    {
        foreach ($organizaton_opcountry as $opcountry) {
            $tag->setCompany(null);
            $this->organizaton_opcountry->removeElement($opcountry);
        }
    }
    /**
     * @return Collection
     */
    public function getOrganizaton_opcountry()
    {
        return $this->organizaton_opcountry;
    }
子类/字段集为ActBusinessCountry

class ActBusinessCountry  {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    public $id;

    /**
     * @ORM\ManyToOne(targetEntity="People\Entity\Organization",inversedBy="organizaton_opcountry")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     */
    public $company;

    /**
     * @ORM\Column(type="string")
     */
    public $country;

    /**
     * @ORM\Column(type="string")
     */
    public $company_id;

    /**
     * Allow null to remove association
     */
    public function setId($id = null)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }

     public function getCompany()
    {
        return $this->company;
    }

    public function setCompany(Company $company = null)
    {
        $this->company = $company;
    }

    public function getCountry()
    {
        return $this->country;
    }

    public function setCountry($country)
    {
        $this->country = $country;
    }

}
组织形式:

$countrySelect = new ActBusinessCountryFieldset($objectManager);

  $this->add(array(
      'type' => 'Zend\Form\Element\Collection',
      'name' => 'organizaton_opcountry',
      'options' => array(
           'should_create_template' => true,
           'use_as_base_fieldet' => true,
           'count' => 1,
           'allow_add' => true,
           'target_element' => $countrySelect,
    ),
));
class ActBusinessCountryFieldset extends Fieldset implements ObjectManagerAwareInterface
{
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->setObjectManager($objectManager);

        parent::__construct('fieldset');

        $this   ->setHydrator(new DoctrineHydrator($objectManager, 'People\Entity\ActBusinessCountry'))
                ->setObject(new \People\Entity\ActBusinessCountry());            

       $this->add(array(
            'type' => 'Zend\Form\Element\Hidden',
            'name' => 'id'
        ));


        $this->add(array(
            'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
            'name'    => 'country',
            'attributes' => array(
                'class' => 'form-control input-small',
            ),
            'options' => array(
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'People\Entity\Country',
                'value'         =>  'country',
                'property'       => 'country_name',
                'class'=>'form-control',
                'label_attributes' => array(
                    'class'=> 'col-sm-3 control-label',
                ),
            ),
        ));

    }

    public function getInputFilterSpecification()
    {
        return array(
            'id' => array(
                'required' => false
            )
        );
        return array(
            'country' => array(
                'required' => true
            )
        );
    }
if ($this->request->isPost()) {

            // Cancel button
            if(isset($_POST['cancel'])) {
                echo "<script>window.close();</script>";
            }

            $form->setData($this->request->getPost());

            var_dump($this->request->getPost('organizaton_opcountry'));

            var_dump($queryresult->getOrganizaton_opcountry());

            //$queryresult->addOrganizaton_opcountry();

            if ($form->isValid()) {
                // Security request
                if ($this->isAllowed('admin_res','admin_priv')) {
                    $form->bindValues();
                    $this->getEntityManager()->persist($queryresult);
                    $this->getEntityManager()->flush();
                }
                //echo "<script>window.close();</script>";
            }
        }
Fielset元素:

$countrySelect = new ActBusinessCountryFieldset($objectManager);

  $this->add(array(
      'type' => 'Zend\Form\Element\Collection',
      'name' => 'organizaton_opcountry',
      'options' => array(
           'should_create_template' => true,
           'use_as_base_fieldet' => true,
           'count' => 1,
           'allow_add' => true,
           'target_element' => $countrySelect,
    ),
));
class ActBusinessCountryFieldset extends Fieldset implements ObjectManagerAwareInterface
{
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->setObjectManager($objectManager);

        parent::__construct('fieldset');

        $this   ->setHydrator(new DoctrineHydrator($objectManager, 'People\Entity\ActBusinessCountry'))
                ->setObject(new \People\Entity\ActBusinessCountry());            

       $this->add(array(
            'type' => 'Zend\Form\Element\Hidden',
            'name' => 'id'
        ));


        $this->add(array(
            'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
            'name'    => 'country',
            'attributes' => array(
                'class' => 'form-control input-small',
            ),
            'options' => array(
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'People\Entity\Country',
                'value'         =>  'country',
                'property'       => 'country_name',
                'class'=>'form-control',
                'label_attributes' => array(
                    'class'=> 'col-sm-3 control-label',
                ),
            ),
        ));

    }

    public function getInputFilterSpecification()
    {
        return array(
            'id' => array(
                'required' => false
            )
        );
        return array(
            'country' => array(
                'required' => true
            )
        );
    }
if ($this->request->isPost()) {

            // Cancel button
            if(isset($_POST['cancel'])) {
                echo "<script>window.close();</script>";
            }

            $form->setData($this->request->getPost());

            var_dump($this->request->getPost('organizaton_opcountry'));

            var_dump($queryresult->getOrganizaton_opcountry());

            //$queryresult->addOrganizaton_opcountry();

            if ($form->isValid()) {
                // Security request
                if ($this->isAllowed('admin_res','admin_priv')) {
                    $form->bindValues();
                    $this->getEntityManager()->persist($queryresult);
                    $this->getEntityManager()->flush();
                }
                //echo "<script>window.close();</script>";
            }
        }
控制器冲洗部分:

$countrySelect = new ActBusinessCountryFieldset($objectManager);

  $this->add(array(
      'type' => 'Zend\Form\Element\Collection',
      'name' => 'organizaton_opcountry',
      'options' => array(
           'should_create_template' => true,
           'use_as_base_fieldet' => true,
           'count' => 1,
           'allow_add' => true,
           'target_element' => $countrySelect,
    ),
));
class ActBusinessCountryFieldset extends Fieldset implements ObjectManagerAwareInterface
{
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        $this->setObjectManager($objectManager);

        parent::__construct('fieldset');

        $this   ->setHydrator(new DoctrineHydrator($objectManager, 'People\Entity\ActBusinessCountry'))
                ->setObject(new \People\Entity\ActBusinessCountry());            

       $this->add(array(
            'type' => 'Zend\Form\Element\Hidden',
            'name' => 'id'
        ));


        $this->add(array(
            'type'    => 'DoctrineModule\Form\Element\ObjectSelect',
            'name'    => 'country',
            'attributes' => array(
                'class' => 'form-control input-small',
            ),
            'options' => array(
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'People\Entity\Country',
                'value'         =>  'country',
                'property'       => 'country_name',
                'class'=>'form-control',
                'label_attributes' => array(
                    'class'=> 'col-sm-3 control-label',
                ),
            ),
        ));

    }

    public function getInputFilterSpecification()
    {
        return array(
            'id' => array(
                'required' => false
            )
        );
        return array(
            'country' => array(
                'required' => true
            )
        );
    }
if ($this->request->isPost()) {

            // Cancel button
            if(isset($_POST['cancel'])) {
                echo "<script>window.close();</script>";
            }

            $form->setData($this->request->getPost());

            var_dump($this->request->getPost('organizaton_opcountry'));

            var_dump($queryresult->getOrganizaton_opcountry());

            //$queryresult->addOrganizaton_opcountry();

            if ($form->isValid()) {
                // Security request
                if ($this->isAllowed('admin_res','admin_priv')) {
                    $form->bindValues();
                    $this->getEntityManager()->persist($queryresult);
                    $this->getEntityManager()->flush();
                }
                //echo "<script>window.close();</script>";
            }
        }
if($this->request->isPost()){
//取消按钮
如果(isset($_POST['cancel'])){
回显“window.close();”;
}
$form->setData($this->request->getPost());
var_dump($this->request->getPost('organizaton_opcountry');
var_dump($queryresult->getorganization_opcountry());
//$queryresult->AddOrganization_opcountry();
如果($form->isValid()){
//安全请求
如果($this->isAllowed('admin\u res','admin\u priv')){
$form->bindValues();
$this->getEntityManager()->persist($queryresult);
$this->getEntityManager()->flush();
}
//回显“window.close();”;
}
}
恐怕我错过了一点。通过post var_dump($this->request->getPost('organizaton_opcountry');在控制器中,添加第三个元素并提交后,输出以下内容:

排列 0 => 排列 'id'=>字符串'1'(长度=1) 'country'=>字符串'Chad'(长度=4) 1 => 排列 'id'=>字符串'2'(长度=1) 'country'=>字符串'Bermuda'(长度=7) 2 => 排列 “id'=>字符串“”(长度=0) 'country'=>string'(未指定)'(长度=15)

也许你们有个主意,或者你们以前也有过同样的问题

非常感谢你的任何提示

亲切问候,,
大卫

也许你可以看看:

检查字段集中的inputFilter,并在表单中为字段集添加inputFilter,例如:

$this->setValidationGroup(array(
    'User' => array(
        'name',
        'role' // <- Fieldset
    )
));
$this->setValidationGroup(数组)(
“用户”=>数组(
“姓名”,
“角色”//