ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

MY_Model Base CRUD Model

November 08, 2009 9:02am

Subscribe [19]
  • #16 / Jan 04, 2010 10:37pm

    megabyte

    179 posts

    Jamie,

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

    I answered my question by rereading your first post!

  • #17 / Jan 17, 2010 4:16am

    megabyte

    179 posts

    I’m getting this error using MY_Model

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

    which is

    parent::__construct();


    Here’s my 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 / Jan 17, 2010 4:57am

    Jamie Rumbelow

    546 posts

    Hey megabyte,

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

    Jamie

  • #19 / Jan 17, 2010 5:19am

    megabyte

    179 posts

    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 / Jan 17, 2010 5:27am

    Jamie Rumbelow

    546 posts

    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.

  • #21 / Jan 17, 2010 2:37pm

    megabyte

    179 posts

    I think there is another issue now.

    But of course it could be related to my code, lol.

    So no MY_Model error anymore, but in my view when I’m using the table class which I have autoloaded and works in other views. I get this error

    Fatal error: Call to a member function set_template() on a non-object

    Could the changes you made to MY_Model have affected this?

  • #22 / Jan 17, 2010 2:43pm

    Jamie Rumbelow

    546 posts

    Nope, definitely your code!

  • #23 / Jan 17, 2010 2:46pm

    megabyte

    179 posts

    I was so hoping you weren’t going to say that. lol.


    Don’t get me wrong, I think you’re a genius, and wish I knew at 15 what you do, although PHP wasn’t around to my knowledge in 1988.

  • #24 / Jan 17, 2010 2:48pm

    Jamie Rumbelow

    546 posts

    Hehe, thanks 😊 and sorry I can’t help - it must be an issue with using the wrong variable - check that you’re accessing the right var in your view.

  • #25 / Jan 17, 2010 2:52pm

    megabyte

    179 posts

    I’m trying to debug. I use the same in every view.

    $tmpl = array ( 'table_open' => '<table cellspacing="0" class="tablesorter">', 
                    'row_alt_start' => '<tr class="odd">'
                  );
    
    $this->table->set_template($tmpl);
  • #26 / Jan 17, 2010 2:59pm

    megabyte

    179 posts

    Here’s another question for you though.


    In the user manual they say best practice is this:

    if ($query->num_rows() > 0)
    {
        foreach($query->result() as $row)
        {    
            
        }
    }


    But if I use your model, then what I get back from this function is a result:

    public function get_all() {
            return $this->db->get($this->table)
                ->result();
        }

    And I can’t use $query->num->rows

  • #27 / Jan 17, 2010 3:12pm

    megabyte

    179 posts

    OK, I found the issue, and yes its to do with you. 😛

    Hopefully you have a sense of humor (which I believe you do), as I am in no way being negative.

    It’s actually something I ran into along time ago.


    In your MY_Model, you are using

    $this->table

    Which is allowed to be changed in the model I extend from MY_Model. So when I do so, it kills $this->table that is created from the table class.

    I’d probably suggest changing $this->table to $this->tbl or $this->_table

  • #28 / Jan 17, 2010 5:31pm

    megabyte

    179 posts

    Maybe an obvious question, but when extending MY_Model you can’t keep the same naming structure can you?

    For example if I extend MY_Model and I create a function called insert() it will overwrite the MY_Model insert.

    Correct?

    If this is the case, is there a way to fix this?

  • #29 / Jan 17, 2010 6:15pm

    Phil Sturgeon

    2889 posts

    Maybe an obvious question, but when extending MY_Model you can’t keep the same naming structure can you?

    For example if I extend MY_Model and I create a function called insert() it will overwrite the MY_Model insert.

    Correct?

    If this is the case, is there a way to fix this?

    There is no need to “fix” this as it is not broken. That is normal PHP behaviour. Simply be careful when working on your models to not override them unless you want to…

    I like the ability to overload the function names as I can do stuff like this:

    function insert($ticket)
        {
            $this->load->helper('date');
            
            $ticket['created_on'] = now();
            
            return parent::insert($ticket);
        }

    Very handy indeed. 😊

  • #30 / Jan 17, 2010 6:26pm

    megabyte

    179 posts

    See this is the magical line I was looking for:

    return parent::insert($ticket);


    thanks.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases