使用cake/sql时的数组元素命名


Array element naming when using cake/sql

我在cake中有一段代码,我从SQL中获取多行数据,其中一列中有一个数值,我需要检查,如果它等于某个数字,则将数据更改为文本。为了做到这一点,我需要知道数组元素是如何命名为$results[????]的,以便获得并更改这个值。那么,在使用SQL/Cake时,数组的命名约定是什么?

这是结块的代码:

$params = array(
    'fields' => array(
        $this->name . '.AUTHORIZE_PROVIDER_NAME',           
        $this->name . '.SOURCE_ID',
        $this->name . '.ORDER_ITEM_TITLE',
        $this->name . '.DOSE_AMOUNT',      
        $this->name . '.DOSE_UNIT',
        $this->name . '.DT_CREATED_TIME',
        $this->name . '.ROUTE_ID',
        $this->name . '.SEQUENCE_NO',
        $this->name . '.LOCATION',
        $this->name . '.BODY_SITE_ID',
        $this->name . '.COMMENT', 
        'DD.DICTIONARY_DATA_CODE',
    ),
    /*
    'conditions' => array(
        //conditions
        $this->name . '.HID'    => $hospital_id,
        $this->name . '.PID'    => $patient_id,                
    ),
    */
    'order' => array(
        $this->name . '.DT_CREATED_TIME',
    ),
    'joins' => array(
        array(
            'table'     => 'DICTIONARY_DATA',
            'alias'     => 'DD',
            'type'      => 'INNER',
            'fields'    => 'DD.DICTIONARY_DATA_CODE as DD_Code',
            'conditions'=> array(
                $this->name . '.PRIORITY_ID = DD.DICTIONARY_DATA_ID',
                $this->name . '.HID' => $hospital_id,
                $this->name . '.PID' => $patient_id,
            )
        )
    ),
);
$rs = $this->find('all', $params);

我在这里得到数据:

foreach ($rs as $record){
    try {
        $result[] = $record[$this->name];
        array_push($result, $record['DD']);
    }
}

并将其返回以作为JSON对象打印出来。所以我想进入$results[]来检查SOURCE_IDROUTE_ID的数值。如果不做foreach,我怎么能做到这一点?

我发现了:

当使用组合SQL语句时,会返回一个三维数组(当只请求一个字段或select时,返回二维数组)。它们的名称如下:

Array(
    Array[table_name] =>
        [column_name] => field value
        [column_name] => field value
        .
        .
    Array[table_name] =>
        [column_name] => field value
        [column_name] => field value
        .
        .
    .
    .
);

当每个元素都通过foreach语句运行时,元素会变为数字[table_name][column_name],现在是[0][1],等等,这取决于它在数组中的位置。

为了检查ROUTE_IDSOURCE_ID的数值,我创建了一个类似的哈希表

$sourceValues = array(
        500002 => 'Verbal',
        500003 => 'Telephone',
        500004 => 'Written',
        500005 => 'Other'
    );
$routeValues = array(
        11     => 'Intramuscular',
        22     => 'Nasal',
        28     => 'Subcutaneous'
    );

并对CCD_ 12和CCD_

foreach($record as $value){
           $source = $value['SOURCE_ID'];
           $route = $value['ROUTE_ID'];
           $value['SOURCE_ID'] = $sourceValues[$source];
           $value['ROUTE_ID'] = $routeValues[$route];
           $result[] = $value;
    }