Serge Velikan
Serge Velikan

Reputation: 1171

Doctrine Entity "Class not found"

I'm proceeding to Doctrine's Getting Started guide and stuck in the beginning because of "Class 'Product' not found in /var/www/test/product-create.php on line 6":

<?php
require_once 'bootstrap.php';

$newProductName = $argv[1];

>>>>> $product = new Product();
$product->setName($newProductName);

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

echo sprintf('Created Product with ID %d' . PHP_EOL, $product->getId());

As written in guide, I have the Product class under "./src" directory in my project.

Please, help me, because I want to start using Doctrine without Symfony and I can't move any further.

Here is my bootstrap.php:

<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

use Symfony\Component\Yaml\Parser;

require 'vendor/autoload.php';

$yaml = new Parser();

$parameters = $yaml->parse(file_get_contents(__DIR__ . '/parameters.yml'));
$parameters = $parameters['parameters'];

$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . '/src'), $parameters['debug']);

$conn = array
    (
        'host'      => $parameters['database_host'],
        'port'      => $parameters['database_port'],
        'driver'    => $parameters['database_driver'],
        'user'      => $parameters['database_user'],
        'password'  => $parameters['database_password'],
        'dbname'    => $parameters['database_name']
    );

$entityManager = EntityManager::create($conn, $config);

And this is my Product.php:

<?php
/**
 * @Entity
 * @Table (name="products")
 **/
class Product
{
    /**
     * @Id
     * @Column(type="integer") @GeneratedValue
     **/
    protected $id;

    /**
     * @Column(type="string")
     **/
    protected $name;

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}

Thank you all in advance!

Upvotes: 3

Views: 5903

Answers (3)

Helmi Aziz
Helmi Aziz

Reputation: 53

I had the same issue and resolved it by configuring the autoload section in my composer.json file properly. Here are the steps I followed:


1.Basic Autoload Configuration:

Include the following code in your composer.json file:

"autoload": {
    "psr-4": {
        "": "src/"
    }
}

This configuration tells Composer to autoload classes from the src/ directory.

2.Using Namespaces:

If your product-create.php file uses the App\Entity namespace, you need to include it in the composer.json file as well:

"autoload": {
   "psr-4": {
      "App\\Entity\\": "src/"
   }
}

This configuration maps the App\Entity namespace to the src/ directory.

3.Custom Directory Paths:

If your entity classes are in a different directory, for example, src/entity/, update the path accordingly:

"autoload": {
    "psr-4": {
        "App\\Entity\\": "src/entity/"
    }
}

This configuration maps the App\Entity namespace to the src/entity/ directory.

4.Regenerate Autoload Files:

After making changes to the composer.json file, you need to regenerate the autoload files by running the following CLI command:

composer dump-autoload

This command updates the Composer autoload files to reflect the new autoload configuration.


So, you don't need to include require_once for each entity class you add.

Upvotes: 1

Mo Fetch
Mo Fetch

Reputation: 111

I just came across your question now, and hope that you found an answer, but to help others you need to make sure that you included in your composer.json the following

"autoload": {
    "psr-0": {"": "src/"}
}

Or include it manually like @Сергей Студеникин suggested

Upvotes: 5

I dont see where you include Product class. You need to write in the top of your file

require_once 'patch_to_your_class/Product.php';

or to use an autoloader for classes.

Upvotes: 4

Related Questions