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

时间:2012-07-18 19:23:21

标签: php arrays cakephp

我在蛋糕中有一段代码,我从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的情况下执行此操作?

1 个答案:

答案 0 :(得分:0)

我明白了:

当使用caking SQL语句时,会返回一个3-D数组(当只请求一个字段或select时,为2-D)。它们的名称如下:

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'
    );

并按原样浏览SOURCE_IDROUTE_ID各行的值:

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