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]
  • #46 / Apr 04, 2010 3:06pm

    hawkfx

    1 posts

    Hello.

    I included this function on your model, it works for MYSQL version 5.x


    It fetch the primary key, without have to change it, I call it on __construct

    private function _fetch_pkey()
        {
            if ($this->table !== NULL)
            {
                $_table_data     = $this->db->query('SHOW KEYS FROM `'. $this->table .'`')->row(); 
                $this->primary_key    = $_table_data->Column_name;
            }
        }
  • #47 / Apr 04, 2010 3:14pm

    ciKD

    17 posts

    Looks interesting!

    With a similar approach, could you write a version which fetches the column-names of the table to an array so a function to (auto)validate a given order_by field could be added, using an in_array() check?

  • #48 / May 10, 2010 12:22pm

    ed_ball

    8 posts

    All links in original post now seem to be broken - what’s the deal?

  • #49 / May 10, 2010 3:47pm

    michalsn

    77 posts

  • #50 / May 11, 2010 1:40pm

    ed_ball

    8 posts

    Thanks. I’ve installed this now but having problems with the table property in MY_Model conflicting with the native CI table method. The workaround I am employing currently is to ‘reset’ $this->table to ‘new CI_table’ before setting table heading etc as otherwise the following error occurs “Call to a member function set_heading() on a non-object…”. Could this be considered a bug in the code or am I suffering from a lack of understanding of OOP?

  • #51 / May 11, 2010 1:55pm

    Jamie Rumbelow

    546 posts

    It was a bug… but a while ago. Take a look at the source, it should be protected $_table.

    Jamie

  • #52 / May 27, 2010 10:04pm

    Mafagafo

    2 posts

    I’m trying to use the version 1.1.1, but i’m having some dificulties:

    - how I can retrieve just 2 columns using MY_Model?
    For example, a table with 15 fields but I want to retrieve only 2 fields: name and date. How i can retrieve just these 2 fields? How i can format the date field with the format “%Y-%m-%d %H:%i:%s”?

    -Can I use more than 1 argument to create a WHERE condition when I use methods like “get_by()” or “get_many_by()”?

    This is a great tool. Congratulations. And sorry about my english.

  • #53 / May 28, 2010 5:12am

    Jamie Rumbelow

    546 posts

    Hi Mafagafo,

    You’ll need to create your own methods to do complex things like this. You can filter out this stuff by calling ActiveRecord methods on CodeIgniter’s database class:

    $this->db->select('username, email');
    $this->users->get_by('id', 1);

    Jamie

  • #54 / Jun 03, 2010 4:15am

    Buso

    297 posts

    Hi, I need some advice on how to organize my models when using base models like the one you created.

    Suppose I want to have a reusable cms/blog (something that allows me to create articles, comments, categories, links, tags, etc etc)

    Would you create one model for each? Example: Article_m, Comment_m, Category_m, so you can have the table name auto-fetched most of the times?

    Or one big model that handles everything?

    If each Article has many Comments.. Which model should handle the articles_comments table? (supposing I want to have this sepparate table. Or just imagine any many-to-many situation)

    Thanks in advance!

  • #55 / Jun 03, 2010 5:36am

    Jamie Rumbelow

    546 posts

    This is more of a generic MVC question, and always the former! Keep things as separated as possible. When dealing with relationships, try to have a “parent” and “child” (in your case, parent is Article), and have the parent handle the relationships.

    Hope this helps,

    Jamie

  • #56 / Jun 03, 2010 4:29pm

    Buso

    297 posts

    It’s just that I never see things like subdirectories inside /models. All the libraries I have seen, they always come with 1 big library, and 1 big model.

    Well thanks for your answer, I guess I have to try this method (separate everything) and see what happens.

  • #57 / Aug 02, 2010 7:25am

    liri

    50 posts

    It seems that there’s also another good MY_Model option developed by
    which is available to reiview at: http://github.com/croxton/MY_Model

    Just wondering why not merge the good of both worlds? Croxton’s got good support
    for more complex operations like joins.

    Regards,
    Liran.

  • #58 / Aug 11, 2010 12:52am

    MT206

    15 posts

    Anyone managed to make this support multiple databases? I need this solution for my current project, but had no luck yet. Unfortunately the author Jamie does not have time for this suggested feature :(

    I’m not sure if you are still looking for an answer but you might want to take a look at this link. He has implemented a My_Model where you can use multiple databases. The link is:

    http://www.lukemacneil.com/code/my-codeigniter-crud-model

  • #59 / May 03, 2012 6:58am

    Keylocker

    8 posts

    I am having HUGE problems with memory when I try to use the functions get_many, get_many_by and get_all

    here’s my controller code

    // List all addresses of a given user
     public function index( $user_id = NULL )
     {
      $this->load->model('addresses_model', 'addresses');
      $addr = $this->addresses->get_many_by( array('user_id' => $user_id) );
    
      print_r($addr); //debug
     }

    in the model

    class Addresses_model extends MY_Model {
     
    public function __construct()
     {
      // Load base construct
      parent::__construct();
     }
    }

    The error i get is

    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40 bytes) in 
    /application/models/addresses_model.php on line 20

    For the record. I tried with $this->db->get() and everything works perfectly. The problem appears just using MY_Model.php
    What can I do? Please help.

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

ExpressionEngine News!

#eecms, #events, #releases