2012-12-27 47 views
1

我一直在按照一些教程來獲取Doctrine的運行,並且當我嘗試將一個對象插入數據庫時​​似乎掛斷了。作爲參考,這是我在下面:doctrine 2 tutorial在Doctrine 2/CodeIgniter中找不到實體模型2

  • 學說是安裝在應用程序/庫文件夾
  • Doctrine.php引導程序將在應用程序/庫文件夾
  • 一個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; 

} 
  • 創造了我在DB模式沒有問題:PHP cli.php ORM:架構工具:創建
  • 拿到「使用原則」下的最後一步設定
  • 嘗試使用在我的控制器下面的代碼,並得到了一個錯誤

    $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 

我唯一的想法是,可能有路徑的東西,因爲我在窗口或我把我的實體模型放在錯誤的地方。

回答

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'; 

這是您的學說實體(機型)必須使用它似乎你正在做正確,你有namespace Entity;作爲第一線的命名空間,你的模型。你可以將它設置爲任何你想要的。

利用這種配置,你的模型文件必須在application/models/Entity

當您創建實體的情況下,使用您配置的命名空間 - 而不是模型路徑:

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

嗯,奇怪,不知道爲什麼他會在教程中將其列爲模型\用戶。謝謝。 –

+0

啊,我沒有注意到。這可能是一個錯誤 - 但我很驚訝,評論中沒有任何關於它的內容(我剛剛離開了一個)。 –

相關問題