我有一个站点记录,其中可以包含任意数量的具有不同信息的 AdProvider 配置字段。不幸的是,fieldNames(提供者的名称)是独一无二的,而且还会有更多。我可以在 Document 中将它们中的每一个硬编码为哈希类型,但是每次添加新的 Provider 时我都必须更新 Document。
我想动态修改 Document 本身,查看我可以从另一个 Mongo 集合中获得的提供者列表,但我不知道如何做到这一点。
我的第一次尝试是在 loadClassMetaData 事件上创建一个侦听器并映射新字段。我看到了字段映射,但它们没有反映在文档中。显然,这些字段没有任何 getter 和 setter,所以我尝试使用魔术 __get 和 __set 方法访问它们,但我得到它们不存在的错误。
也许我会以错误的方式解决这个问题?
示例 Mongo 记录:
{
"_id" : ObjectId("4ff1d29d99c6667722000000"),
"_type" : [
"Models_Site"
],
"enabledAdProviders" : [
"provider1",
"provider2",
"provider3",
"provider4"
],
"provider1" : {
"id" : "4028cbff38e2d7c00666fd2fdc770208"
},
"provider2" : {
"placements" : {
"Top_300x50" : "477",
"Btm_300x50" : "478",
"Top_320x50" : "477",
"Btm_320x50" : "478"
}
},
"provider3" : {
"id" : "8a809449013331fdcdc6662708532b20"
},
"siteId" : "PsTl",
"siteName" : "Publisher Site",
"provider4" : {
"placements" : {
"Top_300x50" : "430",
"Btm_300x50" : "430"
}
}
}
我的听众:
<?php
namespace BIM\DataBundle\Listener;
use BIM\DataBundle\Document\AdPublisherRecord;
use BIM\DataBundle\Document\AdProviderRecord;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
class AdPublisherSiteSetup
{
private $serviceContainer;
/**
* This service is called every time Ads doctrine odm loads a document.
* We are dynamically creating the ad provider setting nodes on the AdPublisher Record
*
*/
public function __construct($serv){
$this->serviceContainer = $serv;
}
public function loadClassMetadata(\Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs $args)
{
$metaData = $args->getClassMetadata();
$document = (string)$metaData->getName();
if($document == "BIM\DataBundle\Document\AdPublisherRecord"){
//query for ad providers
//create as a hash type to store each providers settings.
$providerList = $this->serviceContainer->get('ads.publisher.factory')->getProviderList();
foreach ($providerList as $name => $value) {
$metaData->mapField(array('fieldName' => $name, 'type' => 'hash'));
}
}
}
}