模型使用MySQL视图,某些字段不在同一级别/层次结构中

时间:2014-10-16 13:10:52

标签: php cakephp cakephp-2.0 cakephp-2.4

我已配置模型以使用视图。在我的控制器中查询此视图时,返回的记录集有两个字段与其余字段不在同一级别/ hiearchy。

在这种情况下,请参阅ast (asset_id and asset_configuration_id)

在dbForge中手动查询视图,我得到一个平坦的记录集。我无法弄清楚为什么会这样。

型号:

<?php
App::uses('AppModel', 'Model');

class AssetConfigurationDeploymentStatus extends AppModel {
    public $useTable = "asset_configuration_deployment_statuses";

}
?>

查询view asset_configuration_deployment_statuses:

SELECT `ac`.`id` AS `asset_configuration_id`
     , `ast`.`id` AS `asset_id`
     , `ac`.`domain_id` AS `domain_id`
     , `ac`.`server_id` AS `server_id`
     , `e`.`acronym` AS `environment`
     , `e`.`id` AS `environment_id`
     , `d`.`name` AS `domain`
     , `lds`.`deployed_by` AS `last_deployed_by`
     , `lds`.`deployed_date` AS `last_deployed_date`
     , `lds`.`revision` AS `last_deployed_revision`
     , `lds`.`is_failed` AS `last_deployed_is_failed`
     , `lss`.`deployed_by` AS `last_successful_deployed_by`
     , `lss`.`deployed_date` AS `last_successful_deployed_date`
     , `lss`.`revision` AS `last_successful_deployed_revision`
     , `lss`.`is_failed` AS `last_successful_deployed_is_failed`
     , `eds`.`count_failed_deployments` AS `count_failed_deployments`
     , `er`.`last_successful_environment_revision` AS `last_successful_environment_revision`
     , `ast`.`asset_group_id` AS `asset_group_id`
FROM
  (((((((((`assets` `ast`
JOIN `asset_configurations` `ac`
ON ((`ast`.`id` = `ac`.`asset_id`)))
JOIN `last_deployment_statuses` `lds`
ON ((`ac`.`id` = `lds`.`asset_configuration_id`)))
JOIN `last_successful_deployment_statuses` `lss`
ON ((`ac`.`id` = `lss`.`asset_configuration_id`)))
JOIN `last_environment_deployment_statuses` `eds`
ON ((`ac`.`id` = `eds`.`asset_configuration_id`)))
JOIN `domains` `d`
ON ((`ac`.`domain_id` = `d`.`id`)))
JOIN `servers` `s`
ON ((`ac`.`server_id` = `s`.`id`)))
JOIN `environments_servers` `es`
ON ((`s`.`id` = `es`.`server_id`)))
JOIN `environments` `e`
ON ((`es`.`environment_id` = `e`.`id`)))
JOIN `last_successful_environment_deployment_revision` `er`
ON (((`ac`.`asset_id` = `er`.`asset_id`) AND (`e`.`id` = `er`.`environment_id`))))

控制器代码:

...

$this->loadModel("AssetConfigurationDeploymentStatus");
$all_deploy_items = $this->AssetConfigurationDeploymentStatus->find('all', array(
        'order'=>array('AssetConfigurationDeploymentStatus.environment_id','AssetConfigurationDeploymentStatus.domain'), 
        'conditions' => array('AssetConfigurationDeploymentStatus.asset_id' => $id)
    )
);

foreach($all_deploy_items as $conf){

    var_dump($conf);
    exit;
    ...
}

...

转储输出:

array (size=2)
  'AssetConfigurationDeploymentStatus' => 
    array (size=16)
      'asset_configuration_id' => string '172' (length=3)
      'domain_id' => string '21' (length=2)
      'server_id' => string '10' (length=2)
      'environment' => string 'DEV' (length=3)
      'environment_id' => string '4' (length=1)
      'domain' => string 'my.domain.here' (length=28)
      'last_deployed_by' => string 'user' (length=7)
      'last_deployed_date' => string '2014-06-23 12:05:24' (length=19)
      'last_deployed_revision' => string '644' (length=3)
      'last_deployed_is_failed' => boolean false
      'last_successful_deployed_by' => string 'user' (length=7)
      'last_successful_deployed_date' => string '2014-06-23 12:05:24' (length=19)
      'last_successful_deployed_revision' => string '644' (length=3)
      'last_successful_deployed_is_failed' => boolean false
      'count_failed_deployments' => string '0' (length=1)
      'last_successful_environment_revision' => string '1930' (length=4)
  'ast' => 
    array (size=2)
      'asset_id' => string '47' (length=2)
      'asset_group_id' => string '28' (length=2)

0 个答案:

没有答案