我在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',
),
));
}
}
这是我的创建代码:
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 :(得分:0)
以下是一些Doctrine 2注释代码,它将列“id”设置为此Table的主键并设置自动增量。
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
你的错误是你说的
strategy="INCREMENT"
而不是
strategy="AUTO"
答案 1 :(得分:0)
Doctrine2和mongoDB = bab的想法。有一个库https://github.com/coen-hyde/Shanty-Mongo可以满足您的所有需求。