[英]Symfony2 - doctrine:schema:update fails with entity outside of a bundle ( decoupled )
我正在讀這篇文章:
http://danielribeiro.org/yes-you-can-have-low-coupling-in-a-symfony-standard-edition-application/
作者提到,該項目可以采用這種方法:
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查看該文件夾嗎?
Jakub Zalas的一篇很好的博客文章描述了如何映射生活在bundle之外的實體。
您需要手動添加doctrine查找映射信息的位置,如下所示。
這使得映射信息可用於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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.