2013-10-05 52 views
0

我使用Zend 2和學說2Zend的2 + 2原則返回CheckBox值

我不清楚如何退貨,並從一個複選框返回的值填充數據庫。這些值應該返回表中的多個條目。

我找不到任何有關如何呈現或使用複選框的文檔/文章;所以,我真的很感激此建議或示例代碼。

我在下面附上了我的課。我相信問題出現在我的實體類中:即我沒有正確使用數組集合中的getter/setter,因此我一直從返回的表單中獲取錯誤消息,即返回的值爲空。

這裏是我的代碼:

我的MySQL表:

CREATE TABLE workers_timetable(
id MEDIUMINT UNSIGNED NOT NULL, 
timesId MEDIUMINT UNSIGNED NOT NULL, 
INDEX (id ,timesId), 
INDEX times_id (timesId, id) 

); 

我的班級;

<?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類

<?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' 
      ) 
     )); 
    ) 

回答

0

首先,你想使用DoctrineModule\Form\Element\ObjectMultiCheckbox。有關更多信息,請參閱the DoctrineModule\docs

其次的事情會是你做你的實體裏面的東西錯的,如果我現在不完全啞......

public function addTimesId(Collection $timesId) 
{ 
    foreach ($timesId as $timesId) { 
     $this->setTimesId($this);  // This should be $timesId->setWorker($this); 
     $this->timesId->add($timesId); 
    } 
} 

查看更多相關信息後,再次在the DoctrineModule\docs

+0

如何山姆。感謝您的迴應。我似乎還沒有得到它的工作。我仍然得到空值。當你提到我應該把我的addTimesId()設置爲$ timesId-> setworker($ ythis);應該是班級的名字。即班級的名稱是:WorkersAvailabilityTimeTableFieldset請致電 –

+0

。有沒有人有任何如何使用複選框的工作示例。沒有使用教義2和複選框的教程或例子;這使得像我這樣的新用戶學習如何使用zend/doctrine成爲一場噩夢。我會真正appriciate任何幫助或建議 –

+0

使用ObjectMultiCheckbox並使用它與教程中提供的ObjectSelect完全一樣。唯一的區別是ElementName,其餘的作品完全相同。 – Sam