Welcome Guest, Not a member yet? Register   Sign In
IgnitedRecord model functionality: hook, behavior or something else?
#4

[eluser]echadwickb[/eluser]
I've decided to ditch ignitedrecord. It looks like a great tool with awesome features, but it's too deep and confusing for me. Just the same, I've included the code I wrote to extend the model functionality in ignitedrecord. Maybe this is obvious to everyone but me, but hopefully it will help some other poor soul.

Code:
class IgnitedRecord_reorder {
    
    function IgnitedRecord_reorder(&$ORM, $opts)
    {
        // hooks
        $ORM->add_hook('save_pre_insert',array(&$this,'reorder_items'));
        $ORM->add_hook('save_pre_update',array(&$this,'reorder_items'));
        
        log_message('debug','IgnitedRecord: Behaviour class IgnitedRecord_reorder has been initialized');
        
        $this->ORM =& $ORM;
        
    }
    
    function reorder_items(&$data)
    {
        //Get new order
        if(is_array($data))
        {
            $order = $data['order'];
        }
        else
        {
            $order = $data->order;
        }
        
        // Never figured this part out.  All I wanted was the current uid
        // of the record I was dealing with, so I could determine the current
        // position of the record (if updating, of course. New record would have
        // defaulted to last position)
        $id = $this->ORM->__id;

        //Get links in order
        $sql = "SELECT * FROM `".$this->ORM->table."` ORDER BY `order`";
        $query = $this->ORM->db->query($sql);
        $rows = $query->result_array();
        
        //need to get the position of the current row being saved
        foreach($rows as $index => $row)
        {
            if($id == $row['id'])
            {
                $current_order = $index+1;
                $current_order_val = $row['order'];
            }
        }
        
        //Find link that will follow current link.  If no link use next whole number
        if(count($rows) > $order)
        {
            if($current_order < $order)
            {
                $following_row = $rows[$order];
                $new_order = $following_row['order'] - .000005;
            }
            elseif($current_order > $order)
            {
                $following_row = $rows[$order-1];
                $new_order = $following_row['order'] - .000005;
            }
            else
            {
                $new_order = $current_order_val;
            }            
        }
        else
        {
            $new_order = (count($rows) + 1)/ 10;
        }
        
        //Set order
        if(is_array($data))
        {
            $data['order'] = $new_order;
        }
        else
        {
            $data->order = $new_order;
        }
    }
}

After days and days and days and days of reading through this forum, I've found a few extensions of the model class that are much simpler, yet give me what I originally needed (basic CRUD functionality). In case anyone is curious, I'm currently trying out the following items:

phpfour's model extension: Extended Model for CodeIgniter
Daniel H's model extension: Model Extension


Messages In This Thread
IgnitedRecord model functionality: hook, behavior or something else? - by El Forum - 10-29-2008, 09:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB