我的DAO班怎么能看到我的学说模型?

时间:2013-05-06 07:45:41

标签: php doctrine-orm

我决定将任何DAO操作从我的控制器类移动到DAO类(在java上只是用它)。

无论如何,当我尝试运行我的DQL时,doctrine抱怨它无法看到我的模型/实体。但我认为在DAO类的顶部声明它会阻止这一点。显然它没有。

你能帮帮忙吗?感谢

我的DAO课程

<?php

namespace Directory\Dao;

use Directory\Model\DirectoryModel;

use Doctrine\ORM\QueryBuilder,
    Doctrine\ORM\Query\Expr\Andx;

class DirectoryDao {
    /**
     * @var Doctrine\ORM\EntityManager
     */
    protected $entityManager;

    public function setEntityManager(EntityManager $entityManager) {
        $this->entityManager = $entityManager;
    }
    public function getEntityManager() {
        if (null === $this->entityManager) {
            $this->entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }
        return $this->entityManager;
    }

    public function searchStaffDirectory(QueryBuilder $queryBuilder, DirectoryModel $directory) {
        //$queryBuilder = $this->getEntityManager()->createQueryBuilder();
        $conditions = new Andx;

        $queryBuilder
                ->select('directory')
                ->from('DirectoryModel', 'directory');
        if (strlen($directory->surnameSearch)) {
             $conditions->add($queryBuilder->expr()->like('directory.surnameSearch', $directory->surnameSearch));
        }
        if (strlen($directory->surnameSoundsSearch)) {
             $conditions->add($queryBuilder->expr()->like('directory.surnameSoundsSearch', $directory->surnameSoundsSearch));
        }
        if (strlen($directory->firstnameSearch)) {
             $conditions->add($queryBuilder->expr()->like('directory.firsnameSearch', $directory->firstnameSearch));
        }
        if (strlen($directory->telephoneNumber)) {
             $conditions->add($queryBuilder->expr()->like('directory.telephoneNumber', $directory->telephoneNumber));
        }
        if (strlen($directory->departmentSearch)) {
             $conditions->add($queryBuilder->expr()->like('directory.departmentSearch', $directory->departmentSearch));
        }
        if (strlen($directory->roleSearch)) {
             $conditions->add($queryBuilder->expr()->like('directory.roleSearch', $directory->roleSearch));
        }
        if ($conditions->count()) {
                $queryBuilder->andWhere($conditions);
        }
        $queryBuilder
                ->orderBy('UPPER(directory.surname)')
                ->addOrderBy('UPPER(directory.firstname)');

        $query = $queryBuilder->getQuery();
        $result = $query->getResult();


        var_dump($queryBuilder->getDql());
    }
}
带有堆栈跟踪的

错误消息:

File:

    D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php:49

Message:

    [Semantical Error] line 0, col 22 near 'DirectoryModel': Error: Class 'DirectoryModel' is not defined.

Stack trace:

#0 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(429): Doctrine\ORM\Query\QueryException::semanticalError('line 0, col 22 ...', Object(Doctrine\ORM\Query\QueryException))
#1 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(854): Doctrine\ORM\Query\Parser->semanticalError('Class 'Director...', Array)
#2 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(1529): Doctrine\ORM\Query\Parser->AbstractSchemaName()
#3 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(1426): Doctrine\ORM\Query\Parser->RangeVariableDeclaration()
#4 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(1168): Doctrine\ORM\Query\Parser->IdentificationVariableDeclaration()
#5 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(757): Doctrine\ORM\Query\Parser->FromClause()
#6 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(726): Doctrine\ORM\Query\Parser->SelectStatement()
#7 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(229): Doctrine\ORM\Query\Parser->QueryLanguage()
#8 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(304): Doctrine\ORM\Query\Parser->getAST()
#9 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query.php(233): Doctrine\ORM\Query\Parser->parse()
#10 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\Query.php(245): Doctrine\ORM\Query->_parse()
#11 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php(737): Doctrine\ORM\Query->_doExecute()
#12 D:\work\eclipse\htdocs\directory\vendor\doctrine\orm\lib\Doctrine\ORM\AbstractQuery.php(538): Doctrine\ORM\AbstractQuery->execute(NULL, 1)
#13 D:\work\eclipse\htdocs\directory\module\Directory\src\Directory\Dao\DirectoryDao.php(59): Doctrine\ORM\AbstractQuery->getResult()
#14 D:\work\eclipse\htdocs\directory\module\Directory\src\Directory\Controller\DirectoryController.php(57): Directory\Dao\DirectoryDao->searchStaffDirectory(Object(Doctrine\ORM\QueryBuilder), Object(Directory\Model\DirectoryModel))
#15 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Directory\Controller\DirectoryController->searchAction()
#16 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#17 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(460): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#18 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(204): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#19 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#20 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#21 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#22 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(460): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#23 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(204): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#24 D:\work\eclipse\htdocs\directory\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(294): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#25 D:\work\eclipse\htdocs\directory\public\index.php(12): Zend\Mvc\Application->run()
#26 {main}

1 个答案:

答案 0 :(得分:0)

   $queryBuilder
                ->select('directory')
                ->from('DirectoryModel', 'directory');

我很确定你需要用命名空间

输入整个类名
   $queryBuilder
                ->select('directory')
                ->from('Directory\Model\DirectoryModel', 'directory');

如果模型由ORM管理,当然。