Welcome Guest, Not a member yet? Register   Sign In
Meet CodeIgniter MY_Model
#1

(This post was last modified: 02-20-2015, 02:49 AM by Avenirer.)

Starting with some tutorials about creating a MY_Model, I ended up with a... MY_Model.

My MY_Model can be seen and downloaded at the following repository: https://github.com/avenirer/CodeIgniter-MY_Model

I sure hope you give it a try and report me any issues you encounter. if you feel smart, you can also do some pull request. I’ll be happy to merge them if they look good.

The instructions about using the MY_Model can be found on Github. Also, you can look and the tutorials I've mentioned earlier: Revisiting MY_Model in CodeIgniter: Copying from Jamie Rumbelow and looking at Eloquent and Establishing relationships between tables inside the MY_Model in CodeIgniter (1): has_one and has_many relations.Be careful though, as the final code can be different than what you’re seeing on the Github repository.
Reply
#2

Hi Avenirer, thanks for sharing this to the community. It looks very interesting to me, good work!

Reply
#3

Very cool idea, I'll definitely be poking through this over the weekend!
Reply
#4

Nice I will take a look over it this weekend. Thank you for sharing your work.
Reply
#5

Thank you, everyone.
Reply
#6

Added support for pivot tables to the MY_Model:

Hello, I've just added support for pivot tables to accomodate many to many relationships.
Reply
#7

(This post was last modified: 02-25-2015, 05:59 AM by spjonez.)

Great concept, I'm going to do something similar for my Cinder project but without any relationship code since I don't use an ORM. One method I plan to add that you're missing is a helper for inserting post data. In my app, as I imagine in most, you have a lot of methods that take post data and insert/update it in a database. Since you can't flat out insert/update everything that was submitted you need a way to selectively pull what fields from post data you want to go in, and a way to provide defaults for any missing fields.

In my project I have helpers called extract_array and merge_array that will be used to accomplish this:
Code:
function extract_array( $arr, $keys ) {
    if ( !is_array( $arr ) ) $arr = array( );
    $result = array( );

    foreach ( $arr as $key => $val ) {
        if ( in_array( $key, $keys ) ) {
            $result[ $key ] = $val;
        }
    }

    return $result;
}

function merge_array( ) {
    $args = func_get_args( );
    $result = $args[ 0 ];

    foreach ( $args as $arg ) {
        foreach ( $arg as $k => $v ) {
            if ( is_array( $v ) === true && isset( $result[ $k ] ) === true && is_array( $result[ $k ] ) === true ) {
                $result[ $k ] = merge_array( $result[ $k ], $v );
            }
            else {
                $result[ $k ] = $v;
            }
        }
    }

    return $result;
}

My idea is to build a method you can pass an array of keys to pull from post data, and optionally another param would provide an array of defaults you'd merge with the extracted fields to create a complete blob.

I haven't coded this yet, when I do I'll send you a link to the file in my Git repo. Thought I'd mention in now in case it's something you'd like to build into your package as well.

Cheers!
Reply
#8

Interesting, but when you do that, you should also create some sort of protected fields property, so that, if there are fields that you don't want inserted by a form (like 'id') you can simply not add them to the array.

Relationships (at least the way I understand it) is not really related to ORM. I don't want MY_Model to evolve into that. Smile That is way more complex than my PHP knowledge.
Reply
#9

(This post was last modified: 02-25-2015, 07:10 PM by spjonez.)

With my array helpers you'd write something like this for an insert:
Code:
$post = $this->input->post( );
$post = extract_array( $post, array( 'field_one', 'field_two' ) );

$defaults = array(
    'field_one' => null,
    'field_two' => null,
);

$data = merge_array( $defaults, $post );

$this->db->insert( 'table', $data );

And an update:
Code:
$data = $this->input->post( );
$data = extract_array( $data, array( $this->primary_key, 'field_one', 'field_two' ) );

$id = $data[ $this->primary_key ];
unset( $data[ $this->primary_key ] );

$this->db->update( 'table', $data, array( $this->primary_key => $id ) );

Perhaps I misunderstood your intent with relationships. Are you using it as a way to join to other tables? I don't use CI's active record class for anything other than basic insert/updates. Everything else I write my own SQL statements with $this->query. CI's database classes are great for escaping and managing transactions but I find the where/join/etc methods too verbose.
Reply
#10

Regarding the $_POST: the problem with doing this inside the model would be that you may need different validation rules depending on what you want to do (an insert or an update). The solution might be to enter a hidden element in the form in which you tell the model what you want to do - insert, update -, and this way it can choose what validation rules should apply to the data.

Regarding relationships: yes, that is what they are. relations between tables. This way you can do the queries faster (if we talk about simple queries like joins, not complex ones, the reason being exactly what you said, too verbose).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB