我在过去看到过很多关于这个主题的问题,但没有一个有有效的解决方案。我在看Symfony的书和食谱。我在其中一个示例中使用doctrine来填充getter/setter方法,但是当我试图在另一个示例中重复它时,它不起作用。当我回到前面的练习时,它也停止工作了。
的命令php app/console doctrine:generate:entities AppBundle/Entity/User
给出错误
的命令[学说' ORM映射' ' MappingException]
类"AppBundle'Entity'User"
不是一个有效的实体或映射的超类。
php app/console doctrine:mapping:info
给出错误
(异常)根据当前配置,您没有任何映射的Doctrine ORM实体。如果你有实体或映射文件,你应该检查你的映射配置是否有错误。
这是正在讨论的类:
<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Symfony'Component'Security'Core'User'UserInterface;
/**
* @ORM'Entity(repositoryClass="AppBundle'Entity'UserRepository")
* @ORM'Table(name="app_users")
*/
class User implements UserInterface, 'Serializable {
/**
* @ORM'Column(type="integer")
* @ORM'Id
* @ORM'GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM'Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM'Column(type="string", length=64)
*/
private $password;
/**
* @ORM'Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM'Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct(){
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
public function getUsername(){
return $this->username;
}
public function getSalt(){
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword(){
return $this->password;
}
public function getRoles(){
return array('ROLE_USER');
}
public function eraseCredentials(){
}
/** @see 'Serializable::serialize() */
public function serialize(){
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see 'Serializable::unserialize() */
public function unserialize($serialized){
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
}
在config.yml:
中的Doctrine配置 # Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
我AppKernel.php <?php
use Symfony'Component'HttpKernel'Kernel;
use Symfony'Component'Config'Loader'LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony'Bundle'FrameworkBundle'FrameworkBundle(),
new Symfony'Bundle'SecurityBundle'SecurityBundle(),
new Symfony'Bundle'TwigBundle'TwigBundle(),
new Symfony'Bundle'MonologBundle'MonologBundle(),
new Symfony'Bundle'SwiftmailerBundle'SwiftmailerBundle(),
new Symfony'Bundle'AsseticBundle'AsseticBundle(),
new Doctrine'Bundle'DoctrineBundle'DoctrineBundle(),
new Sensio'Bundle'FrameworkExtraBundle'SensioFrameworkExtraBundle(),
new AppBundle'AppBundle(),
new Acme'DemoBundle'AcmeDemoBundle(),
new Acme'TestBundle'AcmeTestBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony'Bundle'DebugBundle'DebugBundle();
$bundles[] = new Symfony'Bundle'WebProfilerBundle'WebProfilerBundle();
$bundles[] = new Sensio'Bundle'DistributionBundle'SensioDistributionBundle();
$bundles[] = new Sensio'Bundle'GeneratorBundle'SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}
编辑:此外,我尝试用Doctrine在相同的bundle和命名空间中生成一个新实体,没有出现任何问题。尝试使用doctrine:generate:entities再次访问该实体(在添加@ORM' entity和所有这些之后)再次给我同样的错误。所以文件名和命名空间都是正确的
正确的命令:
php app/console doctrine:generate:entities AppBundle:User
尝试:
php app'console doctrine:generate:entity
这将带您到Doctrine2实体生成器:
此命令帮助您生成Doctrine2实体。
这基本上意味着你必须为用户数据库定义表结构(它需要将db表与它正在创建的实体"映射")。