2013-10-11 106 views
5

我讀這篇文章:Symfony2的 - 學說:架構:更新失敗實體包(去耦)外

http://danielribeiro.org/yes-you-can-have-low-coupling-in-a-symfony-standard-edition-application/

筆者提到的,它可能有這種artitcture爲項目:

src/ 
└── Vendor/ 
    └── Product/ 
     └── Bundle 
      └── BlogBundle/ 
      └── ForumBundle/ 
      └── SiteBundle/ 
       └── Controller/ 
        └── IndexController.php 
       └── Resources/ 
        └── views/ 
         └── index.html.twig 
       └── ProductSiteBundle.php 
     └── Entity 
      └── User.php 
     └── Repository 
      └── UserRepository.php 
     └── Service 
      └── UserPasswordRetrievalService.php 

,所以我跟着文章,並與像這樣結束了:

src/ 
└── Product/ 
    └── Bundle 
     └── SiteBundle/ 
      └── Controller/ 
       └── IndexController.php 
      └── Resources/ 
       └── views/ 
        └── index.html.twig 
      └── ProductSiteBundle.php 
    └── Entity 
     └── User.php 

現在的Symfony不能看到我的user.php的,如果我有添加任何額外的代碼,使這個作品的作者並沒有提到,現在我得到這個錯誤:

MappingException: The class 'Product\Entity\User' was not found in the chain configured namespaces OtherNameSpaces 

UPDATE

所以我刪除了我現有的代碼。並沒有這樣的事情:

src/ 
└── Product/ 
    └── Bundle 
     └── SiteBundle/ 
      └── Controller/ 
       └── IndexController.php 
      └── Resources/ 
       └── views/ 
        └── index.html.twig 
      └── ProductSiteBundle.php 
    └── Entity 
     └── User.php 

user.php的

namespace Product\Entity; 

/** 
* @ORM\Entity 
* @ORM\Table(name="users") 
*/ 
class User 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    public function __construct() 
    { 
     parent::__construct(); 

    } 
} 

然後

php app/console doctrine:schema:update --force //No Metadata Classes to process. 

似乎Symfony的不知道該文件夾的所有。有什麼地方可以讓symfony查看文件夾裏面的內容嗎?

+0

什麼是你在user.php的命名空間? – Tib

+0

'命名空間Product \ Entity' @Tib – trrrrrrm

+0

@nifr答案就在它之上。我的文章,你第一次聽到這種做法,並沒有解釋(有意)在內部如何做。 –

回答

11

A good blog article作者Jakub Zalas描述瞭如何映射生活在捆綁之外的實體。

如下圖所示,您需要手動添加教義查找映射信息的位置。

這也使映射信息可用於doctrine:schema:update命令。不要忘記在配置更改後清除緩存。

# app/config/config.php 
doctrine: 
    orm: 
     # ... 
     mappings: 
      Acme: 
       type: annotation 
       is_bundle: false 
       dir: %kernel.root_dir%/../src/Acme/Entity 
       prefix: Acme\Entity 
       alias: Acme 

您現在可以訪問這樣的存儲庫(因爲別名定義的):

$entityManager->getRepository('Acme:YourEntity'); 
+0

完美工作,謝謝! – trrrrrrm