在Symfony/ORM中生成实体getter和setter
我有以下仅具有属性的ORM Symfony实体:在Symfony/ORM中生成实体getter和setter,symfony,annotations,doctrine,getter-setter,Symfony,Annotations,Doctrine,Getter Setter,我有以下仅具有属性的ORM Symfony实体: <?php namespace Evr\HomeBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="ev_article") * @ORM\Entity */ class Article { /** * * @ORM\Column(name="article_id", type="integer") *
<?php
namespace Evr\HomeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="ev_article")
* @ORM\Entity
*/
class Article
{
/**
*
* @ORM\Column(name="article_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles")
* @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id")
*/
private $subcategory;
/**
*
* @ORM\Column(type="string",length=512)
*/
private $title;
/**
*
* @ORM\Column(type="text")
*/
private $content;
/**
*
* @ORM\Column(type="text")
*/
private $exclusive_content;
/**
*
* @ORM\Column(type="date")
*/
private $creation_date;
/**
*
* @ORM\Column(type="integer")
*/
private $views;
/**
*
* @ORM\Column(type="integer")
*/
private $votes;
}
每次执行此操作时,它都会显示以下错误消息:
[Doctrine\ORM\Mapping\MappingException]
Class "Evr\HomeBundle\Entity\Article" is not a valid entity or mapped super
class.
doctrine:generate:entities [--path="..."] [--no-backup] name
我不知道它为什么不生成实体,实体/注释是否有问题?试试:
php app/console doctrine:generate:entities EvrHomeBundle:Article
如果您使用的是symfony 3.0或更高版本,请将应用程序替换为bin:
php bin/console doctrine:generate:entities EvrHomeBundle:Article
如果您使用的是symfony 4+,则:
php bin/console make:entity --regenerate
尝试删除此实体并使用下一个命令重新生成它们:
php app/console doctrine:generate:entity --entity="EvrHomeBundle:Article" --fields="name:string(255) content:text exclusive_content:text creation_date:date views:integer votes:integer"
然后手动添加:
/**
*
* @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles")
* @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id")
*/
private $subcategory;
用法:
orm:生成实体目标路径
控制台中的示例:
orm:generate entities--generate annotations=“true”destination\u path
来源:认为缺少*是解决方案之一 但是在我的例子中,当从命令提示符创建实体时,我更喜欢配置格式为YML,而不是注释 现在我所做的是使用注释给出映射命令,所以它不起作用 尝试将Resources/config/Category.orm.yml配置为:
AppBundle\Entity\Category:
type: entity
table: null
repositoryClass: AppBundle\Repository\CategoryRepository
oneToMany:
products:
targetEntity: Product
mappedBy: Category
AppBundle\Entity\Product:
type: entity
table: null
repositoryClass: AppBundle\Repository\ProductRepository
manyToOne:
category:
targetEntity: Category
inversedBy: products
joinColumn:
name: category_id
referenceColumnName: id
并将Resources/config/Product.orm.yml更改为:
AppBundle\Entity\Category:
type: entity
table: null
repositoryClass: AppBundle\Repository\CategoryRepository
oneToMany:
products:
targetEntity: Product
mappedBy: Category
AppBundle\Entity\Product:
type: entity
table: null
repositoryClass: AppBundle\Repository\ProductRepository
manyToOne:
category:
targetEntity: Category
inversedBy: products
joinColumn:
name: category_id
referenceColumnName: id
我觉得这不是一个错误,而是一个更好的理解 对ORM也要进行计算,以生成getter/setter:
/**
* @var date
*
* @ORM\Column(name="creation_date", type="date")
*/
这将自动在实体文件中生成所有必要的getter和setter
如果您想具体说明这些表,请使用以下方法:
php bin/console doctrine:generate:entities AppBundle:"TABLE_NAME"
用表名替换“表名”。映射导入(从数据库)
从映射生成实体,但不使用getter和setter
php bin/console doctrine:mapping:convert annotation ./src
php bin/console doctrine:generate:entities AppBundle/Entity
或
通过使用getter和setter映射生成Entityes
php bin/console doctrine:mapping:convert annotation ./src
php bin/console doctrine:generate:entities AppBundle/Entity
我试过了,不管使用哪个命令,问题依然存在。嗯,我复制/粘贴了您的实体代码,并更改了名称空间以适合我的一个项目,效果很好。真奇怪!。我有许多其他类与这个实体有关系。与子类别类似,子类别又与您缓存的类Categoryclear相关,然后重试。还要检测映射错误,请执行
php应用程序/控制台原则:映射:信息I清除缓存,然后重试:但问题仍然存在…当我运行命令原则:映射:信息时,它显示1条消息(找到1个映射实体)映射实体就是实体用户。。。(事实上,我在bundle中有5个以上的实体,即使它只显示了1个映射实体)您修改了什么吗?因为即使您进行了修改,问题仍然存在。我删除了子类别
注释确定,我更新了答案,请尝试生成此实体。这有用吗?不,它不会生成getter setter。。。你说你删除了子类别注释?它仍然在回答中对不起,我没有注意到,您实际上从所有注释中删除了一个@
,这会使注释无效…非常感谢您花时间添加另一个答案。这个答案也是另一个可行的解决方案,+1。。三年前我问过这个问题:)最好!你的解决方案在Symfony 3.4上对我有效,你+1,你能告诉我为什么Symfony不在任何地方记录它吗?@Dung我不知道Dung。他们应该是诚实的。非常感谢你花时间补充另一个答案,它也有效+1:)我三年前问过这个问题。干杯非常感谢您抽出时间讨论这个有效的解决方案。我三年前问过这个问题。祝你一切顺利+1感谢您添加此有效解决方案,我在3年前提出了此问题,再次感谢+1感谢您,此问题很老,但您的答案将非常有用+1