Welcome Guest, Not a member yet? Register   Sign In
Meet CodeIgniter MY_Model
#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


Messages In This Thread
Meet CodeIgniter MY_Model - by Avenirer - 02-20-2015, 02:47 AM
RE: Meet CodeIgniter MY_Model - by Rufnex - 02-20-2015, 03:24 AM
RE: Meet CodeIgniter MY_Model - by spjonez - 02-20-2015, 05:55 AM
RE: Meet CodeIgniter MY_Model - by dmyers - 02-20-2015, 08:58 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-25-2015, 02:50 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-25-2015, 02:52 AM
RE: Meet CodeIgniter MY_Model - by spjonez - 02-25-2015, 05:57 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-25-2015, 06:28 AM
RE: Meet CodeIgniter MY_Model - by spjonez - 02-25-2015, 07:07 PM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-26-2015, 01:54 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-26-2015, 05:46 AM
RE: Meet CodeIgniter MY_Model - by spjonez - 02-26-2015, 06:55 PM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-26-2015, 11:51 PM
RE: Meet CodeIgniter MY_Model - by Avenirer - 02-27-2015, 12:35 AM
RE: Meet CodeIgniter MY_Model - by spjonez - 03-03-2015, 04:36 PM
RE: Meet CodeIgniter MY_Model - by Avenirer - 03-04-2015, 06:57 AM
RE: Meet CodeIgniter MY_Model - by mwhitney - 03-06-2015, 11:27 AM
RE: Meet CodeIgniter MY_Model - by spjonez - 03-09-2015, 05:41 AM
RE: Meet CodeIgniter MY_Model - by ivantcholakov - 03-09-2015, 12:52 PM
RE: Meet CodeIgniter MY_Model - by spjonez - 03-11-2015, 06:27 AM
RE: Meet CodeIgniter MY_Model - by san1020 - 03-12-2015, 05:14 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 03-16-2015, 03:14 AM
RE: Meet CodeIgniter MY_Model - by ircdirk - 03-25-2015, 06:52 AM
RE: Meet CodeIgniter MY_Model - by dgvirtual - 09-25-2016, 01:06 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 09-27-2016, 11:26 PM
RE: Meet CodeIgniter MY_Model - by dgvirtual - 09-28-2016, 12:00 AM
RE: Meet CodeIgniter MY_Model - by Avenirer - 09-28-2016, 12:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB