1

我在 CodeIgniter 控制器中尝试使用 Doctrine 模型时收到“消息:class_parents():类注释不存在且无法加载”。

这是完整的堆栈跟踪致命错误:在 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService.php 中未捕获异常 'ReflectionException' 并带有消息 'Class Comment does not exist' :73 堆栈跟踪:#0 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService.php(73): ReflectionClass->__construct('Comment') #1 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\Mapping\ClassMetadataInfo.php(769): Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getClass('Comment') #2 C :\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php(591): Doctrine\ORM\Mapping\ClassMetadataInfo->initializeReflection(Object(Doctrine\Common\Persistence\Mapping\RuntimeReflectionService)) #3 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\Mapping\ClassMetadataFactory.php(272): Doctrine\ORM \Mapping\ClassMetadataFactory->initializ in C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\Common\Persistence\Mapping\RuntimeReflectionService.php 在第 73 行

这是我在 /application/models/Entities/Comment.php 中的模型

    <?php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entities\Comment
 */
class Comment
{
    /**
     * @var integer $id
     */
    private $id;

    /**
     * @var integer $parentid
     */
    private $parentid;

    /**
     * @var integer $isactive
     */
    private $isactive;

    /**
     * @var integer $isremoved
     */
    private $isremoved;

    /**
     * @var datetime $removaldate
     */
    private $removaldate;

    /**
     * @var string $user_name
     */
    private $user_name;

    /**
     * @var string $user_email
     */
    private $user_email;

    /**
     * @var string $user_avatar
     */
    private $user_avatar;

    /**
     * @var text $comment
     */
    private $comment;

    /**
     * @var datetime $creationdate
     */
    private $creationdate;

    /**
     * @var integer $rating
     */
    private $rating;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set parentid
     *
     * @param integer $parentid
     * @return Comment
     */
    public function setParentid($parentid)
    {
        $this->parentid = $parentid;
        return $this;
    }

    /**
     * Get parentid
     *
     * @return integer 
     */
    public function getParentid()
    {
        return $this->parentid;
    }

    /**
     * Set isactive
     *
     * @param integer $isactive
     * @return Comment
     */
    public function setIsactive($isactive)
    {
        $this->isactive = $isactive;
        return $this;
    }

    /**
     * Get isactive
     *
     * @return integer 
     */
    public function getIsactive()
    {
        return $this->isactive;
    }

    /**
     * Set isremoved
     *
     * @param integer $isremoved
     * @return Comment
     */
    public function setIsremoved($isremoved)
    {
        $this->isremoved = $isremoved;
        return $this;
    }

    /**
     * Get isremoved
     *
     * @return integer 
     */
    public function getIsremoved()
    {
        return $this->isremoved;
    }

    /**
     * Set removaldate
     *
     * @param datetime $removaldate
     * @return Comment
     */
    public function setRemovaldate($removaldate)
    {
        $this->removaldate = $removaldate;
        return $this;
    }

    /**
     * Get removaldate
     *
     * @return datetime 
     */
    public function getRemovaldate()
    {
        return $this->removaldate;
    }

    /**
     * Set user_name
     *
     * @param string $userName
     * @return Comment
     */
    public function setUserName($userName)
    {
        $this->user_name = $userName;
        return $this;
    }

    /**
     * Get user_name
     *
     * @return string 
     */
    public function getUserName()
    {
        return $this->user_name;
    }

    /**
     * Set user_email
     *
     * @param string $userEmail
     * @return Comment
     */
    public function setUserEmail($userEmail)
    {
        $this->user_email = $userEmail;
        return $this;
    }

    /**
     * Get user_email
     *
     * @return string 
     */
    public function getUserEmail()
    {
        return $this->user_email;
    }

    /**
     * Set user_avatar
     *
     * @param string $userAvatar
     * @return Comment
     */
    public function setUserAvatar($userAvatar)
    {
        $this->user_avatar = $userAvatar;
        return $this;
    }

    /**
     * Get user_avatar
     *
     * @return string 
     */
    public function getUserAvatar()
    {
        return $this->user_avatar;
    }

    /**
     * Set comment
     *
     * @param text $comment
     * @return Comment
     */
    public function setComment($comment)
    {
        $this->comment = $comment;
        return $this;
    }

    /**
     * Get comment
     *
     * @return text 
     */
    public function getComment()
    {
        return $this->comment;
    }

    /**
     * Set creationdate
     *
     * @param datetime $creationdate
     * @return Comment
     */
    public function setCreationdate($creationdate)
    {
        $this->creationdate = $creationdate;
        return $this;
    }

    /**
     * Get creationdate
     *
     * @return datetime 
     */
    public function getCreationdate()
    {
        return $this->creationdate;
    }

    /**
     * Set rating
     *
     * @param integer $rating
     * @return Comment
     */
    public function setRating($rating)
    {
        $this->rating = $rating;
        return $this;
    }

    /**
     * Get rating
     *
     * @return integer 
     */
    public function getRating()
    {
        return $this->rating;
    }
}

这是控制器的代码

<?php
require APPPATH . 'models\Entities\Comment.php';

class CommentController extends CI_Controller{
    var $em;
    function __construct() {
        parent::__construct();
        $this->em = $this->doctrine->em;
    }
    public function index()
    {
        $comment = $this->em->find('Comment', 1);
        echo $comment->user_name . '<br/>' . $comment->comment;
    }
}

这是教义.php

<?php


class Doctrine
{
    // the Doctrine entity manager
    public $em = null;

    public function __construct()
    {
        // include our CodeIgniter application's database configuration
        require_once APPPATH.'config/database.php';

        // include Doctrine's fancy ClassLoader class
        require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';

        // load the Doctrine classes
        $doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH.'libraries');
        $doctrineClassLoader->register();

        // load Symfony2 helpers
        // Don't be alarmed, this is necessary for YAML mapping files
        $symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', APPPATH.'libraries/Doctrine');
        $symfonyClassLoader->register();

        // load the entities
        $entityClassLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'models');
        $entityClassLoader->register();

        // load the proxy entities
        $proxyClassLoader = new \Doctrine\Common\ClassLoader('Proxies', APPPATH.'models');
        $proxyClassLoader->register();

        // set up the configuration
        $config = new \Doctrine\ORM\Configuration;

        if(ENVIRONMENT == 'development')
            // set up simple array caching for development mode
            $cache = new \Doctrine\Common\Cache\ArrayCache;
        else
            // set up caching with APC for production mode
            $cache = new \Doctrine\Common\Cache\ApcCache;
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);

        // set up proxy configuration
        $config->setProxyDir(APPPATH.'models/Proxies');
        $config->setProxyNamespace('Proxies');

        // auto-generate proxy classes if we are in development mode
        $config->setAutoGenerateProxyClasses(ENVIRONMENT == 'development');

        // set up annotation driver
        $yamlDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(APPPATH.'models\Mappings');
        $config->setMetadataDriverImpl($yamlDriver);

        // Database connection information
        $connectionOptions = array(
            'driver' => 'pdo_mysql',
            'user' => $db['default']['username'],
            'password' => $db['default']['password'],
            'host' => $db['default']['hostname'],
            'dbname' => $db['default']['database']
        );

        // create the EntityManager
        $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);

        // store it as a member, for use in our CodeIgniter controllers.
        $this->em = $em;
    }
}

不知道出了什么问题。我是 php 和它的技术的新手。请帮忙。任何有用的想法将不胜感激

4

1 回答 1

6

在字符串中,您始终使用完全限定的类名

这条线必须从

$comment = $this->em->find('Comment', 1);

$comment = $this->em->find('Entities\Comment', 1);
于 2013-02-27T10:00:34.803 回答