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]
  • #871 / May 25, 2009 12:52am

    Nabeel

    15 posts

    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.

  • #872 / May 25, 2009 1:12am

    OverZealous

    1030 posts

    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:

    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.

  • #873 / May 25, 2009 1:31pm

    Nabeel

    15 posts

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

  • #874 / May 25, 2009 9:47pm

    tdktank59

    322 posts

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

  • #875 / May 25, 2009 10:51pm

    OverZealous

    1030 posts

    Don’t forget it is foreach($problem->all as $pro).  You are using a DataMapper generated result with my example.

  • #876 / May 25, 2009 10:59pm

    tdktank59

    322 posts

    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.

  • #877 / Jun 01, 2009 12:07am

    tdktank59

    322 posts

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

  • #878 / Jun 01, 2009 12:11am

    OverZealous

    1030 posts

    The object you call save() on contains the last ID. IE:

    $u = new User();
    $u->name = 'John Doe';
    $u->save();
    
    echo $u->id;

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

  • #879 / Jun 01, 2009 12:18am

    tdktank59

    322 posts

    sick! Thanks for the answer saves some time rather than recreating the object…

  • #880 / Jun 01, 2009 12:25am

    OverZealous

    1030 posts

    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.

  • #881 / Jun 01, 2009 12:28am

    tdktank59

    322 posts

    very true. I forgot completely about that… Thanks for the tip!

  • #882 / Jun 01, 2009 1:12am

    tdktank59

    322 posts

    Did we ever get that bug fixed where we had to save the main object before creating a relationship with it?
    The only reason I ask is im not able to test my scripts right now…
    And on another note… If it doesnt work yet we should put it on the dev path for DMZ or DM
    aka:

    $role = new Role();
    $role->name = $name;
    
    $permission = new Permission();
    $permission->where_in('id',$permissions);
    
    $role->save($permission->all);

    Also does the save($permission->all); have to be $permission if only 1 row exists? instead of permission->all

  • #883 / Jun 01, 2009 1:18am

    OverZealous

    1030 posts

    Did we ever get that bug fixed where we had to save the main object before creating a relationship with it?

    This is not a bug.  You cannot save a relationship to a non-existant object.

    However, you can already do the example you provided.  This simply saves the current object (if changed or new), and then immediately saves the relationship(s).  If you are having trouble with that, then you have a bug somewhere.  Always check the boolean result of $object->save()

    Also does the save($permission->all); have to be $permission if only 1 row exists? instead of permission->all

    $permission is always the same thing as the first item in $permission->all.

  • #884 / Jun 01, 2009 1:25am

    tdktank59

    322 posts

    Cool, thanks

  • #885 / Jun 08, 2009 1:09pm

    Khoa

    52 posts

    Firstly, I have to say thank as this is one of the best libraries ever! Great job!

    But I have an issue that I’ve been trying to find in this post but could not as it has about 90 pages :cheese: ! So I will post it again here, sorry if it has been asked.

    I think it is something about saving relationships and object itself at the same time, and also a bit to do with transactions.

    The situation is, for blog we need to save tags for entry, entry has many tags, tags have many entries and so on. I’ve done the saving new entry bit, it’s fine, but when it comes to edit an existing entry which already has a number of tags, it seems a bit tricky to me.


    SITUATION:

    1. Entry has a validation rule that tag is required
    2. Entry 1 has been created with 4 tags: tag1, tag2, tag3 and tag4
    3. User updates it and clears all the tag. This should fail and all tags stay there.


    WHAT I DID:

    $entry = new Entry();
    $entry->get_by_id($id);
    
    // (D) Delete all existing tags inside memory
    $entry->tag->delete_all();
    
    // (L) Then loop through new tags coming from post and add them in. 
    // Nothing in this case as user does not enter anything
    
    // Finally save the changes of other details like title, etc.
    $entry->save();


    WHAT HAPPENED:

    - It does not allow user to save as it does not pass validation (requires tag). This is GOOD.
    - However, all tags are gone because they have been deleted before the validation happens! This is BAD.


    WHAT I EXPECTED:

    - Display a message saying that tag is required
    - Nothing changes in the DB, no tag is deleted.


    QUESTION:

    - How could I update the entry->tag without changing the DB so that when validate() runs at the save() command, it fails and nothing changes?

    I tried to use transactions like this but no luck:

    - trans_begin()
    - delete()
    - trans_rollback()

    Transaction does not recover records that have been deleted when rolling back.

    Thanks.

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

ExpressionEngine News!

#eecms, #events, #releases