• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MY_Model Base CRUD Model

#11
[eluser]happydude[/eluser]
I want to make a suggestion for MY_Model.
Please, add $limit and $offset to the functions that get many rows (get_many_by and get_all) as most times I have to write custom models to limit the amount of data and from where it gets fetched. I also need offset for pagination purposes.

Thanks in advance.

#12
[eluser]Jamie Rumbelow[/eluser]
Update 1.1.1
A limit() method. Call it before any methods to add a LIMIT clause to your query. Takes two parameters - the limit integer and an optional offset.

#13
[eluser]happydude[/eluser]
errrm, sorry. Thank you so much for your fast response and fast action.

This one just popped into my head one hour after I made my last post and I am just checking this thread now. An order_by method will be handy too. It should handle strings or multiple array containing fields with which the result set will be ordered and the order_by value (asc, desc, random).

e.g. $this->product->order_by('sort = asc, date = desc')->get($id); //string
and $this->product->order_by(array('sort' => 'asc', 'date' => 'desc')->get($id); //array

Following your limit() method in the latest release, I've got something working on my end. Just thought I should let you know.

It'll be my last request though for this library though. Wink

Thanks for the simple but great libraries. They are lifesavers. Smile

#14
[eluser]megabyte[/eluser]
Quote:Tutorial
I’ve written a tutorial that takes a reasonable look at the functionality provided by the entire class, as well as my MY_Controller Controller System on my delightful blog. Check it out and feel free to leave a comment.

Your website has changed. Can you give an update on where your tutorial is for your MY_Model please.

I'm trying to learn more about it. Specifically:

Code:
/**
     * Runs the before create actions.
     *
     * @param array $data The array of actions
     * @return void
     * @author Jamie Rumbelow
     */
    private function _run_before_create($data) {
        foreach ($this->before_create as $method) {
            $data = call_user_func_array(array($this, $method), array($data));
        }
        
        return $data;
    }
    
    /**
     * Runs the after create actions.
     *
     * @param array $data The array of actions
     * @return void
     * @author Jamie Rumbelow
     */
    private function _run_after_create($data, $id) {
        foreach ($this->after_create as $method) {
            call_user_func_array(array($this, $method), array($data, $id));
        }
    }


Thanks.

#15
[eluser]Jamie Rumbelow[/eluser]
Sorry about that, the first post is updated.

Jamie

#16
[eluser]megabyte[/eluser]
Jamie,

In your future versions, will you be allowing for joins?

I answered my question by rereading your first post!

#17
[eluser]megabyte[/eluser]
I'm getting this error using MY_Model

Quote:Call to undefined method MY_Model::MY_Model() on line 18

which is

parent::__construct();


Here's my code

Code:
class Countries_model extends MY_Model
{

    function __construct()
    {
        parent::__construct();
        $this->table = 'countries';
    }

    
    /**
     * Get Countries
     *
     * @param    int
     * @return    array
     */
    function get_all_countries($limit, $offset)
    {
        $this->db->select("*, DATE_FORMAT(created, '%b. %d,%Y') as created", FALSE);
        $this->limit($limit, $offset);
        return $this->get_all();
    }
    
    
    /**
     * Count Countries
     *
     * @param    int
     * @return    array
     */
    function count_all_countries()
    {
        return $this->count_all();
    }
    
}

#18
[eluser]Jamie Rumbelow[/eluser]
Hey megabyte,

Can you show me the controller code you're using to load it?

Jamie

#19
[eluser]megabyte[/eluser]
Code:
class Countries extends Base {
    
    var $TABLE;
    var $CLASS;
    
    
    function Countries()
    {
        parent::Base();
        $this->_init();
    }
    
    /**
     * Add Ons
     * place to add extra code
     * @access    private
     * @return    string/void
     */        
    function _init()
    {        
        $this->CLASS = 'settings/countries';
        $this->VIEW = 'settings/countries';
        
        $this->load->model('countries_model');
    
        $data = array('error' => '', 'success' => '', 'active' => 'accounts');
        $data['id'] = $this->uri->segment('id');    
        $data['CLASS'] = $this->CLASS;
        $this->load->vars($data);
    }

    /**
     * View All Users
     *
     * @access    public
     * @return    void
     */        
    function index()
    {        
        // set the referral url
        $this->session->set_userdata('success_url', $this->uri->uri_string());
        
        $pager_range = array(50, 100, 200);
        $page          = $this->uri->segment('page', 1) -1;
        $limit          = $this->uri->segment('limit', $pager_range[0]);    
        $offset      = $page * $limit;
    
        $this->load->library('pagination');
        // set the base_url, this includes al segments up to the current page uri and the per page uri
        $config['base_url'] = $this->CLASS.'index/';
        $config['total_rows'] = $this->countries_model->count_all_countries();
        $config['page'] = 'page';
        $config['limit'] = 'limit';
        $config['pager_range'] = $pager_range;
        $config['pager_value'] = $this->input->post('pager_value');
        $this->pagination->initialize($config);        
        $data['pager'] = $this->pagination->pager_links();
        $data['query']    = $this->countries_model->get_all_countries($limit, $offset);
        
        if($this->input->post('pager_value'))
        {
            redirect($this->pagination->pager_redirect_to(), 'location');
        }
        
        $this->load->view($this->VIEW.'index', $data);
    }            
}

#20
[eluser]Jamie Rumbelow[/eluser]
Hmm, I think I know what's going on here. Hang on. I'll edit when a fix has been made.

EDIT Done. It's on the GitHub repo, check it out straight from master.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.