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

[eluser]echadwickb[/eluser]
I'm learning ignited record with a small faq app. The app needs to let the user order the each faq element. In the table I've got an order column. When a user changes or adds an faq, I'd like the model to adjust the order of all the faqs in the table to accomodate re-ordering of the element.

So I'd run an sql statement like this right before the save:

UPDATE faqs SET order = order + 1 WHERE order > $this->order

So should I create a behaviour, hook or do something else? Add_hook looks promising, but the documentation implies the hook function needs to be in the controller. Doesn't seem right to put this functionality in the controller. It really belongs in the model. I'm still researching behaviour.

Thanks to anyone who can give me some direction.
#2

[eluser]Randy Casburn[/eluser]
You're thinking is pretty clear on this except a hook isn't really the right direction.

This is classic MVC behavior:

1) The user interacts with a page and provides some input to a controller (say 'save')
2) your controller instructs an appropriate Model to perform your update query
3) your controller then loads up a new views, perhaps showing the updated FAQ in the correct order

That should do it.

Randy
#3

[eluser]echadwickb[/eluser]
Thanks for your quick reply Randy. I was going to reply sooner, but I'm having trouble getting some functionality working. I hate to post ugly, non-working code.

I decided to create a behaviour and put a hook inside of it. I loosely followed the timestamp behaviour included with the ignitedRecord source code. The functionality I was looking for works and seems to follow the intentions of ignitedRecord's developer. Once I get the code in working order, I will post back to this thread so others can see how I did it (or laugh uncontrollably at my inferior coding skills . . . whatever).
#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




Theme © iAndrew 2016 - Forum software by © MyBB