我一直在遵循一些教程,让学说运行,似乎挂了,当我试图插入一个对象到数据库。作为参考,这是我所遵循的:原则2教程
- Doctrine安装在application/libraries文件夹
- Doctrine.php bootstrapper在application/libraries文件夹 在application/文件夹中创建一个cli.php文件。
- 教程没有说在哪里放我的第一个实体模型,所以我把它放在应用程序/模型
名称空间实体;
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 form:schema-tool:create
- 在Using Doctrine设置下完成最后一步
-
尝试在我的控制器中使用以下代码,并得到一个错误
$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
我唯一的想法是,可能有一些路径,因为我在窗口,或者我把我的实体模型在错误的地方。
在您所遵循的教程中,有一个重要的设置:
// 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;