2013-03-05 101 views
1

我使用Zend Framework 1與Bisna庫集成了Doctrine 2.我使用Doctrine 2 CLI從我的數據庫模型生成了我的實體。除了關聯記錄的setter方法外,這一切都正常工作。他們接受的參數必須是特定的命名空間(這裏的\Category)。Doctrine 2命名空間問題

class Article 
{ 
    public function setCategory(\Category $category = null) { 
     $this->category = $category; 
     return $this; 
    } 
} 

然而,當我這樣做:

$article = $this->em->getRepository('\Application\Entity\Article')->find(1); 

$category = new \Application\Entity\Category(); 
$category->SetName('New Category'); 

$article->setCategory($category); 

我得到以下致命錯誤:Argument 1 passed to Application\Entity\CategoryField::setCategory() must be an instance of Category, instance of Application\Entity\Category given

當我改變setter方法來接受\Application\Entity\Category對象時,它當然在工作。我應該爲每個生成的方法執行此操作,還是有其他選項?這是我第一次使用名稱空間,所以它可能很簡單。更多信息有關use

http://php.net/manual/en/language.namespaces.importing.php否則,你就必須:

回答

0

您可以隨時添加到您的類文件的頂部:use \Application\Entity\Category;,然後簡單地以後引用它,像這樣:public function setCategory(Category $category = null)

退房參考完整的命名空間,否則您的應用程序不知道\Category是參考\Application\Entity\Category