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 - 04-28-2009

[eluser]OES[/eluser]
I agree apersaud.

OverZealous has done a great ob with the additions.

Maybe OverZealous would you mind starting a new thread with you file ??

Hope you agree :-).

[OES]


DataMapper 1.6.0 - El Forum - 04-28-2009

[eluser]OverZealous[/eluser]
Hah, I apologize. I have been resisting making DMZ a full-on "competitor" to DataMapper, but I know that it might have to be that way.

I'll start a new thread, but I don't have time to update the documentation or examples right now.

If someone wants to help, I'd greatly appreciate it. I believe there is a way to add content to the Wiki, as well, but I don't feel like digging into that right now.

UPDATE:
You can view the dedicated DMZ thread here:
http://ellislab.com/forums/viewthread/112904/


DataMapper 1.6.0 - El Forum - 04-28-2009

[eluser]NachoF[/eluser]
Ok, I just got confused like crazy... the very first example at the user guide shows the Controllers Users being saved in a file called login.php/... whats up with that??.. shouldnt all controllers be named the same as the controller class but not capitalized??..... right now I have an "Event" datamapper model and and when I created an Event controller and tried to create a new instance of Event (referring to the model) and called the get method it gave me an error, Im assuming cause it thinks Im creating an instance of the controller instead of the model... so, anyway, my question is, what should controllers be named? and the files??.. please help.


DataMapper 1.6.0 - El Forum - 04-28-2009

[eluser]OverZealous[/eluser]
@NachoF
The naming scheme is the same as CodeIgniter. DataMapper doesn't change that. The document just has a copy-and-paste error. That file would need to be saved under controllers/users.php, as you would expect.

Also, you can't have two Classes with the same name. So, if your model is named Event, your controller cannot be named Event. Most people use Events, which works well. Alternatively, someone about 2 pages back mentioned using CodeIgniter routes to handle the naming scheme.


DataMapper 1.6.0 - El Forum - 04-28-2009

[eluser]NachoF[/eluser]
Post moved to DMZ thread.


DataMapper 1.6.0 - El Forum - 04-29-2009

[eluser]Freshinc.Olly[/eluser]
Does this support any form of database cache? I have a very large application that im trying to do, which typically will fetch 2500 seperate records for each user every time they use the application. This is a necessary action, but would take a lot of strain off the server if i could cache the records, and then update them when needed.

Regards.
Olly


DataMapper 1.6.0 - El Forum - 04-29-2009

[eluser]OverZealous[/eluser]
No, DataMapper just connects database queries to Objects.

However, it is built on top of CodeIgniter's database, so you should be able to manually use CodeIgniter's database caching. (This took about 2 minutes of googling, for CodeIgniter database cache.)

Just ensure that you build the query the same way each time, and only cache the get() function itself. If you cache otherwise, you won't get the column names, and that will break DataMapper. (Note that this only works if the query is on the same URI every time.)
Code:
$big_ole_blob_o_data = new Thingy();
$big_ole_blob_o_data->where('stuff', 1);

// note: calling cache control on ->db
$big_ole_blob_o_data->db->cache_on();
$big_ole_blob_o_data->get();
$big_ole_blob_o_data->db->cache_off();

// process $big_ole_blob_o_data

If you find yourself using this a lot on different models, extend DataMapper to a new base class, like this:
Code:
class DataMapperCacher extends DataMapper {
    
    var $_should_delete_cache = FALSE;
    
    // Will cache this query on the current page.
    function get_cached($limit = '', $offset ='') {
        if($this->_should_delete_cache) {
            $this->db->cache_delete();
            $this->_should_delete_cache = FALSE;
        }
        $this->db->cache_on();
        parent::get($limit, $offset);
        $this->db->cache_off();
        return $this;
    }

    // Call before get_cached() to clear the current cache;
    function clear_cache() {
        $this->_should_delete_cache = TRUE;
        return $this;
    }
}

Then use that as your base class for the cacheable models:
Code:
class Thing extends DataMapperCacher {
    // use like normal
}

Now, a big caveat: I have not tested anything above. There's no guarantee any of it will work without error, and I have no plans to further test the example at this time. Please take it and run with it. If you get it working, but need changes, please let me know, and I'll update this post so that others can use the feature.

Update:
I just realized this will not work for relationship-based queries. So, make sure you use $obj2->where_related($obj1)->get_cached(), NOT $obj1->obj2->get_cached(). That most definitely will cause issues.


DataMapper 1.6.0 - El Forum - 04-30-2009

[eluser]Freshinc.Olly[/eluser]
Thank you for your reply, we will certainly be using something like this in the future.


DataMapper 1.6.0 - El Forum - 05-04-2009

[eluser]camporter1[/eluser]
I'm still having some trouble with the my self-referencing stuff. I have my User class defined as follows:
Code:
class User extends DataMapper {
    var $has_many = array(
        'associate' => array(
            'class' => 'user',
            'other_field' => 'user'
        )
    );
}

Then, in my controller, I have:
Code:
$user = new User();
$user->where('email', '[email protected]')->get();
$associate = $user->associate->get();
foreach ($associate->all as $a) {
    echo $a->email;
}

This will take an email, and figure out the email of any of that users' associates' email addresses. What happens though is that I end up with a blank screen, meaning it doesn't find aanything.

The table is named associates_users, and the user with that email does have associates.


DataMapper 1.6.0 - El Forum - 05-04-2009

[eluser]OverZealous[/eluser]
@camporter1

You have to specify both sides of the relationship. You need to add in the $user side:
Code:
// assuming $has_one, it's the same for $has_many
class User extends DataMapper {
    var $has_many = array(
        'associate' => array(
            'class' => 'user'
        )
    );
    var $has_one = array(
        'user' => array(
            'other_field' => 'associate'
        )
    );
}

FYI: you don't need to specify other_field if it is the same as class.

Let me know if that works for you.