我知道这似乎是一个非常基本的问题,但在投票否决之前,请阅读到底。
当使用Codeigniter和条令2时,我想到了这个问题。
我注意到,至少在使用注释时,synthax与将Doctrine 2与Symfony2一起使用时略有不同,我想知道为什么。
我将提供一个例子:
这是Symfony2中的一个类,具有多对一关系:
<?php
namespace PondipLakeBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineORMMapping as ORM;
/**
* Lake
*
* @ORMTable(name="pondip_lake")
* @ORMEntity(repositoryClass="PondipLakeBundleEntityLakeRepository")
*/
class Lake
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var integer
* @AssertType(type="PondipLakeBundleEntityVenue")
*
* @ORMManyToOne(targetEntity="PondipLakeBundleEntityVenue")
* @ORMJoinColumn(name="venue_id", referencedColumnName="id")
*/
private $venue;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var text
*
* @ORMColumn(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var integer
*
* @ORMManyToOne(targetEntity="PondipUserAccountBundleEntityUser")
* @ORMJoinColumn(name="created_by", referencedColumnName="id", nullable=true)
*/
private $createdBy;
请注意所有的@ORM\。
现在,当将其与codeigner2/Doctrine2.3.0一起使用时,我的控制台中存在映射错误。但是,在遵循一些教程时,我最终删除了所有的ORM(以及相应的使用Doctrine\ORM\Mapping作为ORM),它起到了作用。
这项工作:
<?php
namespace Entity;
/**
* Friend Model
*
* @Entity
* @Table(name="friend")
* @author Miles Yohann Merran <hello_miles@hotmail.com>
*/
class Friend
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="string", length=32, unique=true, nullable=false)
*/
protected $name;
/**
* @Column(type="text", nullable=false)
*/
protected $description;
/**
* @Column(type="string", length=64, nullable=false)
*/
protected $picture;
/**
* @Column(type="string", length=64, nullable=false)
*/
protected $genre;
/**
* @ManyToOne(targetEntity="User", inversedBy="friends")
* @JoinColumn(nullable=false)
*/
protected $user;
/**
* @var datetime $date
*
* @Column(name="date", type="datetime")
*/
protected $date;
当这不起作用时(注意ORM)
<?php
namespace Entity;
use DoctrineORMMapping as ORM;
/**
* Friend Model
*
* @Entity
* @Table(name="friend")
* @author Miles Yohann Merran <hello_miles@hotmail.com>
*/
class Friend
{
/**
* @Id
* @Column(type="integer", nullable=false)
* @GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Column(type="string", length=32, unique=true, nullable=false)
*/
protected $name;
/**
* @Column(type="text", nullable=false)
*/
protected $description;
/**
* @Column(type="string", length=64, nullable=false)
*/
protected $picture;
/**
* @Column(type="string", length=64, nullable=false)
*/
protected $genre;
/**
* @ORMManyToOne(targetEntity="User", inversedBy="friends")
* @ORMJoinColumn(nullable=false)
*/
protected $user;
/**
* @var datetime $date
*
* @ORMColumn(name="date", type="datetime")
*/
protected $date;
为什么?有什么区别?
编辑:确切地说,我尝试了两个结构相同的实体,一个在avery注释语句之前有@ORM\,另一个没有。在使用终端命令时,带有@ORM\的一个没有得到条令2的正确管理,而没有的一个工作得很好。我找不到任何关于这方面的文件,即使我在doc和src中清楚地看到有Doctrine Common、Doctrine ORM和Doctrine DBAL。他们彼此之间不兼容吗?如何正确管理这种差异?尤其是在使用S以外的其他框架(如CI2)时
感谢
长话短说,在symfony中,条令注释读取器是AnnotationReader
,而在使用条令Setup::createAnnotationMetadataConfiguration
助手的CI中,默认情况下您有SimpleAnnotationReader
。
通过将false
传递给Setup::createAnnotationMetadataConfiguration
的第五个参数$useSimpleAnnotationReader
,您应该能够具有与symfony中相同的行为。