课程'日历\文件\日历'在链配置的名称空间\ Document中找不到?

时间:2014-04-15 15:03:39

标签: php mongodb zend-framework2 doctrine-odm

我在skelton应用程序中添加了一个模块名称日历,但是当我想创建日历时它给了我错误

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

这是我的module.config.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',
    ),
),
);

这是我的日历文件:

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

这是我的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',
    ),
),
);

这是我的控制器创建动作:

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

我如何删除此错误?

1 个答案:

答案 0 :(得分:1)

在您的module.config.php中,在宣布您的学说驱动程序规范时,您正在使用__NAMESPACE__常量。为此,您需要在配置文件中声明命名空间...

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

    // ...

);