Question

i added a module name calender in skelton application but when i want to create calender it gives me error

class 'Calendar\Document\Calendar' was not found in the chain configured namespaces \Document

here is my my module.config.php code:

<?php
return array(
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index'
                )
            )
        )
    )
),
'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_driver' => array(
            'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
        ),
        'odm_default' => array(
            'drivers' => array(
                __NAMESPACE__ .'\Document' => __NAMESPACE__ . '_driver'
            )
        )
    )
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        //'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),
);

and here is my calendar document:

 <?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="string") */
private $calendar_id;

/** @ODM\Field(type="string") */
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'=>$this->calendar_id,
         'user_id'=>$this->$this->user_id,
         'title'=>$this->title,
         'description'=>$this->description    
     );
}
  }

and here is my module.config.doctrine.mongo.odm.local.php:

 <?php
 return array(
'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController'
    ),
),
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index'
                )
            )
        )
    )
),
'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_driver' => array(
            'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
        ),
        'odm_default' => array(
            'drivers' => array(
                __NAMESPACE__ .'\Document' => __NAMESPACE__ . '_driver'
            )
        )
    )
),
'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
        'Zend\Log\LoggerAbstractServiceFactory',
    ),
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        //'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),
);

and here is my controller create action:

 <?php
namespace Calendar\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Calendar\Document\Calendar;
use Calendar\Form\CalendarForm;

  public function createAction()
    {
        //if ($this->zfcUserAuthentication()->hasIdentity())
        //{
        $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
            $form = new CalendarForm();
            if($this->getRequest()->isPost())
    {
        $post = $this->getRequest()->getPost();
        $form->setInputFilter($form->getInputFilter());
        $form->setData($post);
        //echo('<pre>');var_dump($Data);var_dump($post);echo($_POST['username']);echo($post['username']);exit;
        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();
            //echo new Response($s->getProperty('id'));
            //
            $message='calendar Added Successfully.';
            $form = new CalendarForm();
        }
    }
    return array( 'form' => $form, 'add_message' => $message, 'update' => $update, 'user'=>$this->user );

how i remove this error?

Was it helpful?

Solution

In your module.config.php you are using the __NAMESPACE__ constant when declaring your doctrine driver spec. For that to work, you need to declare the namespace in your config file...

<?php
// module.config.php - declare the namespace for this module
namespace Calendar;

return array(

    // ...

    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
                'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
            ),
            'odm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ .'\Document' => __NAMESPACE__ . '_driver'
                )
            )
        )
    ),

    // ...

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