Question

I'm trying to build some CodeIgniter models but am running into an issue that I can't figure out. I think it may have to do with variable scope, but I'm not sure.

I have a method in my controller that takes a $token value as an argument:

public function review($token)
{
        $order = Order_model::get_by_token($token, BC_EXT_TYPE_ID, 'Order_product');

        print_r($order);
}

As you can see, I'm calling a static method called get_by_token(). Here's the code for that function:

public static function get_by_token($unique_token, $ext_type_id = NULL, $join_class = NULL, $details = TRUE){

                   $CI =& get_instance();

        $where = array(
                "unique_token" => $unique_token,
                "deleted" => 0,
                "ext_type_id" => $ext_type_id
        );

        $CI->db->where($where);
        $query = $CI->db->get(static::$db_table);
        $model = $query->row(0, get_called_class());

        self::get($model->id, null, null, 'Order_product');
}

This function just gets the ID of the review from the database based on the token, and then calls another static method to join additional data to the review. Here's the get() function:

public static function get($id = NULL, $ext_id = NULL, $ext_type_id = NULL, $join_class = NULL, $details = TRUE){

                $CI =& get_instance();

                $where = array(
                        "deleted" => 0
                );

                if(!is_null($id)){
                        $where['id'] = $id;
                }
                elseif(!is_null($ext_type_id) && !is_null($ext_id)){
                        $where['ext_id'] = $ext_id;
                        $where['ext_type_id'] = $ext_type_id;
                }

                $CI->db->where($where);

                $query = $CI->db->get(static::$db_table);
                $model = $query->row(0, get_called_class());

                if(!is_null($join_class)){
                        $joins = $join_class::get($model->id, null, null, $details);
                        $join_property = explode('_', $join_class);
                        $join_property = $join_property[1].'s';
                        $model->$join_property = array();
                        foreach($joins as $join){
                                $model->{$join_property}[] = $join;
                        }

                }

                return $model;

}

As you can see, if a $join_class is provided, another static method is called for the $join_class in order to get the data to be joined and add it to the object. The get() method for the join class is here:

public static function get($primary_id, $secondary_id = NULL, $external_type_id = NULL, $details = TRUE){

        $CI =& get_instance();

        $where = array(
                static::$primary_column => $primary_id,
                'deleted'=>0
        );

        if(!is_null($secondary_id)){
                $where[static::$secondary_column] = $secondary_id;
        }

        if(!is_null($external_type_id)){
                $where['ext_type_id'] = $external_type_id;
        }

        $CI->db->where($where);
        $query = $CI->db->get(static::$db_table);

        $result = array();

        foreach($query->result(get_called_class()) as $row){

                if($details){
                        $secondary_model = static::$secondary_model;
                        $secondary_column = static::$secondary_column;
                        $object = $secondary_model::get($row->$secondary_column);
                        $row->details = $object;
                }

                $result[] = $row;
        }

        return $result;

}

The issue I am having is that if I print the value of $model just before the first get() function returns, it correctly shows me all of the data I expect. However, when I print $order variable from the review method, I get nothing back - an empty response.

I'm not sure why the value is changing, but I think it is probably something I am overlooking with variable scope. Does anyone have any idea?

Was it helpful?

Solution

In your get_by_token function, use this :

return self::get($model->id, null, null, 'Order_product');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top