2013-10-11 108 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