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

时间:2014-06-01 16:39:32

标签: doctrine-orm zend-framework2 fieldset

在浏览了几个教程后,重新调整了Zend2 / Doctrine 2和Fieldsets,我终于找到了fieldset / collction。

但新的"领域"不会被添加到数据库表中。存储现有元素的任何更改。主要类组织称为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,
    ),
));

Fielset Elements:

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

我害怕,我错过了一点。虽然发布了var_dump($ this-&gt; request-&gt; getPost(&#39; organizaton_opcountry&#39;));在添加第三个元素并提交后,控制器输出以下内容:

阵列   0 =&gt;     排列       &#39; ID&#39; =&GT;字符串&#39; 1&#39; (长度= 1)       &#39;国&#39; =&GT;字符串&#39; Chad&#39; (长度= 4)   1 =&gt;     排列       &#39; ID&#39; =&GT;字符串&#39; 2&#39; (长度= 1)       &#39;国&#39; =&GT;字符串&#39;百慕大&#39; (长度= 7)   2 =&gt;     排列       &#39; ID&#39; =&GT;字符串&#39;&#39; (长度= 0)       &#39;国&#39; =&GT;字符串&#39;(未指定)&#39; (长度= 15)

也许你们有个主意,或者你之前遇到过同样的问题。

非常感谢您的任何暗示。

亲切的问候, 大卫

1 个答案:

答案 0 :(得分:0)

也许你看看:

Saving a Doctine 2 Entity that contains an ObjectSelect element using Zend Form

检查Fieldset中的inputFilter,并为表格集添加一个inputfilter,例如:

$this->setValidationGroup(array(
    'User' => array(
        'name',
        'role' // <- Fieldset
    )
));