我有两个表news和news_category。为此,我使用symfony命令"doctrine:mapping:convert"创建了两个映射类。两个文件如下:
-
news.orm.yml .
News: type: entity table: news fields: newsId: id: true type: integer unsigned: false nullable: false column: news_id generator: strategy: IDENTITY newsTitle: type: string length: 255 fixed: false nullable: false column: news_title newsDescription: type: text nullable: false column: news_description newsStatus: type: string length: 255 fixed: false nullable: false column: news_status createdAt: type: date nullable: false column: created_at manyToOne: category: targetEntity: NewsCategory cascade: { } mappedBy: null inversedBy: null joinColumns: category_id: referencedColumnName: category_id orphanRemoval: false lifecycleCallbacks: { }
2)。NewCategory.orm.yml
NewsCategory:
type: entity
table: news_category
fields:
categoryId:
id: true
type: integer
unsigned: false
nullable: false
column: category_id
generator:
strategy: IDENTITY
categoryTitle:
type: string
length: 255
fixed: false
nullable: false
column: category_title
categoryDescription:
type: text
nullable: false
column: category_description
lifecycleCallbacks: { }
之后,我使用了另一个symfony命令"doctrine:mapping:import",使用这个,我再次在实体文件夹News.php和newcategory .php中生成两个文件
如下所示。
1) news.php
<?php
namespace AdminNewsBundleEntity;
use DoctrineORMMapping as ORM;
/**
* News
*
* @ORMTable(name="news")
* @ORMEntity
*/
class News
{
/**
* @var integer
*
* @ORMColumn(name="news_id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $newsId;
/**
* @var string
*
* @ORMColumn(name="news_title", type="string", length=255, nullable=false)
*/
private $newsTitle;
/**
* @var string
*
* @ORMColumn(name="news_description", type="text", nullable=false)
*/
private $newsDescription;
/**
* @var string
*
* @ORMColumn(name="news_status", type="string", length=255, nullable=false)
*/
private $newsStatus;
/**
* @var DateTime
*
* @ORMColumn(name="created_at", type="date", nullable=false)
*/
private $createdAt;
/**
* @var NewsCategory
*
* @ORMManyToOne(targetEntity="NewsCategory")
* @ORMJoinColumns({
* @ORMJoinColumn(name="category_id", referencedColumnName="category_id")
* })
*/
private $category;
}
2) NewCategory.php
namespace AdminNewsBundleEntity;
use DoctrineORMMapping as ORM;
/**
* NewsCategory
*
* @ORMTable(name="news_category")
* @ORMEntity
*/
class NewsCategory
{
/**
* @var integer
*
* @ORMColumn(name="category_id", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
/**
* @var string
*
* @ORMColumn(name="category_title", type="string", length=255, nullable=false)
*/
private $categoryTitle;
/**
* @var string
*
* @ORMColumn(name="category_description", type="text", nullable=false)
*/
private $categoryDescription;
}
问题是现在当我使用"doctrine:generate: Entities"创建实体时它给了我以下错误:
D:wampwwwSymfony>php app/console doctrine:generate:entities AdminNewsBundle
Generating entities for bundle "AdminNewsBundle"
[DoctrineCommonPersistenceMappingMappingException]
Invalid mapping file 'Admin.NewsBundle.Entity.News.orm.yml' for class 'Admi
nNewsBundleEntityNews'.
doctrine:generate:entities [--path="..."] [--no-backup] name
对不起,我的英语不好,请帮助我解决这个问题,因为我是新的symfony2
尝试:
1) php app/console doctrine:mapping:convert yml ./src/Admin/NewsBundle/Resources/config/doctrine/metadata/orm --from-database --force --namespace="Admin\NewsBundle\Entity\"
for Linux 命名空间="Admin\NewsBundle\Entity\" for Win probably 命名空间="AdminNewsBundleEntity\"
注意映射在正确的位置,具有正确的名称和正确的语法。
2) php app/console doctrine:mapping:import AdminNewsBundle annotation
3) php app/console doctrine:generate:entities AdminNewsBundle
尝试用nspace
替换实体名称的第一行YMLAdminNewsBundleEntityNews:
比如。