Here is my code:

This class is the CaBase class

<?php
class CaBase extends AppModel {

//--------------------------------------------------------------------------

public $useDbConfig = 'db_chartaccess';

/**
 * IMPORTANT
 * Child class MUST override this variable!
 */
public $name = 'CaBase';

/**
 * IMPORTANT
 * Child class MUST override this variable!
 */
public $useTable = '';

/**
 * IMPORTANT
 * Child class MUST override this variable!
 */
public $primaryKey = '';

/**
 * IMPORTANT
 * The child class MUST override this method to return the proper value for
 * its primary key based on the $hospital_id, $patient_id, and $admission_id
 */
protected function getPrimaryKeyValue(
    $hospital_id,
    $patient_id,
    $admission_id = null
) {
    return -1;
}

//--------------------------------------------------------------------------
//The following are some common convenience functions.
//--------------------------------------------------------------------------

public function findById($id) {

    $conditions = array(
        $this->name . "." . $this->primaryKey => $id,
    );
    $rs = $this->find('all', array('conditions' => $conditions));
    return $rs;
}

public function findByPatient(
    $hospital_id,
    $patient_id,
    $admission_id = null
) {
    $id = $this->getPrimaryKeyValue($hospital_id,$patient_id,$admission_id);
    return $this->findById($id);
}

public function wsGetDetails($id) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    $rs = $this->findById($id);

    if ( 1 != count($rs) ) {
        $msg = $msg_prefix;
        if ( 1 > count($rs) ) {
            $msg .= "Could not find the record with id=" . $id;
        } else { // now it must be 1 < count($rs)
            $msg .= "Found more than 1 record with id=" . $id;
        }
        $ret = WsResponse::getResponse_Error($msg);
        return $ret;
    }

    $msg = $msg_prefix . "Record obtained successfully! (id = " . $id . ")";
    $ret = new WsResponse(
        Constants::STATUS_SUCCESS,
        $msg,
        $rs[0][$this->name]
    );

    return $ret;

}

/**************************************************************************/
/**************************************************************************/

/**
 * "Field Processing" Framework
 *
 * The constants/variables/functions below form the framework/mechanism of
 * field-processing.
 *
 * These constants/variables/functions should be defined in this "base"
 * class only. No child class needs to override them.
 *
 */

//Note
//I define these constants here instead of the global Constants class
//because these constants are only being used in the model class files
//which are the child classes of this class.
//--------------------------------------------------------------------------
const KEY_FIELD_LOGIC_NAME = 'FIELD_LOGIC_NAME';
const KEY_MAPPING_LOGIC_COMPLEXITY = 'FIELD_MAPPING_LOGIC_COMPLEXITY';
const KEY_FIELD_QUESTION_ID = 'FIELD_QUESTION_ID';
/**
 * LEVEL1 means: simple standard logic (ie. field exists in a table)
 */
const LEVEL1_COMPLEXITY = 'LEVEL1_COMPLEXITY';
/**
 * LEVEL2 means: complicated (eg. field does not exist in a table, instead,
 * it corresponds to the result of a complex query, or computation, etc.)
 */
const LEVEL2_COMPLEXITY = 'LEVEL2_COMPLEXITY';
/**
 * LEVEL3 means: complicated and passing identifying variable
 *(eg. field does not exist in a table, instead,
 * it corresponds to the result of a complex query, or computation, etc.)
 */
const LEVEL3_COMPLEXITY = 'LEVEL3_COMPLEXITY';
/**
 * CONVERT_FROM_REQUEST_TO_ME: Indicates the value should be converted from
 * the format found in the request to the current model's format.
 */
const CONVERT_FROM_REQUEST_TO_ME = 'CONVERT_FROM_REQUEST_TO_ME';
/**
 * CONVERT_FROM_ME_TO_RESPONSE: Indicates the value should be converted from
 * the current model's format to the format required by the response.
 */
const CONVERT_FROM_ME_TO_RESPONSE = 'CONVERT_FROM_ME_TO_RESPONSE';

/**
 * IMPORTANT
 * Child class MUST override this variable!
 * 
 * IMPORTANT
 * 1) If KEY_MAPPING_LOGIC_COMPLEXITY is Level1, then the value of
 *    KEY_FIELD_LOGIC_NAME MUST be the actual name of the field on the table
 *    in ChartAccess database.
 * 2) If KEY_MAPPING_LOGIC_COMPLEXITY is Level2, then the value of
 *    KEY_FIELD_LOGIC_NAME MUST be the name of a method to be executed for
 *    the field.
 */
protected $filedMethodMappings = array();
//$filedMethodMappings has the following structure:
//  array(
//      'Exposed name of field1'
//          => array(
//                KEY_MAPPING_LOGIC_COMPLEXITY => 'LEVEL1 | LEVLE2 | ...'
//                KEY_FIELD_LOGIC_NAME => 'Name of the logic (eg. method
//                                         name) to be executed for this
//                                         field',
//             ),
//      ......
//  );
//

/**
 * IMPORTANT
 * This method NEEDS TO BE OVERRIDDEN by the child class in MOST cases!
 * When $direction = CONVERT_FROM_REQUEST_TO_ME:
 *   The value passed in that is going to be saved into this model needs to
 *   be changed (eg. the value passed in is a lookup value in String format,
 *   but the field of this model is expecting an integer ID value),
 * When $direction = CONVERT_FROM_ME_TO_RESPONSE:
 *   The value that is going to be passed out from this model and going to
 *   be used in the response needs to be changed (eg. the value in this
 *   model is an interger ID value of a lookup value, but the response is
 *   expecting the lookup value to be in String format),
 */
protected function convertFieldValue($direction, $ca_field, $value) {
    //This base class only implements the most basic logic:
    //    Return the value directly without any computation
    return $value;
}

/**
 * IMPORTANT
 * Dispatch to "wsProcessLevel1()" or "wsProcessLevel1()" based on the
 * value of KEY_MAPPING_LOGIC_COMPLEXITY of the field
 */
public function process($params) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    try {
        $method = $params[Constants::KEY_FIELD_METHOD];

        if (!array_key_exists($method, $this->filedMethodMappings)) {
            $msg = $msg_prefix
                   . "No info found for $method in filedMethodMappings.";
            $ret = WsResponse::getResponse_Error($msg);
            return $ret;
        }

        $mapping_info = $this->filedMethodMappings[$method];
        $complexity = $mapping_info[CaBase::KEY_MAPPING_LOGIC_COMPLEXITY];
        $logic_name = $mapping_info[CaBase::KEY_FIELD_LOGIC_NAME];


        switch ($complexity) {
            case CaBase::LEVEL1_COMPLEXITY:
                $ret = $this->wsProcessLevel1($params, $logic_name);
                break;
            case CaBase::LEVEL2_COMPLEXITY:
                $ret = $this->wsProcessLevel2($params, $logic_name);
                break;
            case CaBase::LEVEL3_COMPLEXITY:
                $question_id = $mapping_info[CaBase::KEY_FIELD_QUESTION_ID];
                $ret = $this->wsProcessLevel3($params, $logic_name, $question_id);
                break;
            default :
                $msg = $msg_prefix
                       . "Unsupported complexity level: " . $complexity;
                $ret = WsResponse::getResponse_Error($msg);
                break;
        }

    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred during processing.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;
}

 /**
 * IMPORTANT
 * For LEVEL3_COMPLEXITY, $logic_name should be the name of a method to be
 * executed for the field. The method with that name MUST be implemented in
 * the child class of this base class as a 'protected' or 'public' method.
 */
private function wsProcessLevel3($params, $logic_name, $question_id) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    $func_name = $logic_name;

    try {
        $ret = call_user_func_array(
                    array($this, $func_name),
                    array($params, $question_id)
                );

        if (false === $ret) {
            $msg = $msg_prefix
                   . "The call to $this->name.$func_name with the given "
                   . "parameters returned 'false'.";
            $ret = WsResponse::getResponse_Error($msg);
        } else {
            //Put my footage in the message
            $ret->message = $this->name . "::" . __FUNCTION__ . ": "
                            . $ret->message;
        }

    } catch (Exception $e) {
        $msg = $msg_prefix
               . "Exception occurred when calling $this->name.$func_name";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;

}


/**
 * IMPORTANT
 * For LEVEL2_COMPLEXITY, $logic_name should be the name of a method to be
 * executed for the field. The method with that name MUST be implemented in
 * the child class of this base class as a 'protected' or 'public' method.
 */
private function wsProcessLevel2($params, $logic_name) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    $func_name = $logic_name;

    try {
        $ret = call_user_func_array(
                    array($this, $func_name),
                    array($params)
                );

        if (false === $ret) {
            $msg = $msg_prefix
                   . "The call to $this->name.$func_name with the given "
                   . "parameters returned 'false'.";
            $ret = WsResponse::getResponse_Error($msg);
        } else {
            //Put my footage in the message
            $ret->message = $this->name . "::" . __FUNCTION__ . ": "
                            . $ret->message;
        }

    } catch (Exception $e) {
        $msg = $msg_prefix
               . "Exception occurred when calling $this->name.$func_name";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;

}

/**
 * IMPORTANT
 * For LEVEL1_COMPLEXITY, $logic_name should be the actual name of the field
 * on the table in ChartAccess database.
 * For fields marked as LEVEL1_COMPLEXITY, we can use the common standard
 * logic [ie. wsUpdateFieldLevel1(), wsReadFieldLevel1()] to update/read
 * their values. These common standard methods are provided below.
 */
private function wsProcessLevel1($params, $logic_name) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    try {
        $action  = $params[Constants::KEY_ACTION];
        switch ($action) {
            case Constants::ACTION_UPDATE:
                $ret = $this->wsUpdateFieldLevel1($params, $logic_name);
                break;
            case Constants::ACTION_READ:
                $ret = $this->wsReadFieldLevel1($params, $logic_name);
                break;
            default :
                $msg = $msg_prefix . "Unsupported action: " . $action;
                $ret = WsResponse::getResponse_Error($msg);
                break;
        }

    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred during processing.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;
}

/**
 * IMPORTANT
 * For LEVEL1_COMPLEXITY, $logic_name MUST be the actual name of the field
 * on the table in ChartAccess database.
 */
private function wsUpdateFieldLevel1($params, $logic_name) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    $ca_field = $logic_name;

    try {
        $hospital_id  = $params[Constants::KEY_HOSPITAL_ID];
        $patient_id   = $params[Constants::KEY_PATIENT_ID];
        $admission_id = $params[Constants::KEY_ADMISSION_ID];
        $data = $this->convertFieldValue(
                    CaBase::CONVERT_FROM_REQUEST_TO_ME,
                    $ca_field,
                    $params[Constants::KEY_FIELD_DATA]
                );
        $my_id = $this->getPrimaryKeyValue(
                    $hospital_id,
                    $patient_id,
                    $admission_id
                );

        $conditions = array(
            $ca_field         => $data,
            $this->primaryKey => $my_id,
        );
        $this->set($conditions);
        $result = $this->save();

        if (false == $result) {
            $status = Constants::STATUS_ERROR;
            $msg = $msg_prefix . "Failed to update";
        } else {
            $status = Constants::STATUS_SUCCESS;
            //$msg = $msg_prefix . "Updated aaa successfully";
        }

        $ret = new WsResponse($status, $msg, $result);

    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;
}

/**
 * IMPORTANT
 * For LEVEL1_COMPLEXITY, $logic_name MUST be the actual name of the field
 * on the table in ChartAccess database.
 */
private function wsReadFieldLevel1($params, $logic_name) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    $ca_field = $logic_name;

    try {
        $hospital_id  = $params[Constants::KEY_HOSPITAL_ID];
        $patient_id   = $params[Constants::KEY_PATIENT_ID];
        $admission_id = $params[Constants::KEY_ADMISSION_ID];
        $my_id        = $this->getPrimaryKeyValue(
                                $hospital_id,
                                $patient_id,
                                $admission_id
                            );

        $rs = $this->read($ca_field, $my_id);

        if (false == $rs) {
            $value = null;
            $status = Constants::STATUS_ERROR;
            $msg = $msg_prefix . "Failed to read value (returned false).";
        } else {
            $value = $this->convertFieldValue(
                            CaBase::CONVERT_FROM_ME_TO_RESPONSE,
                            $ca_field,
                            $rs[$this->name][$ca_field]
                             // $this->name should be the model name
                        );

            $status = Constants::STATUS_SUCCESS;
            $msg = $msg_prefix . "Value obtained.";
        }

    } catch (Exception $e) {
        $value = null;
        $status = Constants::STATUS_ERROR;
        $msg = $msg_prefix . "Failed to read value (exception occurred).";
    }

    $ret = new WsResponse($status, $msg, $value);
    return $ret;
}

}
?>

This class is the CaExtraFlowsheetFields, it extends CaBase

<?php
App::import('Model', 'CaBase');
//App::import('Model', 'CaAnswerEntry');

class CaExtraFlowsheetFields extends CaBase {
public $name = 'CaExtraFlowsheetFields';

/*
NOTE: This is to take all the fields in flowsheet and
maps their id's.
*/
//public $useTable = 'ANSWER_ENTRY';
public $useTable = 'PATIENT_FLOWSHEET_DATA';
public $primaryKey = 'PT_FS_DATA_ID';

protected function getPrimaryKeyValue(
    $hospital_id,
    $patient_id,
    $admission_id = null
) {

return $patient_id;
}

//*CHANGE BEGIN*
protected $filedMethodMappings = array(

    'Method_GO' => array(
        CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
        CaBase::KEY_FIELD_LOGIC_NAME         => 'wsMethod_GO',
        CaBase::KEY_FIELD_QUESTION_ID        => '',
        )
);

//########################################################################//
//Note[]>Block[]               //
//>Method that calls LookUpField for every field in flowsheet //                                       //
//########################################################################//
private function wsMethod_GO ($params) {

    $unifiedKeys = $this->query("select distinct FLOWSHEET_ID from FLOWSHEET_TEMPLATE;");
    foreach($unifiedKeys as $value){

        $flowsheet_name = $this->query("select FLOWSHEET_NAME from FLOWSHEET_TEMPLATE where FLOWSHEET_ID = ".$value.";");
        array_push($filedMethodMappings,
            $flowsheet_name => array(
                CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
                CaBase::KEY_FIELD_LOGIC_NAME         => 'wsUnifiedKey',
                CaBase::KEY_FIELD_QUESTION_ID        => ''.$value,
                );
            );
    }

    unset($value);
}
//########################################################################//

public function wsUnifiedKey($params, $flowsheet_id) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": "."field names hoooo: ".$field_name;

    try {
        //$flow = ClassRegistry::init('CaFlowsheetTemplate');
        //$flowsheet_id = $flow->getPrimaryKeyValue($field_name);


        $action  = $params[Constants::KEY_ACTION];
        switch ($action) {
            case Constants::ACTION_UPDATE:
                $ret = $this->wsUnifiedKeyUpdate($params, $flowsheet_id);
                break;
            case Constants::ACTION_READ:
                $ret = $this->wsUnifiedKeyRead($params, $flowsheet_id);
                break;
            default :
                $msg = $msg_prefix . "Unsupported action: " . $action;
                $ret = WsResponse::getResponse_Error($msg);
                break;
        }

    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred during processing.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;
}

private function wsUnifiedKeyUpdate($params, $flowsheet_id) {

    //$ret = $this->wsUpdateLastFlowsheetValueByUnitId($params, 100010, ' ');
    $flname = ClassRegistry::init('CaFlowsheetTemplate');
    $uom = $flname->FindUOMbyFlowsheetId($flowsheet_id);

    $ret = $this->wsUpdateLastFlowsheetValueByUnitId($params, $flowsheet_id, $uom);


    return $ret;
}

private function wsUnifiedKeyRead($params, $flowsheet_id) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    try {
        //$ret = $this->wsGetLastFlowsheetValueByUnitType($params, 100010);
         $ret = $this->wsGetLastFlowsheetValueByUnitType($params, $flowsheet_id);
        //Put my footage in the message
        $ret->message = $msg_prefix . $ret->message;
    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;
}

//--------------------------------------------------------------------------

private function wsGetLastFlowsheetValueByUnitType($params, $unit) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    try {
        $hospital_id  = $params[Constants::KEY_HOSPITAL_ID];
        $patient_id   = $params[Constants::KEY_PATIENT_ID];
        $admission_id = $params[Constants::KEY_ADMISSION_ID];

        $fs_data = ClassRegistry::init('CaPatientFlowsheetData');

        $ret = $fs_data->wsGetLatestFlowsheetValueByUnitId(
                    $unit, $hospital_id, $patient_id, $admission_id
        );

    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;

}

//--------------------------------------------------------------------------

private function wsUpdateLastFlowsheetValueByUnitId($params, $flowsheet_id, $units) {
    $msg_prefix = $this->name . "::" . __FUNCTION__ . ": ";

    try {
        $hospital_id  = $params[Constants::KEY_HOSPITAL_ID];
        $patient_id   = $params[Constants::KEY_PATIENT_ID];
        $admission_id = $params[Constants::KEY_ADMISSION_ID];
        $data         = $params[Constants::KEY_FIELD_DATA];

        $fs_data = ClassRegistry::init('CaPatientFlowsheetData');

        $ret = $fs_data->wsUpdateLatestFlowsheetValueByUnitId(
                    $flowsheet_id, $hospital_id, $patient_id, $admission_id, $data, $units
        );

    $mu = $this->query('P_MU_CORE_VITALSIGN @admission_id='.$admission_id.';');     

    } catch (Exception $e) {
        $msg = $msg_prefix . "Exception occurred.";
        $ret = WsResponse::getResponse_Error($msg);
    }

    return $ret;

}
}
?>

Any ideas as to why I'm getting this error? I can't see what I did wrong.

This code correctly updates KEY_FIELD_QUESTION_ID:

protected $filedMethodMappings = array(
    'Weight' => array(
                CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
                CaBase::KEY_FIELD_LOGIC_NAME         => 'wsUnifiedKey' ,
                CaBase::KEY_FIELD_QUESTION_ID        => '1', ),
    'Height' => array(
                CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
                CaBase::KEY_FIELD_LOGIC_NAME         => 'wsUnifiedKey' ,
                CaBase::KEY_FIELD_QUESTION_ID        => '2', ),
    'Temperature' => array(
                CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
                CaBase::KEY_FIELD_LOGIC_NAME         => 'wsUnifiedKey' ,
                CaBase::KEY_FIELD_QUESTION_ID        => '3', ),
    'Blood Pressure' => array(
                CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
                CaBase::KEY_FIELD_LOGIC_NAME         => 'wsUnifiedKey' ,
                CaBase::KEY_FIELD_QUESTION_ID        => '4', ),
share|improve this question

What's "CaBase" supposed to be? We can't see its definition, I suppose it's the name of your class? More info would help about what you are trying to do. – NaturalBornCamper Jun 18 at 19:21
The line CaBase::KEY_FIELD_QUESTION_ID => ''.$value; is really weird, did you add that up? It looks like you are trying to modify KEY_FIELD_QUESTION_ID, which is a constant and cannot be modified. I don't get the ''.$value either. If you modified that, what was the original line? – NaturalBornCamper Jun 18 at 19:45
1  
Ah, thought that was what you were trying to do but you don't need the '' for now. what you are doing in the last bit of code is not updating a constant, but crating an associative array, with the key being the value of the constant. like such: php.net/manual/en/language.types.array.php so what you are trying to do is not valid. What are you trying to do? You just want to modify the constant? – NaturalBornCamper Jun 18 at 20:06
I wouldn't recommend changing working code unless you absolutely know what to do. If you absolutely need to do it, maybe you can just change KEY_FIELD_QUESTION_ID from a constant to a class variable, although changing its value might make you code stop working, it's probably a constant for a good reason. – NaturalBornCamper Jun 18 at 20:22
Probably because you are trying to do the same thing but somewhere else, can you update the code in your question? – NaturalBornCamper Jun 18 at 20:29
show 3 more comments
feedback

2 Answers

up vote 1 down vote accepted

Ok, found a lot of errors in your wsMethod method, try to use this function in your CaExtraFlowsheetFields class, I hope it's the only problem as I have to go now:

private function wsMethod_GO ($params) {
    $unifiedKeys = $this->query("select distinct FLOWSHEET_ID from FLOWSHEET_TEMPLATE;");
    foreach($unifiedKeys as $value){
        $flowsheet_name = $this->query("select FLOWSHEET_NAME from FLOWSHEET_TEMPLATE where FLOWSHEET_ID = '".$value."';");

        array_push($this->filedMethodMappings,
            array(
                CaBase::KEY_MAPPING_LOGIC_COMPLEXITY => CaBase::LEVEL3_COMPLEXITY,
                CaBase::KEY_FIELD_LOGIC_NAME         => 'wsUnifiedKey',
                CaBase::KEY_FIELD_QUESTION_ID        => ''.$value
            )
        );
    }

    unset($value);
}
share|improve this answer
This worked!! Thank you so much!! – nathpilland Jun 18 at 21:09
feedback
CaBase::KEY_FIELD_QUESTION_ID => ''.$unifiedKey;

This line is invalid. It contains, like the error says, an unexpected T_DOUBLE_ARROW.

share|improve this answer
What makes it invalid/how can I make it valid? KEY_FIELD_QUESTION_ID is a constant from class CaBase – nathpilland Jun 18 at 19:02
Well, what exactly are you trying to do there? What makes you think a => is the right thing to go in that place, considering it is only used for array key assignments? – Kolink Jun 18 at 19:07
What I want to do is put the number stored in $unifiedKey, run a few methods from the CaBase class using KEY_FIELD_QUESTION_ID, then output the text to the terminal. I tried to change it to KEY_FIELD_QUESTION_ID = ''.$value, but that didn't work either. – nathpilland Jun 18 at 19:14
Well considering you have an unconditional return inside the foreach, I'm not entirely sure how you expect that to work. – Kolink Jun 18 at 19:16
1  
No, because the key field question id is an array key, not the element of the array itself. – Kolink Jun 18 at 20:16
show 5 more comments
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.