繁体   English   中英

教义2注释问题

[英]Doctrine 2 annotation issue

我正在使用Doctrine 2.5.4和纯PHP 5编写自己的CMS。

这是我的CMS (GOOGLE链接)。

在构建时,我解决了这个错误:

错误NewsDAO:带有消息“类News”的异常“ Doctrine \\ ORM \\ Mapping \\ MappingException”不是有效的实体或映射的超类。 在/var/www/html/xxxxx.com/public_html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:346堆栈跟踪中:#0 /var/www/html/xxxxx.com/public_html /vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php(91):Doctrine \\ ORM \\ Mapping \\ MappingException :: classIsNotAValidEntityOrMappedSuperClass('News')#1 / var / www / html / xxxxx。 com / public_html / vendor / doctrine / orm / lib / Doctrine / ORM / Mapping / ClassMetadataFactory.php(151):Doctrine \\ ORM \\ Mapping \\ Driver \\ AnnotationDriver-> loadMetadataForClass('News',Object(Doctrine \\ ORM \\ Mapping \\ ClassMetadata))#2 /var/www/html/xxxxx.com/public_html/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(332):Doctrine \\ ORM \\ Mapping \\ ClassMetadataFactory-> doLoadMetadata(Object(Doctrine \\ ORM \\ Mapping \\ ClassMetadata),NULL,false,Array)#3 /var/www/html/xxxxx.com/public_html/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory。 php(78):原则\\公共\\持久性\\映射\\ AbstractClassMetadataFacto ry-> loadMetadata('新闻')#4 /var/www/html/xxxxx.com/public_html/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(216):Doctrine \\ ORM \\ Mapping \\ ClassMetadataFactory-> loadMetadata('News')#5 /var/www/html/xxxxx.com/public_html/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(281):Doctrine \\ Common \\ Persistence \\ Mapping \\ AbstractClassMetadataFactory-> getMetadataFor('News')#6 /var/www/html/xxxxx.com/public_html/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php(44):主义\\ ORM \\ EntityManager-> getClassMetadata('新闻')#7 /var/www/html/xxxxx.com/public_html/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(698):教义\\ ORM \\ Repository \\ DefaultRepositoryFactory-> getRepository(Object(Doctrine \\ ORM \\ EntityManager),'News')#8 /var/www/html/xxxxx.com/public_html/DAO/newsDAO.php(13):教义\\ ORM \\ EntityManager- > getRepository('新闻')#9 /var/www/html/xxxxx.com/public_html/handler/indexHDL.php(5):NewsDAO-> getAll()#10 / va r / www / html / xxxxx.com / public_html / manager / router.php(22):require_once('/ var / www / html / l ...')#11 /var/www/html/xxxxx.com/ public_html / manager / router.php(18):路由器->视图('索引')#12 /var/www/html/xxxxx.com/public_html/manager/router.php(13):路由器->路由() #13 /var/www/html/xxxxx.com/public_html/manager/router.php(8):路由器-> __ construct()#14 /var/www/html/xxxxx.com/public_html/manager/CMS.php (11):路由器-> getInstance()#15 /var/www/html/xxxxx.com/public_html/manager/CMS.php(6):CMS-> __ construct()#16 / var / www / html / xxxxx .com / public_html / index.php(18):CMS :: getInstance()#17 {main}注意:未定义变量:/var/www/html/xxxxx.com/public_html/DAO/newsDAO.php中的rs 17

我的newsDAO.php

<?php
require_once MDLDIR."news.php";
class NewsDAO{
public function __construct(){}

public function getAll(){   
    global $em; 
    //$tables = $em->getConnection()->getSchemaManager()->listTables();
    //var_dump($tables)--> return a lot of things from database;
    //$classes = array();
    //$metas = $em->getMetadataFactory()->getAllMetadata(); 
    //foreach ($metas as $meta) {
    //  $classes[] = $meta->getName();
    //}
    //var_dump($classes);exit();-->  return array(0){}
    try{    
        //global $em;
        $rs = $em->getRepository("News")->findAll();
    }catch (Exception $e){
        echo "ERROR NewsDAO: ".$e;
    }       
    return $rs;
  }             
}
?>

我的实体是news.php

<?php
 use Doctrine\ORM\Mapping AS ORM;
/**
*   @ORM\Entity 
*   @ORM\Table(name="Rich_news")
*/
class News{

/**
*   @nid 
*   @ORM\Column(type="integer")
*   @ORM\GeneratedValue     
*/
public $id;
/** @author @ORM\Column(type="string")*/
public $author;
/** @date @ORM\Column(type="integer")*/
public $date;
/** @title @ORM\Column(type="string")*/
public $title;
/**@content @ORM\Column(type="string")*/
public $content;
/**@full @ORM\Column(type="string")*/
public $full;
/**@title_en @ORM\Column(type="string")*/
public $title_en;
/**@content_en @ORM\Column(type="string")*/
public $content_en;
/**@full_en @ORM\Column(type="string")*/
public $full_en;
/**@flink @ORM\Column(type="string")*/
public $flink;
/**@img @ORM\Column(type="string")*/
public $img;    
}
?>

我在MySQL中的表Rich_news

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| nid        | int(11)      | NO   | PRI | NULL    | auto_increment |
| author     | varchar(50)  | NO   |     |         |                |
| date       | int(11)      | NO   |     | 0       |                |
| title      | varchar(255) | NO   |     |         |                |
| content    | text         | NO   |     | NULL    |                |
| full       | text         | NO   |     | NULL    |                |
| title_en   | varchar(255) | NO   |     |         |                |
| content_en | text         | NO   |     | NULL    |                |
| full_en    | text         | NO   |     | NULL    |                |
| flink      | text         | NO   |     | NULL    |                |
| img        | text         | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

我的Config.php

<?php
ini_set("display_errors",true); 
define('TIMER_START', microtime( true ) );  
define('DS', DIRECTORY_SEPARATOR );
define('ROOT_DIR', realpath( dirname( __FILE__ ) ). DS );   
define('DAODIR', ROOT_DIR.'DAO'.DS );
define('MNGDIR', ROOT_DIR.'manager'.DS );
define('HDLDIR' , ROOT_DIR.'handler'.DS );
define('TPLDIR', ROOT_DIR.'template'.DS );  
define('SKNDIR', TPLDIR.'skin'.DS );    
define('MDLDIR', ROOT_DIR.'model'.DS );             
define('DBN', 'xxxxx' );        
define('HOST', 'xxxxx' );       
define('USR', 'xxxxx' );
define('PWD','xxxxx');
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array(MDLDIR);
$isDevMode = false;

// the connection configuration
$dbParams = array(
    'driver'    => 'pdo_mysql',
    'host'      => HOST,
    'user'      => USR,
    'password'  => PWD,
    'dbname'    => DBN,
    'charset'   =>'utf8',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode); 
$em = EntityManager::create($dbParams, $config);
//$em->getConnection()->getDatabasePlatform()->registerDoctrinceTypeMapping('enum','string');

/*function exceptionHandler($exception){
    error_log($exception->getMessage());
} 

set_error_handler("exceptionHandler");*/

?>

我试图在news.php文件中找出问题所在。 为什么没有解决??? 大家可以帮我吗??? 提前致谢。

如“ Doctrine注释”文档中所述,添加use语句并检查正确的注释(是否为类使用命名空间?),例如:

<?php
use Doctrine\ORM\Mapping AS ORM;

 /**
 *   @ORM\Entity 
 *   @ORM\Table(name="news")
 */
 class News{
 /**
 *  @nid 
 *  @ORM\Column(type="integer")  // **my id column is nid** 
 *  @ORM\GeneratedValue     
 */
 public $id;

 /** @author 
  * @ORM\Column(type="string")*/
 public $author;

希望这个帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM