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]
  • #736 / Apr 22, 2009 4:07pm

    ale21ale

    3 posts

    HEllo my friends !

    I have a doubt. How can I manage datamapper from a LOCK TABLE in Mysql?.
    Using transactions in my application but there are records that can be accessed by users and 2 if a user is editing the record and another user deletes an error occurs in the DB. How do you solve this?

  • #737 / Apr 22, 2009 5:04pm

    OverZealous

    1030 posts

    You have to manage that in the application.  Like you would in any application.  DataMapper is just glue.  You still need to build the structure yourself.

    If you need object or row locking, you’ll have to manage it yourself.

  • #738 / Apr 22, 2009 5:14pm

    ale21ale

    3 posts

    Sorry Phil. Is that the question could have been very stupid. I use google translator jeje I not write in English very well. I know the lock tables are managed by the database engine, thought he could have some method in the library datamapper.

  • #739 / Apr 22, 2009 5:41pm

    OverZealous

    1030 posts

    Ahh, I apologize, then!

    Hopefully it came across, then, DataMapper does not handle anything like locking.  You’ll need to manage in your own code.

    If you come up with a standardized way of locking a row, you can always add it to a subclass of DataMapper, and then use that as your base class for your models:

    class DataMapperLocker extends DataMapper {
        
        function is_locked() {
            // look up and return whether this row is locked, based on $this->id.
        }
        function lock() {
            // handle locking the row or object
        }
        function unlock() {
            // handle unlocking this row or object
        }
    }
    
    // in your class
    class MyWidget extends DataMapperLocker {
        // configure like normal
    }

    This is something I’ve been wanting to do as well, however, it requires a lot of interface coding, so I’ve been putting it off.  The biggest issue for me is the more complex issue of asynchronous editing, and ensuring that rows are unlocked when necessary.  Otherwise it can be really annoying for end users if someone locks a row, but then gets disconnected.

  • #740 / Apr 22, 2009 9:03pm

    warrennz

    56 posts

    Is there anyway that datamapper supports multiple databases? Can this sort thing even support multiple data sources?

    I’ve had a good look throughout the files and nothing seems to give any indication it does.

    Maybe I could just redefine $this->db at the start of my model with a new db config, and then on __destruct() reset it to the ‘default’ db config. Ideas?

    And also, anyway to do this sort of thing or is datamapper not really intended for that..
    example.png


    Thanks!

  • #741 / Apr 22, 2009 10:46pm

    OverZealous

    1030 posts

    Multiple DataBases really wouldn’t work with DM, because it’s main goal is to help with mapping relationships to objects.  There isn’t much call for mapping relationships across multiple DBs.

    Second, if I understand correctly, are you trying to merge multiple rows into one row??  That is an incredibly bad idea, as it makes the database really inefficient.  There is no reason why your DB can’t handle hundreds-of-thousands of rows, unless it really sucks (like Access or something).  Just set up proper indexing.  If you know the database is read heavy, you can go nuts on the indexes to really help queries run faster.

    Finally, DataMapper (original) doesn’t handle multiple relationships to the same object very well.  My extension (DMZ, search this board) handles them better, but what you are doing doesn’t look much like a highly normalized DB.  Unless your DB is normalized, I doubt DM will work for you, much less be very efficient.

  • #742 / Apr 22, 2009 11:10pm

    warrennz

    56 posts

    Hey OverZealous

    Thanks for yet another very good reply

    The db/tables in question are managed by a proprietary system that I have no control over and the tables them selves are essentially logs (of the beaten track its a radius server and the logs are internet usage and connectivity logs). Querying them isn’t really a problem in terms of performance I just didn’t think it was a very good idea to duplicate a relationship row for every record given than there are a allot of them.

    Anyway, ill just stick to standard CI’s AR for the moment.

    I tried for along time to get multiple relationships on the smae object to work with DMZ but just couldn’t. Maybe ill have another crack at it when I understand what DM(Z) is doing a bit more 😛

    Thanks again OverZealous

    On a slightly different note, is there anyway to store a ‘created_on’ field in the relation table? and have DM set the right time etc like it does with a normal model. Could just use a trigger I surpose..

  • #743 / Apr 23, 2009 1:00am

    OverZealous

    1030 posts

    There’s nothing in DM currently to handle dates on relationship tables.  I never thought of that.

    I apologize that DMZ is complicated.  The real problem was trying to shoehorn better relationships into the DM design.  Personally, I’d prefer to have the all of the relationship data stored in a single file, so that a lot of the duplicate information could be parsed automatically.  However, DM just isn’t set up for that, and I didn’t want to break any existing code to make it work.

    I’ve thought about writing a little JS/PHP application to help kickstart a DMZ project by allowing you to describe the models and relationships, and then having it kick out the PHP code.  But I’ve been extremely busy, and just haven’t had the time.

  • #744 / Apr 23, 2009 7:27am

    Peet86

    14 posts

    Hello Guys!

    I want to translate this query to Datamapper methods, but I have some preblemes with:

    SELECT node.name, (COUNT(parent.name) - 1) AS depth
    FROM nested_category AS node,
    nested_category AS parent
    WHERE ...

    I found the count function in Datamapper documentation, but how can I define the “AS” ?
    I dont want to make new models for self referencing, because this is a nested set model query. I want to get a category tree data with this.

    Thanks!

  • #745 / Apr 23, 2009 7:32am

    warrennz

    56 posts

    @OverZealous:
    I see I see. Makes sense really. Will be very interested to see what happens with your DMZ project..


    Re: dates - What I’ve done is created a ‘join_created’ field in the relationship table. Then created a mysql trigger to insert a time stamp when a record is created(INSERT). Then just add ...->where(‘join_created <’,‘some_time_stamp’)->..in my chain. Because join_created is the only field with that name it wont create an ambiguous error. This is probably really REALLY bad design but I don’t have any other quick fix for now 😛. I don’t understand DM(Z) enough to extend it to support created and edited fields in the relationship table, maybe someone else does? 😊

  • #746 / Apr 23, 2009 7:58am

    OES

    127 posts

    I have a relationship table which links users to categorys. Is it possible for DM query to check to see if a user is part of that category.

    In normal AR its a simple look up of the table to see if the user_id & category_id excists in the table. ie (model query).

    function check_member($cat, $user)
        {
            $array = array('category_id' => $cat, 'user_id' => $user);
            $this->db->where($array);
            $query = $this->db->get('categories_users');
            if ($query->num_rows() > 0){
               return TRUE;
            }
            return FALSE;
    
        }

    Any suggestions on if this can be done via DM instead.

    Hope you can advise.

  • #747 / Apr 23, 2009 8:06am

    warrennz

    56 posts

    $u = new User()
    $u->get_by_id($UserID);
    
    return $u->category->get_by_id($CatID)->exists()? true : false;

    EDIT:
    Explanation

    $u is your user object
    $u->category->get(); will return all the rows of categories that belong to that user( depending on relationship type - many or one etc)

    $obj:get_by_id() is essentially an extension of where(‘id’,‘someid’);

    So $u->category->get_by_id($catID); will firstly limit the query down to all categories that belong to that user, then (as per get_by_id) limit it down to the category with that $CatID

    This what you mean?

  • #748 / Apr 23, 2009 8:23am

    OES

    127 posts

    Go Damn.  I didnt see the excists function.

    Much better.

    Perfect than you

  • #749 / Apr 23, 2009 8:25am

    warrennz

    56 posts

    You’re welcome

    To note

    exist() runs

    if(empty($this->id))
    ...

    $this->id being, in your case, the category id - not the relationship category id but the actual category table id. SO, if the category relationship is there but the resulting category is not, exists() will return false.

  • #750 / Apr 23, 2009 10:11am

    naren_nag

    81 posts

    I have a table called people, a table called workexperiences and a table called companies. I have three corresponding models: person, workexperience and company.

    A person has many workexperience
    A workexperience has one company

    After adding two hundred relationships between person and workexperience, the database has stopped saving relationships in the join tables.

    So individual records continue to get added to people, workexperience and company. But none of the join tables are getting populated.

    The last auto id in the people_workexperiences join table is 199.
    The last auto id in the companies_workexperiences join tables is 150.

    Has anybody come across a bug of this sort?

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

ExpressionEngine News!

#eecms, #events, #releases