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 - 05-24-2009

[eluser]Nabeel[/eluser]
Thanks for the reply. I usually write things "low level" within MVC, so the queries, templates, etc. I wrote a little framework I've been using commercially for a little while, but moving to something more prevalent is a good idea. At work we do most things 'in house' in-terms of framework, it's hard to sometimes 'bust out' of that thinking.

I'm using DataMapper now (well, DMZ), though it seems I've run into a bit of a roadblock with naming models. In IR, it's <table>_model, which works out well, since now I have naming conflicts between my controllers and models in some instances. I have a "schedules" controller, as well as "schedules_model". So I specified the table name... no problem there. But since the join column is <model name>_id, I can't do schedules_model_id (unless I've misunderstood how that works). I'm still messing with it a bit

Oye.


DataMapper 1.6.0 - El Forum - 05-24-2009

[eluser]OverZealous[/eluser]
I think the docs are fairly clear, but this is how it works for the model Schedule:

PHP Class: Schedule
DM "Model": schedule
Table name: schedules
FK names: schedule_id
Many-to-many table related to User: schedules_users (cols: id, schedule_id, user_id)
Many-to-many table related to Event: events_schedules (cols: id, event_id, schedule_id)
Many-to-one related to Alarm (schedule has many alarms): add schedule_id to the table alarms
One-to-many related to Account (account has many schedules): add account_id to the table schedules

Now, because the model is called Schedule, you cannot have a controller called Schedule, otherwise the names would collide. The recommended format is to pluralize your controllers, Schedules. If that doesn't work(i.e.: like Moose or Deer), then you'll need to rename either the model or the controller.

If you need to override the names, say for a class called Address (a common problem), there are two properties in the model you can override: $table and $model:
Code:
class Address extends DataMapper {
    var $table = 'addresses'; // table name
    var $model = 'address'; // used for column names, like address_id
    // continue on with the class definition
}
Most of the time, DM will figure out the names correctly.

* This all assumes you are not using a prefix on tables or join tables (but those are easy, just add them as prefixes).
† DMZ only, otherwise use the many-to-many format.


DataMapper 1.6.0 - El Forum - 05-25-2009

[eluser]Nabeel[/eluser]
Thanks for all the information! Sometimes its a little tough for me to parse the documentation, gonna go at it again today. Appreciate it!


DataMapper 1.6.0 - El Forum - 05-25-2009

[eluser]tdktank59[/eluser]
@OverZealous
Ok well the query part works... However I can't get the data... I think im just being stupid or something lol...

but foreach ($problem as $pro) does not work as well as $problem->result() as $row dosnt work...


DataMapper 1.6.0 - El Forum - 05-25-2009

[eluser]OverZealous[/eluser]
Don't forget it is foreach($problem->all as $pro). You are using a DataMapper generated result with my example.


DataMapper 1.6.0 - El Forum - 05-25-2009

[eluser]tdktank59[/eluser]
LOL told ya it was something stupid like that lol..

That fixed it btw...

Also have to change the where_in_related_problems to where_in... the first one didn't work for some reason.


DataMapper 1.6.0 - El Forum - 05-31-2009

[eluser]tdktank59[/eluser]
Is there anyways to grab the last ID inserted?

for example I am creating a new entry into the database, however I need to use the ID right after I create it. Is there a way to get this? (I looked through the userguide and I could not find anything about it... I did however just scan the pages...)


DataMapper 1.6.0 - El Forum - 05-31-2009

[eluser]OverZealous[/eluser]
The object you call save() on contains the last ID. IE:

Code:
$u = new User();
$u->name = 'John Doe';
$u->save();

echo $u->id;

This allows you to use a saved object immediately, as well.


DataMapper 1.6.0 - El Forum - 05-31-2009

[eluser]tdktank59[/eluser]
sick! Thanks for the answer saves some time rather than recreating the object...


DataMapper 1.6.0 - El Forum - 05-31-2009

[eluser]OverZealous[/eluser]
FYI, DataMapper just rides on top of CodeIgniter's Database classes. Therefore, you can always find the last insert ID using $u->db->insert_id(), as well as using any of the other database methods. (However, you need to be contained within a transaction to ensure that the correct insert_id, as concurrent inserts can incorrectly change the value.)

Also, the insert information for DataMapper is outlined here, right at the top.