如何在zf2中的doctrine2 odm中设置自动递增id


how to set auto increment id in doctrine2 odm in zf2?

我在zf2中使用了doctrine2 odm,我想制作自动递增id,我将日历id制作为自动递增,但它不保存在数据库中,我如何制作日历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 );               
        }

这里有一些Doctrine 2注释代码,它将列"id"设置为该表的主键,并设置自动增量。

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

你的错误是你说

strategy="INCREMENT"

而不是

strategy="AUTO"

Doctrine2和mongoDB=bab思想。有一个图书馆https://github.com/coen-hyde/Shanty-Mongo它将处理你所需要的一切。