原则实体“未找到类”; [英] Doctrine Entity "Class not found"
问题描述
由于在第6行的/var/www/test/product-create.php中未找到类'产品',我正在继续阅读《 Doctrine入门指南》,并停留在开头:
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());
按照指南中的说明,我在项目中的 ./src目录下有Product类。
As written in guide, I have the Product class under "./src" directory in my project.
请帮帮我,因为我想在没有Symfony的情况下开始使用Doctrine,因此我无法再前进了。
Please, help me, because I want to start using Doctrine without Symfony and I can't move any further.
这是我的bootstrap.php:
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);
这是我的Product.php:
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;
}
}
谢谢大家!
推荐答案
我看不到您在哪里包含产品类。您需要在文件顶部
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';
require_once 'patch_to_your_class/Product.php';
或在类上使用自动加载器。
or to use an autoloader for classes.
这篇关于原则实体“未找到类”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!