Question

i used doctrine2 odm in zf2 and i want to make auto increment id,i make calendar id as auto increment but it does not save in database,how i make calendar id auto increment and save in database? here is my code:

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

and here is my form:

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

and here is my createaction code:

 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 );               
        }
Was it helpful?

Solution

Here is some Doctrine 2 Annotation Code which sets the Column "id" as primary key for this Table and sets the autoincrement.

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

Your mistake is that you said

strategy="INCREMENT"

instead of

strategy="AUTO"

OTHER TIPS

Doctrine2 and mongoDB = bab idea. There is a library https://github.com/coen-hyde/Shanty-Mongo that will handle all you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top