【问题标题】:Can't find entity model in Doctrine 2 / CodeIgniter 2在 Doctrine 2 / CodeIgniter 2 中找不到实体模型
【发布时间】:2012-12-27 16:16:07
【问题描述】:

我一直在关注一些让 Doctrine 运行的教程,但当我尝试将对象插入数据库时​​似乎挂断了。作为参考,这就是我所关注的:doctrine 2 tutorial

  • Doctrine 安装在 application/libraries 文件夹中
  • Doctrine.php 引导程序位于 application/libraries 文件夹中
  • 在 application/ 文件夹中创建一个 cli.php 文件
  • 教程没有说明我的第一个实体模型放在哪里,所以我把它放在 application/models 中

命名空间实体;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="user")
 */
class User
{

/**
 * @Id
 * @Column(type="integer", nullable=false)
 * @GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @Column(type="string", length=32, unique=true, nullable=false)
 */
protected $username;

/**
 * @Column(type="string", length=64, nullable=false)
 */
protected $password;

/**
 * @Column(type="string", length=255, unique=true, nullable=false)
 */
protected $email;

/**
 * The @JoinColumn is not necessary in this example. When you do not specify
 * a @JoinColumn annotation, Doctrine will intelligently determine the join
 * column based on the entity class name and primary key.
 *
 * @ManyToOne(targetEntity="Group")
 * @JoinColumn(name="group_id", referencedColumnName="id")
 */
protected $group;

}

/**
 * @Entity
 * @Table(name="group")
 */
class Group
{

/**
 * @Id
 * @Column(type="integer", nullable=false)
 * @GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @Column(type="string", length=32, unique=true, nullable=false)
 */
protected $name;

/**
 * @OneToMany(targetEntity="User", mappedBy="group")
 */
protected $users;

}
  • 在数据库中创建了我的架构没有问题:php cli.php orm:schema-tool:create
  • 得到了“使用原则”设置下的最后一步
  • 尝试在我的控制器中使用以下代码,但出现错误

    $em = $this->doctrine->em;
    
    $user = new models\User;
    $user->setUsername('Joseph');
    $user->setPassword('secretPassw0rd');
    $user->setEmail('josephatwildlyinaccuratedotcom');
    
    $em->persist($user);
    $em->flush();
    

生产

Fatal error: Class 'models\User' not found in C:\wamp\www\ci\application\controllers\Home.php on line 11

我唯一的想法是路径可能有问题,因为我在 Windows 中,或者我将实体模型放在了错误的位置。

【问题讨论】:

    标签: php codeigniter orm doctrine doctrine-orm


    【解决方案1】:

    来自the tutorial you're following,有一个重要的设置:

    // With this configuration, your model files need to be in
    // application/models/Entity
    // e.g. Creating a new Entity\User loads the class from
    // application/models/Entity/User.php
    $models_namespace = 'Entity';
    

    这是您的 Doctrine 实体(模型)必须使用的命名空间,看起来您做得正确,因为您将 namespace Entity; 作为模型的第一行。您可以将其设置为任何您想要的。

    使用此配置,您的模型文件需要在application/models/Entity

    当您创建实体实例时,请使用您配置的命名空间 - 而不是模型路径:

    // $user = new models\User; "models" is not the right namespace
    $user = new Entity\User;
    

    【讨论】:

    • 嗯,很奇怪,不知道为什么他会在教程中将其列为模型\用户。谢谢。
    • 啊,我没注意到。这可能是一个错误 - 但我很惊讶在 cmets 中没有任何关于它的内容(我只留下了一个)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2011-10-17
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多