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

April 10, 2010 6:41pm

Subscribe [14]
  • #16 / Aug 02, 2010 8:35am

    liri

    50 posts

    Mark,

    In a simple model I’m just doing in the constructor after calling the parent constructor

    $this->load_table('user');

    And in the controller performing:

    $res = $this->users_model->get();

    Which results in an error:
      ErrorException [ Fatal Error ]: Error Number: 1096 / No tables used / SELECT *


    Why is this happening?


    On a side note, is it just me or is your MY_Model is considerably slower than normal due to it’s show_columns operations and other similar methods?


    Thanks,
    Liran.

  • #17 / Aug 02, 2010 9:31am

    Mark Croxton

    319 posts

    Hi liri,

    get() requires at least one argument (an array of options). It doesn’t by default do SELECT * on the current table. Would be very easy to add that to the function if you wanted it.

    show_columns will add a small overhead which is why I would recommend using a cache library such as MP Cache in the way I described above.

  • #18 / Aug 02, 2010 9:51am

    liri

    50 posts

    Hey Mark,

    Thanks for the prompt reply.

    What I do not require any where conditions or don’t want to specify all the fields to get one by one,
    assuming I just want them all. As would be with the default AR pattern in CI:
    $this->db->from(‘user’)->get();


    Also, could you provide links/resources for actual usage examples which are using your model
    in actual applications?

  • #19 / Aug 02, 2010 10:00am

    liri

    50 posts

    Ahh, I see now in the code the following:
      if ($options[‘fields’] == “*”)
    Kind of missed it before.


    Though I’m also seeing the use of default option values (which I think is smart to do):
            $options = $this->_default(
              array(
                  ‘fields’    => array($this->$table->pk),
                  ‘sort’      => ‘asc’,
                  ‘order_by’    => $this->$table->alias.’.’.$this->$table->pk,
                  ‘offset’  => 0,
                  ‘distinct’  => true,
                  ‘join’      => ‘left’
              ), $options);

    So I’m not sure why it requires it anyway.

  • #20 / Aug 02, 2010 11:03am

    Mark Croxton

    319 posts

    You’ll find this near the top of the function:

    if (count($args)>0)
    {

    ...
    }

    Add an else{} condition to handle the ‘no arguments’ case.

    Actual usage - so far I’ve used it for one project, a sizable extranet with 39 tables. The complexity of this project meant I was able to really test and refine the code. What I would say is that some of the usefulness of the model will only become apparent to you from actual use and from reading the code closely.

    I’d love to hear from anyone else using it in their projects.

  • #21 / Aug 02, 2010 11:06am

    liri

    50 posts

    You’ll find this near the top of the function:

    What I would say is that some of the usefulness of the model will only become apparent to you from actual use and from reading the code closely.

    Exactly why I asked for resources which actually make use of the library.
    Well I guess that project you used it for isn’t open sourced and there aren’t others?

    Would be good if we could even work out documentation, something similar to the CI kind would be awesome.


    Regards,
    Liran.

  • #22 / Aug 02, 2010 1:28pm

    Mark Croxton

    319 posts

    I agree that full documentation and a sample application would be helpful. I am in fact working on a new site for my company that will have a documentation area for our various libraries and add-ons. I have a fully featured authentication & authorisation system that would lend itself to acting as a demo.

    Finding the time is my biggest problem…

  • #23 / Aug 02, 2010 2:35pm

    liri

    50 posts

    Tell me about it 😊

  • #24 / Aug 03, 2010 3:30am

    victorche

    147 posts

    Really good set of functions! I am testing and playing for 2 days and I am impressed. I have couple of small questions though.
    Can you please give me and example of using get_column()
    I have a really small table with only 2 columns ‘id’ and ‘name’
    And I only need an array of the all names (categories names in this case). The table is not the primary one, used in this model, but anyway I described the relations about my 2 tables:
    $this->load_table(‘posts’);<br /> $this->load_table(‘categories’);<br />       <br /> // describe relationships<br /> // $this->table1->related(‘table2’, array(‘table1_key’, ‘table2_key’));<br /> $this->posts->related(‘categories’, array(‘cat_id’, ‘id’));
    All I need is an array of the category names, in order to display them in a sidebar menu like:
    </p><h3>Categories</h3> <p><ul><br />   {post_categories}<br />   <li><a href="/posts/cat/{name}/">{name}</a></li><br />   {/post_categories}<br /> </ul>

  • #25 / Aug 03, 2010 5:53am

    Mark Croxton

    319 posts

    $categories = $this->with('categories')->get_column('name');
  • #26 / Aug 03, 2010 6:17am

    liri

    50 posts

    What is the purpose of with()?

  • #27 / Aug 03, 2010 6:47am

    Mark Croxton

    319 posts

    with() determines the table the model ‘points’ to and acts upon.

    By default the primary table for a model is the first one loaded in the model constructor. But often you will want to start your query with another table. So in the previous example, with() was used because the primary table was ‘posts’.

    Note that once you have switched the primary table using with(), then any following logic uses that table as the primary too.

    Passing TRUE as the second argument to with() will initialize various parameters used in the current query (such as the array of tables already joined in the query).

  • #28 / Aug 03, 2010 4:02pm

    victorche

    147 posts

    $categories = $this->with('categories')->get_column('name');

    Thanks, but not exactly what i wanted :/
    Your query is giving me the following:

    Array ( [0] => news [1] => other )

    And I need the following:

    Array ( [0] => Array ( [name] => news ) [1] => Array ( [name] => other ) )

    And i am using this in my controller, so i had to add the model like:

    $categories = $this->blog->with('categories')->get_column('name');
  • #29 / Aug 04, 2010 5:19am

    liri

    50 posts

    @victorche
    Will you please post your complete table structure for both tables and an actual result-set output
    so we can see an example of the data?

  • #30 / Aug 04, 2010 10:21am

    Mark Croxton

    319 posts

    So just a normal resultset as an array:

    $categories = $this->blog->with('categories')->get(array('fields' => 'name'));

    As you’re using this in a controller, and get() will return FALSE if no results, I’d actually do this:

    if (!! $categories = $this->blog->with('categories')->get(array('fields' => 'name')))
    {
       // safely do stuff with $categories
    }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases