简体   繁体   English

Doctrine 2中的动态表/实体名称

[英]Dynamic Table/Entity names in Doctrine 2

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. 我需要一个表示通用表的实体作为具有X id后缀的表的模型。 For example, I have an entity: CustomerX The tables I need to query are cusotmer_1, customer_2, customer_3...etc.. 例如,我有一个实体:CustomerX我需要查询的表是cusotmer_1,customer_2,customer_3 ......等等。

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. 所以问题是,我可以在第一次获得客户时正确设置,但第二次,doctrine生成的sql最终使用我创建的第一个表。

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'. 因此,如果我首先运行:CustomerX :: getCustomerRecords('123'),执行的sql和运行CustomerX :: getCustomerRecords('987')的sql仍在使用'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? 在Doctrine2中以编程方式修改表的模式名称?

Question is old, but it can be helpful for someone. 问题很老,但对某人有帮助。

If loadClassMetada called each time then it seems that issue in your code. 如果每次调用loadClassMetada,那么在您的代码中似乎存在问题。 But, I suppose, that metadata is cached by doctrine. 但是,我想,元数据是由学说缓存的。 In that case you can change it directly, please look following code snippet, it should work: 在这种情况下,您可以直接更改它,请查看以下代码片段,它应该工作:

<?php
class FooController extends Controller {
  function fooAction() {
    $em = $this->getDoctrine()->getEntityManager();
    $cm = $em->getClassMetadata('FooBundle:FooEntity');
    $cm->setTableName('special_table_name');
    $repo = $em->getRepository('FooBundle:FooEntity');
    $entities = $repo->createQueryBuilder('f')
            ->setMaxResults(1)
            ->orderBy('f.id', 'desc')
            ->getQuery()
            ->getResult();
    return new Response('');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM