Doctrine Loader Entity

Asked

Viewed 39 times

1

By uploading the project Doctrine cannot load the files from the Entity folder.

bootstrap.php

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


require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../config/config.php';

$entitiesPath = array(__DIR__.'/Blog/Entity');


$config = Setup::createAnnotationMetadataConfiguration($entitiesPath, $dev);
$entityManager = EntityManager::create($dbParams, $config);

config.php

$dbParams = [
    'driver' => 'pdo_mysql',
    'host' => '127.0.0.1',
    'dbname' => 'blog',
    'user' => 'root',
    'password' => ''
];

$dev = true;

Post.php

namespace Blog\Entity;

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Column;


/**
 * Blog Post entity
 *
 * @Entity
 * @Table(indexes={
 *      @Index(name="publication_date_idx", columns="publicationDate")
 * })
 */
class Post
{
    /**
     * @var int
     *
     * @Id
     * @GeneratedValue
     * @Column(type="integer")
     */
    protected $id;
    /**
     * @var string
     *
     * @Column(type="string")
     */
    protected $title;
    /**
     * @var string
     *
     * @Column(type="text")
     */
    protected $body;
    /**
     * @var \DateTime
     *
     * @Column(type="datetime")
     */
    protected $publicationDate;



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

    /**
     * Set title
     *
     * @param string $title
     * @return Post
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set body
     *
     * @param string $body
     * @return Post
     */
    public function setBody($body)
    {
        $this->body = $body;

        return $this;
    }

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

    /**
     * Set publicationDate
     *
     * @param \DateTime $publicationDate
     * @return Post
     */
    public function setPublicationDate($publicationDate)
    {
        $this->publicationDate = $publicationDate;

        return $this;
    }

    /**
     * Get publicationDate
     *
     * @return \DateTime 
     */
    public function getPublicationDate()
    {
        return $this->publicationDate;
    }
}

index php.

/**
 * Lists all blog posts
 */

require_once __DIR__.'/../src/bootstrap.php';

/** @var $posts \Blog\Entity\Post[] Retrieve the list of all blog posts */
$posts = $entityManager->getRepository('Blog\Entity\Post')->findAll();
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>My blog</title>
</head>
<body>
<h1>My blog</h1>

<?php foreach ($posts as $post): ?>
    <article>
        <h1>
            <?=htmlspecialchars($post->getTitle())?>
        </h1>
        Date of publication: <?=$post->getPublicationDate()->format('Y-m-d H:i:s')?>

        <p>
            <?=nl2br(htmlspecialchars($post->getBody()))?>
        </p>

        <ul>
            <li>
                <a href="edit-post.php?id=<?=$post->getId()?>">Edit this post</a>
            </li>
            <li>
                <a href="delete-post.php?id=<?=$post->getId()?>">Delete this post</a>
            </li>
        </ul>
    </article>
<?php endforeach ?>
<?php if (empty($posts)): ?>
    <p>
        No post, for now!
    </p>
<?php endif ?>

<a href="edit-post.php">
    Create a new post
</a>
</html>

When executing the index.php file, the message below informs that the class has not been loaded. How to fix?

Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' with message 'Class 'Blog\Entity\Post' does not exist' in /Users/israel/Sites/blog/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96 Stack trace: #0 /Users/israel/Sites/blog/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(41): Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('Blog\\Entity\\Pos...') #1 /Users/israel/Sites/blog/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(281): Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getParentClasses('Blog\\Entity\\Pos...') #2 /Users/israel/Sites/blog/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(311): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getParentClasses('Blog\\Entity\\Pos...') #3 /Users/israel/Sites/blog/vendor/doctrine/common/lib/Doctrine/Common/Persis in /Users/israel/Sites/blog/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php on line 96
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.