首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >zend 2+ Doctrine 2返回复选框值

zend 2+ Doctrine 2返回复选框值
EN

Stack Overflow用户
提问于 2013-10-05 11:34:01
回答 1查看 1.2K关注 0票数 0

我用的是zend 2和教义2

我不清楚如何从复选框返回数据库并使用返回的值填充数据库。这些值应该返回表的多个条目。

我找不到任何关于如何呈现或使用复选框的文档/文章;因此,我非常希望得到有关这方面的建议或示例代码。

我已把我的课附在下面。我认为这个问题发生在我的实体类中:也就是说,我没有正确地将getter/setter用于数组集合,因此我一直从返回的postform中得到错误消息,即返回的值是空的。

这是我的代码:

我的mysql表:

代码语言:javascript
复制
CREATE TABLE workers_timetable(
id MEDIUMINT UNSIGNED NOT NULL, 
timesId MEDIUMINT UNSIGNED NOT NULL,
INDEX (id ,timesId ), 
INDEX times_id (timesId, id ) 

); 

我的班;

代码语言:javascript
复制
<?php

namespace Workers\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface; 

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;


/**
 *
 * @ORM\Entity
 * @ORM\Table(name="workers_timetable")
 * @property int $timesId
 */
class WorkersAvailabilityTimeTable {

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

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




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


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



   public function addTimesId(Collection $timesId)
    {
        foreach ($timesId as $timesId) {
            $this->setTimesId($this);
            $this->timesId->add($timesId);
        }
    }

    public function setTimesId()
    {
        return $this->timesId;
    }


    public function removeTimesId(Collection $timesId)
    {
        foreach ($timesId as $timesId) {
            $this->setTimesId(null);
            $this->timesId->removeElement($timesId);
        }
    }

    public function getTimesId()
    {
        return $this->timesId;
    }


    //put your code here
}

我的fieldSet类

代码语言:javascript
复制
<?php

namespace Workers\Form\Fieldset;

use Workers\Entity\WorkersAvailabilityTimeTable;
use Doctrine\Common\Persistence\ObjectManager;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;



class WorkersAvailabilityTimeTableFieldset extends Fieldset implements InputFilterProviderInterface
{
   public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('WorkerTimeTable');


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


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

         $this->add(array(
            'type' => 'Zend\Form\Element\MultiCheckbox',
            'name' => 'timesIds',
            'options' => array(
                'label' => 'Please Select Your Availablity',
                'value_options' => array(

                    '1'  =>'mon',
                    '2' =>'tues'
   ),
            ),
            'attributes' => array(
                'value' => '1' //set checked to '1'
            )
        ));
     )
EN

回答 1

Stack Overflow用户

发布于 2013-10-05 11:55:58

首先,您需要使用DoctrineModule\Form\Element\ObjectMultiCheckbox。有关更多信息,请参阅DoctrineModule\docs

第二件事就是你在你的身体里做错事如果我现在不是完全愚蠢的话.

代码语言:javascript
复制
public function addTimesId(Collection $timesId)
{
    foreach ($timesId as $timesId) {
        $this->setTimesId($this);      // This should be $timesId->setWorker($this);
        $this->timesId->add($timesId);
    }
}

再次在DoctrineModule\docs上查看更多关于晚些时候的信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19197329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档