Welcome Guest, Not a member yet? Register   Sign In
I really hope I don't spend Xmas on this problem... Can't find the bug here!
#1

[eluser]stormbytes[/eluser]
I've got a series of class methods calling one and other. I've been debugging with NB + Xdebug so I can see all the values in real time.

Code:
// The create_resource() function verifies if an 'asset' exists and returns false if it doesn't.
// It accepts the $asset_id argument. I've visually confirmed that a valid $asset_id is in
// fact being passed to this function by the caller.

public function create_resource($asset_id = NULL)
{
    if( ! $asset_id OR ! $this->assets_model->get_asset_by_id($asset_id))
    {
        return FALSE;
    }
}

// Here's the get_asset_by_id() function from assets_model. If a record exists in the
// user_assets table with the specified asset_id, the function returns the row.

public function get_asset_by_id($asset_id = NULL)
{
    if( ! $asset_id)
    {
        return FALSE;
    }
    
    $args['where']    = array('a.id' => $asset_id);
    $args['limit']         = 1;
    
    $rows = $this->_read($args);
    
    if($rows->num_rows() == 1)
    {
        foreach($rows->result() as $row)
        {
            return $row;
        }
    }
}

// Finally, here's the root function that actually makes the database call using ActiveRecord:

private function _read($args = array())
{
    if(array_key_exists('where', $args))
    {
        $this->db->where($args['where']);
    }

    if(array_key_exists('order_by', $args))
    {
        $this->db->order_by($args['order_by']);
    }
    else
    {
        $this->db->order_by('date_created DESC');    
    }
    
    if(array_key_exists('limit', $args))
    {
        $limit = $args['limit'];
    }
    else
    {
        $limit = NULL;
    }
    
    if(array_key_exists('offset', $args))
    {
        $offset = $args['offset'];
    }
    else
    {
        $offset = NULL;
    }

    $query =     "a.id,
                a.user_id,
                a.title,
                a.is_public,
                a.is_enabled,
                c.category,
                u.user_handle";
    
    $this->db->select($query);
    
    $this->db->join('meta_categories c', 'a.category_id = c.id', 'inner');
    $this->db->join('users u', 'a.user_id = u.id', 'inner');
        
    if(($rows = $this->db->get('user_assets a', $limit, $offset))) // CIUG has order of 2nd, 3rd args backwards!
    {
        return $rows;
    }
}

The problem:

For some reason which is entirely beyond me, get_asset_by_id() returns FALSE when a record with the specified asset_id exists in the user_assets table. I traced the 'asset_id' all throughout the call stack. It's there. I know the application is reading the user_assets table because I'm pulling the asset_id value from the user_assets table in a previous function (method) programmatically.

Is there an obvious syntax error I'm overlooking? Entirely possible since I've been starring at this problem for going-on-day-3 now.


EDIT:

I've isolated the bug but still have no idea why it's borking my query!

I've re-written the get_asset_by_id() function to bipass the 'master-fetch' method _read($args = aray()) as follows:

Code:
public function get_asset_by_id($asset_id = NULL)
{
    if( ! $asset_id)
    {
        return FALSE;
    }
//
//    $args['where']    = array('a.id' => $asset_id);
//    $args['limit']    = 1;
//
//    $rows = $this->_read($args);
//
    $rows = $this->db->get_where('user_assets', array('id' => $asset_id));
    
    if($rows->num_rows() == 1)
    {
        foreach($rows->result() as $row)
        {
            return $row;
        }
    }
}

The function now behaves as expected and returns the requested db row. I don't understand why it didn't work the first way I had it. There were no db errors and the query looks fine to me.

Despite this 'fixing' the problem, I much prefer to have a single 'fetch' method so that if anything needs to be changed all's I have to do is update a single function. As written, the get_asset_by_id() function is a repeat of _read(), and I'd still have to add all my joins.

Help?
#2

[eluser]Bart Mebane[/eluser]
If you add the two joins to your "bypass" method does it still work?




Theme © iAndrew 2016 - Forum software by © MyBB