如果在查询

时间:2016-03-26 10:59:10

标签: php symfony pdo doctrine

我遇到Symfony / Doctrine的问题

我为1个实体使用多个表格( table_entity_1 table_entity_2 ),由于某种原因$cm->setPrimaryTable(array('name' => $tableName)); 不会切换$query=$em->createQuery('...')

的表格

我知道使用正确的表名称调用$cm->setPrimaryTable(array('name' => $tableName));

dump($em->getClassMetadata('ENTITY')->getTableName());也显示正确的表格

然而,如果我做2个查询,第二个查询使用错误的表,它使用与第一个相同的

这是我的代码:

public function getAlertDataForReporting($deviceId, $refreshTimestamp)
{
    $tableName = $deviceId . "_alert";

    $em = $this->getEntityManager('s1data');
    $cm = $em->getClassMetadata('S1CoreBundle:S1DeviceDataAlert');
    $cm->setPrimaryTable(array('name' => $tableName));
    $cm->setTableName($tableName);


    dump($em->getClassMetadata('S1CoreBundle:S1DeviceDataAlert')->getTableName());

    $query=$em->createQuery('
        SELECT
        a.acknowledges,
        a.alertId,
        a.errorType,
        a.timestamp,
        a.component,
        a.sensorCounterkey,
        a.alertData,
        a.errorCode,
        a.bus,
        a.address
        FROM S1CoreBundle:S1DeviceDataAlert a
        WHERE a.errorType != 100
        AND a.errorReportedByEmail IS NULL
        AND a.statusType > 20
        AND UNIX_TIMESTAMP(a.timestamp) < :refreshTimestamp
        ORDER BY a.timestamp DESC'
    )->setParameter('refreshTimestamp', $refreshTimestamp);



    dump($query->getSQL());
    //dump($query->getParameters());

    $alertData = $query->getResult();

    //dump($alertData);


    return $alertData;
}

以下是结果输出:

&#34; 103_alert&#34;

&#34; SELECT t0_.acknowledges AS acknowledges_0, t0_.alert_id AS alert_id_1, t0_.error_type AS error_type_2, t0_.timestamp AS timestamp_3, t0_.component AS component_4, t0_.sensor_counterkey AS sensor_counterkey_5, t0_.alert_data AS alert_data_6, t0_.error_code AS error_code_7, t0_.bus AS bus_8, t0_.address AS address_9 FROM **103_alert** t0_ WHERE t0_.error_type <> 100 AND t0_.errorreportedbyemail IS NULL AND t0_.status_type > 20 AND UNIX_TIMESTAMP(t0_.timestamp) < ? ORDER BY t0_.timestamp DESC&#34;

&#34; 160_alert&#34;

&#34; SELECT t0_.acknowledges AS acknowledges_0, t0_.alert_id AS alert_id_1, t0_.error_type AS error_type_2, t0_.timestamp AS timestamp_3, t0_.component AS component_4, t0_.sensor_counterkey AS sensor_counterkey_5, t0_.alert_data AS alert_data_6, t0_.error_code AS error_code_7, t0_.bus AS bus_8, t0_.address AS address_9 FROM **103_alert** t0_ WHERE t0_.error_type <> 100 AND t0_.errorreportedbyemail IS NULL AND t0_.status_type > 20 AND UNIX_TIMESTAMP(t0_.timestamp) < ? ORDER BY t0_.timestamp DESC&#34;

&#34; 408_alert&#34;

&#34; SELECT t0_.acknowledges AS acknowledges_0, t0_.alert_id AS alert_id_1, t0_.error_type AS error_type_2, t0_.timestamp AS timestamp_3, t0_.component AS component_4, t0_.sensor_counterkey AS sensor_counterkey_5, t0_.alert_data AS alert_data_6, t0_.error_code AS error_code_7, t0_.bus AS bus_8, t0_.address AS address_9 FROM **103_alert** t0_ WHERE t0_.error_type <> 100 AND t0_.errorreportedbyemail IS NULL AND t0_.status_type > 20 AND UNIX_TIMESTAMP(t0_.timestamp) < ? ORDER BY t0_.timestamp DESC&#34;

注意每个查询中的FROM 103_alerts

所以我的问题是我在切换Enitity的表名时,DML仍在每个查询中使用第一个查询的表

请注意,代码黑色是从服务调用的,并且位于自定义存储库

0 个答案:

没有答案