doudeng2016
2012-09-14 20:51
浏览 34
已采纳

Doctrine 2找不到实体类

I can't get Doctrine to find my entities classes which lays in www/entities/..

I'm keeping getting the error "Fatal error: Class 'Event' not found in /home/dxs/public_html/create-event.php on line 7".. And i feel like i tried almost everything..

The file which im calling is:

create-event.php:

<?php
require_once("bootstrap.php");
require_once("entities/Event.php");
$name = $argv[1];
$description = $argv[2];

$event1 = new Event;
$event1->setName("test");
$event1->setDescription("testdesc");

$entityManager->persist($event1);
$entityManager->flush();

echo "Created Event with ID " . $event1->getId() . "
";

Bootstrap.php

<?php
use Doctrine\ORM\EntityRepository;

if(!class_exists("Doctrine\Common\Version", FALSE))

{

    require_once 'bootstrap_doctrine.php';

}

Bootstrap

<?php
use Doctrine\ORM\EntityRepository;

if(!class_exists("Doctrine\Common\Version", FALSE))
{

    require_once 'bootstrap_doctrine.php';

}

Updated bootstrap doctrine

 <?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));

use Doctrine\ORM\Tools\Setup;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;

require_once "Doctrine/ORM/Tools/Setup.php";

Setup::registerAutoloadPEAR();
$cl = new Doctrine\Common\ClassLoader('Entities', __DIR__);
$cl->register();

$isDevMode = true;

$path = array(__DIR__.'/entities');
$config = Setup::createAnnotationMetadataConfiguration($path, $isDevMode);

// database configuration parameters
$conn = array(
    'driver' => 'pdo_mysql',
    'user' => 'dxs',
    'password' => 'pass',
    'dbname' => 'db'

);
// obtaining the entity manager
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);   

AnnotationRegistry::registerFile("/usr/local/lib/php/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
AnnotationRegistry::registerAutoloadNamespace("Symfony\Component\Validator\Constraint", "/usr/local/lib/php/Doctrine/Symfony");
AnnotationRegistry::registerAutoloadNamespace("MyProject\Annotations", ROOT.DS.'www');

$reader = new AnnotationReader();
AnnotationReader::addGlobalIgnoredName('dummy');

What am i doing wrong?

Updated event class with annotations

Event class

<?php
namespace Entities\Event;
use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @Table(name="events")
 * @Annotations\Event
 */
class Event
{
    /** @ORM\Id @GeneratedValue
     *  @var int
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotEmpty
     * @var string
     */
    protected $name;
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqing7789 2012-09-14 21:56
    已采纳

    See this doc doctrine annotations for the correct way to define your entity.

    E.g. you need the * @ORM\Entity in your opening comment.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题