原则2中的动态表/实体名称 [英] Dynamic Table/Entity names in Doctrine 2

查看:134
本文介绍了原则2中的动态表/实体名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以清楚地看到我的代码正在发生什么。



我需要一个表示一个通用表的实体作为模型具有X id后缀的表。例如,我有一个实体:CustomerX
我需要查询的表是cusotmer_1,customer_2,customer_3 ...等。



我正在使用:

  class CustomerX {
/ **
* CustomerX
*
* @Table(name =customer_)
* @Entity
* /


// .....属性和setters / getters ...

private $ _tableName = null;

public function getTableName(){
return $ this-> _tableName;
}

public function setTableName($ tableName){
$ this-> _tableName = $ tableName;
return $ this;
}

public function loadClassMetadata(LoadClassMetadataEventArgs $ eventArgs)
{
$ classMetadata = $ eventArgs-> getClassMetadata();

$ table = $ classMetadata-> table;
$ table ['name'] ='customer _'。$ this-> getTableName();
$ classMetadata-> setPrimaryTable($ table);
}


public static function getCustomerRecords($ CustomerId){
$ em = \Helper_Doctrine :: em();

$ custTable = new \ME\CustomerX();
$ custTable-> setTableName($ CustomerId);
$ evm = $ em-> getEventManager();
$ evm-> addEventListener(\Doctrine\ORM\Events :: loadClassMetadata,$ custTable);

//获取客户信息
$ query = $ em-> createQuery(
'SELECT w
FROM \ME\CustomerX w
WHERE w.customerId =:CustId';
$ query-> setParameter('CustId',$ CustomerId);
$ custParams = $ query-> getResult();

$ evm-> removeEventListener(\Doctrine\ORM\Events :: loadClassMetadata,$ custTable);
$ em-> flush();

return $ custParams;
}

}

所以问题是,我可以得到这个设置正确的第一次我通过获得一个客户,但第二次,由doctrine生成的sql最终使用我创建的第一个表。



所以如果我运行:CustomerX :: getCustomerRecords('123')首先,执行的sql,当我运行CustomerX :: getCustomerRecords('987')仍然在使用'customer_123'。



我必须做错事,如果有任何建议关于如何正确删除或重置表名称到新的东西,这将是伟大的。



谢谢。



<我最初使用这个作为参考。
在Doctrine2中以编程方式修改表的模式名称

解决方案

问题很旧,但对某人来说可能会有所帮助。



如果loadClassMetada每次调用,那么这似乎是您的代码中的问题。
但是,我想,元数据是由教义缓存的。
在这种情况下,您可以直接更改它,请查看以下代码片段: https:// gist。 github.com/marzocchi/3237804 ,它应该可以工作。



谢谢,


I am hoping someone can shed some light on what is going on with my code.

I need to have an entity that represents a generic table as a model for tables with X id suffix. For example, I have an entity: CustomerX The tables I need to query are cusotmer_1, customer_2, customer_3...etc..

I am currently using:

class CustomerX {
/**
 * CustomerX
 *
 * @Table(name="customer_")
 * @Entity
 */


//..... properties and setters/getters....

private $_tableName = null;

public function getTableName() {
    return $this->_tableName;
}

public function setTableName($tableName) {
    $this->_tableName = $tableName;
    return $this;
}

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
    $classMetadata = $eventArgs->getClassMetadata();

    $table = $classMetadata->table;
    $table['name'] = 'customer_'.$this->getTableName();
    $classMetadata->setPrimaryTable($table);
}


public static function getCustomerRecords($CustomerId) {
    $em = \Helper_Doctrine::em();

    $custTable = new \ME\CustomerX();
    $custTable->setTableName($CustomerId);
    $evm = $em->getEventManager();
    $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $custTable);

    //get the customer info
    $query = $em->createQuery(
        'SELECT w
         FROM \ME\CustomerX w
         WHERE w.customerId = :CustId';
    $query->setParameter('CustId', $CustomerId);
    $custParams = $query->getResult();

    $evm->removeEventListener(\Doctrine\ORM\Events::loadClassMetadata, $custTable);
    $em->flush();

    return $custParams;
}

}

So the issue is, i can get this to set correctly the first time I run through getting a customer, but then the second time, the sql generated by doctrine ends up using the first table I created.

So if i run: CustomerX::getCustomerRecords('123') first, the sql that gets executed and when I run CustomerX::getCustomerRecords('987') still is using 'customer_123'.

I must be doing something wrong. If anyone has any suggestions on how to correctly remove or reset the table name to something new, that would be great.

Thanks.

I initially used this as reference. Programmatically modify table's schema name in Doctrine2?

解决方案

Question is old, but it can be helpful for someone.

If loadClassMetada called each time then it seems that issue in your code. But, i suppose, that metadata is cached by doctrine. In that case you can change it directly, please look following code snippet: https://gist.github.com/marzocchi/3237804 , it should work.

Thanks,

这篇关于原则2中的动态表/实体名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆