2014-04-21 34 views
0

我在zf2中使用doctrine2 odm,我想使自动增量id,我做日历id作为自动增量,但它不保存在数据库中,我如何使日历id自动增量并保存在数据库中? 这里是我的代码:如何在zf2中doctrine2 odm中设置自动增量ID?

<?php 
namespace Calendar\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; 
//use Doctrine\ODM\MongoDB\DocumentRepository; 

/** @ODM\Document(collection="calendar") */ 
class Calendar 
{ 
/** @ODM\Id */ 
private $id; 

/** @ODM\Field(type="int",strategy="INCREMENT") */ 
private $calendar_id; 

/** @ODM\Field(type="int") */ 
private $user_id; 

/** @ODM\Field(type="string") */ 
private $title; 

/** @ODM\Field(type="string") */ 
private $description; 

/** 
* @return the table field 
*/ 
public function getProperty($property) { 
    if (property_exists($this, $property)) { 
     return $this->$property; 
    } 
} 

public function setProperty($property, $value) { 
    if (property_exists($this, $property)) { 
     $this->$property = $value; 
    } 
    return $this; 
} 

public function getData(){ 
    return array(
     'id'=>$this->id, 
     'calendar_id'=>calendar_id, 
     'user_id'=>$this->user_id, 
     'title'=>$this->title, 
     'description'=>$this->description  
    ); 
} 
} 

,这里是我的表单:

<?php 
namespace Calendar\Form; 

use Zend\Form\Form; 
class CalendarForm extends Form 
{ 
public function __construct($name = null) 
{ 
    // we want to ignore the name passed 
    parent::__construct('calendar'); 
    $this->setAttribute('method', 'post'); 

    $this->add(array(
     'name' => 'calendar_id', 
     'attributes' => array(
      'type' => 'hidden', 
     ), 
    )); 

    $this->add(array(
     'name' => 'user_id', 
     'attributes' => array(
      'type' => 'hidden', 
     ), 
    )); 
    $this->add(array(
     'name' => 'title', 
     'attributes' => array(
      'type' => 'text', 
     ), 
     'options' => array(
      'label' => 'Title', 
     ), 
    )); 
    $this->add(array(
     'name' => 'description', 
     'attributes' => array(
      'type' => 'textarea', 
     ), 
     'options' => array(
      'label' => 'description', 
     ), 
    )); 
    $this->add(array(
     'name' => 'submit', 
     'attributes' => array(
      'type' => 'submit', 
      'value' => 'Save', 
      'id' => 'submitbutton', 
     ), 
    )); 
    } 
} 

这里是我createaction代码:

public function createAction() 
    { 
     $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default'); 
      $form = new CalendarForm(); 
      $update=false; 
      $message=''; 
      if($this->getRequest()->isPost()) 
      { 
       $post = $this->getRequest()->getPost(); 
       $form->setInputFilter($form->getInputFilter()); 
       $form->setData($post); 
       if($form->isValid()) 
       { 
        $formData=$form->getData(); 
        $s = new Calendar(); 
        $s->setProperty('calendar_id',$_post['calendar_id']); 
        $s->setProperty('user_id',1); 
        $s->setProperty('title',$post['title']); 
        $s->setProperty('description',$post['description']); 
        $dm->persist($s); 
        $dm->flush(); 
        $update=1; 
        $message='calendar Added Successfully.'; 
        //$form = new CalendarForm(); 
        //$this->redirect()->toRoute('calendar'); 
       } 
      } 
      return array('form' => $form, 'add_message' => $message, 'update' => $update, 'calendar'=>$this->calendar);    
     } 
+0

任何使用mongo odm在zf2中增加id的人? –

+0

这不是一个关于zf2的问题,它是关于doctrine2的,关于它的。关于'GeneratedValue'和'strategy'的手动约 – user1954544

回答

0

下面是一些学说2注释代码这台列“id”作为此表的主键并设置自动增量。

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

你的错误是,你说的

strategy="INCREMENT" 

代替

strategy="AUTO" 
+0

http://docs.doctrine-project.org/en/2.0.x/reference/basic-mapping.html#identifier-generation-strategies – user1954544