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.

DataMapper 1.6.0

September 05, 2008 12:32pm

Subscribe [115]
  • #331 / Dec 07, 2008 8:48pm

    OverZealous

    1030 posts

    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:

    // 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;
    // Add this to the constructor
    function DataMapper()
    {
        $this->_assign_libraries();
    
        if( ! DataMapper::$_loaded_once)
        {
            $this->_load_languages();
    
            $this->_load_helpers();
    
            DataMapper::$_loaded_once = TRUE;
        }
        // ...
    // 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.

  • #332 / Dec 07, 2008 9:11pm

    stensi

    109 posts

    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).

  • #333 / Dec 07, 2008 10:34pm

    hugle

    289 posts

    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]”;
    $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

  • #334 / Dec 07, 2008 10:42pm

    stensi

    109 posts

    @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.

  • #335 / Dec 07, 2008 10:51pm

    hugle

    289 posts

    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 !

  • #336 / Dec 07, 2008 10:58pm

    stensi

    109 posts

    lol, no problem 😊  Happy to help!

  • #337 / Dec 07, 2008 11:04pm

    hugle

    289 posts

    lol, no problem 😊  Happy to help!

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

  • #338 / Dec 08, 2008 1:41am

    hugle

    289 posts

    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

  • #339 / Dec 08, 2008 11:22am

    hugle

    289 posts

    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:

    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:

    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:

    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! 😊

  • #340 / Dec 08, 2008 2:38pm

    OverZealous

    1030 posts

    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:

    'rules' => array('required', 'if_exists' => 'topic.id');
  • #341 / Dec 08, 2008 8:57pm

    hugle

    289 posts

    Thank you OverZealous.com very much!
    It now works as used to work always 😊

    Good luck using DataMapper!:)

  • #342 / Dec 08, 2008 9:29pm

    hugle

    289 posts

    Yesterday I saw, that neither header (‘Location: /’); nor redirect () do not work.

    Today I have reproduced it, and spotted, that if I put datamaper, in autoload.php like:

    $autoload['libraries'] = array('datamapper');

    and make simple Test controller:

    class Test extends Controller {
    
        function Test()
        {
            parent::Controller();
        }
    
        function index()
        {        
    
        }
    }

    When I navigate to the page, and make View Source, the content is ” ” (one whitespace)

    When I remove datamapper from autoload, refresh the page, it loads without a ” “.

    Anyone spotted similar issues ? or even knwo a workaround ?

    Thanks !

  • #343 / Dec 08, 2008 9:36pm

    OverZealous

    1030 posts

    Are you adding database to your autoload first?  You need database in there as well.

    Please take a minute to read over the DataMapper User’s Guide.  It explains a lot of this, and other problems you have been having, and it isn’t very long.  DM is slightly different than using straight CodeIgniter in some respects.

    Also, make sure you don’t have any files with leading spaces before the <?php.  This is a well known problem with PHP.  It’s even more of an issue if you have files being saved as UTF-8 with a leading BOM (byte-order mark).  Search the web for “PHP BOM” to see how to remove it.

  • #344 / Dec 08, 2008 10:45pm

    hugle

    289 posts

    Yes OverZealous, I have tried adding database, like

    $autoload['libraries'] = array('database', 'datamapper');

    also.. the problem still exists. If I remove the datamapper from autoload, the redirects and header (‘Location: /’); do start to work.
    I also tried to load even more libraries, redirects work until I add the ‘datamapper’ library..

    I went through the documentation but somehow I didn’t find anything similar that could lead to such a behavior.

    It is strange, because when I remove datamapped from autoloader, it starts to work, so I think it is not the
    PHP BOM issue, or white spaces before the <?php.

    And yes, I was using UTF-8, also tried to save file as windows-1257 - same result.

    I think maybe there could be a white space somewhere in DM library?

  • #345 / Dec 08, 2008 10:53pm

    OverZealous

    1030 posts

    I know that the DM I am using, the latest one, has no whitespace issue.  The whitespace/BOM is probably occuring within one of your models, or within a language file, or something similar.

    DM loads all of the models within your application/models directory on init.  Therefore, if any php file in this directory has this problem, it will show up.

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

ExpressionEngine News!

#eecms, #events, #releases