CodeIgniter Forums
DataMapper 1.6.0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper 1.6.0 (/showthread.php?tid=11358)



DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]OverZealous[/eluser]
I was trying to debug something, and discovered that DM is once again reloading the Form_validation library on every model.

This isn't huge a problem performance-wise, but it does tend to add a little more checking and loading than necessary, plus it fills the log files with "Form Validation Already loaded..." messages.

I would like to make a recommendation to only load the library the first time, as well as only calling the language and helper loading once.

My suggested (although probably not-very-elegant) solution is this:
Code:
// near top
static $common = array();
// add new variable to track whether anything has been loaded yet.
// Might be safe checking empty(DataMapper::$config) instead
static $_loaded_once = FALSE;
Code:
// Add this to the constructor
function DataMapper()
{
    $this->_assign_libraries();

    if( ! DataMapper::$_loaded_once)
    {
        $this->_load_languages();

        $this->_load_helpers();

        DataMapper::$_loaded_once = TRUE;
    }
    // ...
Code:
// modify _assign_libaries

function _assign_libraries()
{
    if ($CI =& get_instance())
    {
    if( ! DataMapper::$_loaded_once) {
        // Load CodeIgniters form validation
        $CI->load->library('form_validation');
    }

Those few changes will prevent the libaries, language, and helper files from being loaded or checked dozens or even hundreds of times for very big datasets. Mostly, though, it will make debugging easier on me ;-)

Also, a recommendation but not too important, does it make sense to migrate the date format code from being hard coded to being stored in the config file? This would allow users to change the format to whatever they wanted.


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]stensi[/eluser]
No real performance issue with this but yeah, it would help debugging.

UPDATE:

I just fixed it by having an isset check in the _assign_libraries() method, so it will only load the Form Validation library if not set. Saves having to add any other code elsewhere.

I'm currently compiling a list of what to include in the next version. The biggest additions will most likely be the inclusion of language internationalisation and the view_{field}_as_{format}() methods (probably).


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]hugle[/eluser]
Hello everyone.
Well, I should stay i'm stuck at the beginning ...

I load the controller, navigate to "Create Users"
and get this:

// Create User
$u = new User();
$u->username = "Fred Smith";
$u->email = "[email protected]Smith.com";
$u->password = "apples";
$u->confirm_password = "apples";
Fatal error: Class 'User' not found in ..httpdocs/system/application/controllers/examples.php on line 75

What I could be wrong? aren't those classes loaded automatically ?

Thanks and sorry if it is too lame question,
using version 1.5.4


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]stensi[/eluser]
@hugle: It looks like it's probably falling over because it can't find the user.php model file in your application/models folder, or it might be that DataMapper hasn't been added to the autoload config file.

Make sure you've followed all of the Usage Examples steps before trying out the example, and then you should be right to go.


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]hugle[/eluser]
Thanks stensi for such a quick and clear reply. My bad - missed the autoload part, aghrrrrrrrrr....
sorry for any inconvenience.
After looking at the examples, it seems really very sexy

Thanks !


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]stensi[/eluser]
lol, no problem :-) Happy to help!


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]hugle[/eluser]
[quote author="stensi" date="1228726715"]lol, no problem :-) Happy to help![/quote]

I was sleeping just for 2 hours, maybe that is the point Smile


DataMapper 1.6.0 - El Forum - 12-07-2008

[eluser]hugle[/eluser]
Hello everyone once again.
I thought maybe some of you tried/did some forums using DataMapper and could share it?

Actually I'm thinking of doing some simple forums

Thanks and good luck


DataMapper 1.6.0 - El Forum - 12-08-2008

[eluser]hugle[/eluser]
Hello stensi.

Well, your library seems very nice, continuing to use it.

Came to one question here, I have extended form_vallidation library, so I have:

Code:
function if_exists($str, $field)
    {
        echo 'test';
        $CI =& get_instance();
        
        $count = count($str); //we count how many values we got (in arr|string)
        list($table, $column) = split("\.", $field, 2);
        
        if(is_array($str))
        {
            $str = implode(",", $str); // impplode the string for use with MySQL. in case we use checkbox arrays (example: name="hobby[]"
            $array = TRUE;
        } else
        {
            $array = FALSE;
        }
        if ($array) {
            $query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column IN ($str)");
            $row = $query->row();
        } else {
            $query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = ?", array($str));
            $row = $query->row();
        }
                                
        return ($row->dupe == $count) ? TRUE : FALSE;    
    }


If I use this validator from CI (not DM) It works nice, I call it like:
Code:
required|if_exists[topics.id]
So the function check if the value entered to the field, exists in database, table 'topic' and column 'id'

Somehow this functions doesn't work if I call it from DM validator.
my validation config is as folows:
Code:
var $validation = array(
    array(
        'field' => 'topic_id',
        'label' => 'Topic ID',
        'rules' => array('required', 'if_exists[topics.id]')
    ),
    array(
        'field' => 'user_id',
        'label' => 'User ID',
        'rules' => array('required')
    ),
    array(
        'field' => 'message',
        'label' => 'Message',
        'rules' => array('trim', 'required', 'xss_clean')
    )
    );

Any suggestions would be nice, thanks CI community for CI, and stensi for this wonderful Datamapper tool! Smile


DataMapper 1.6.0 - El Forum - 12-08-2008

[eluser]OverZealous[/eluser]
Real quick, here, did you know that DataMapper already defines a "unique" rule? Check out the validation section of the DM manual.

UPDATE: Ignore what I just wrote. What's wrong is with DataMapper, you need to call the rules like this:
Code:
'rules' => array('required', 'if_exists' => 'topic.id');