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]
  • #466 / Feb 02, 2009 4:07pm

    OverZealous

    1030 posts

    @Daniel H
    Currently, there is no way to have the different relationships to the same object without subclassing the duplicated object, as if it were a self-reference.

    It depends on what your needs are, but the basic way to do this is to create your objects like so:

    class User extends DataMapper {
        $table = 'users';  // here for clarity, not required
        $model = 'user'; // ditto
        // add in validation, etc.
    }
    
    class EditingUser extends User { // note, extends User!
      $table = 'users'; // required!
      $model = 'editinguser'; // here for clarity, not required
      // nothing else is needed in this class
    }
    
    class Task extends DataMapper {
       $has_one = array( 'user', 'editinguser' );
    }

    Now your table is a little trickier:

    -- this joining table is named after the TABLE for users
    CREATE TABLE tasks_users (
        id SERIAL NOT NULL,
        task_id integer NOT NULL,
        user_id integer,
        editinguser_id integer
    );

    That table will handle both of the joins.

    There is one important drawback: the EditingUser cannot be used for other join operations!  DataMapper will always look for editinguser_id on the joins, so you can’t, for example do this:

    $task = new Task();
    $task->get_by_id(10);
    // assume a user has multiple phone numbers
    $task->editinguser->get()->phonenumber->get();

    That will fail because there is no editinguser_id column on the phonenumbers_users table.

    @bEz
    You don’t need to shout it 😉, but DataMapper does not have any built-in mechanisms for dealing with multiple DataBases.  What is it, exactly, you are trying to achieve?  Cross DB queries?  Look up in one then the other?

    DataMapper uses the same DataBase library as CI, so you might be able to hack something in by replacing the Model’s db element:

    // do queries on default DB
    $obj = new Object();
    $obj->get();
    // now query alternate database
    $obj->db = $this->load->database('other_db', TRUE);
    $obj->get();
    // obj2 will still be using the default database.
    $obj2 = new Object();

    If you ALWAYs need a different database for a specific model, you could create a custom constructor, like so:

    class Thing extends DataMapper {
        function Thing() {
            parent::DataMapper();
            $CI =& get_instance();
            $this->db = $CI->load->database('otherdb', TRUE);
        }
    }

    (I haven’t tested this, these are just ideas that might work for you)

  • #467 / Feb 02, 2009 5:16pm

    Daniel H

    197 posts

    Thanks overzealous, so I think the most sensible thing is to have an ‘owner’ and an ‘editor’ object which have 1-1 relationships with a user. I don’t want to start using dm in a way it isn’t designed to work.

    I can’t imagine this would be that much of an overhead, and keeps thing pretty clean anyway…

    Cheers.

  • #468 / Feb 02, 2009 5:36pm

    OverZealous

    1030 posts

    I creating sub-classes is OK for DM.  It’s the only way, for example, to create a self-reference.

    That being said, your solution is fine (I’ve thought of doing it myself), however, it does add a few new queries into each lookup, possibly even adding many queries on loops.

    If you use the 1-1 relationsips, due to the 5th-form join tables, you now have to query:
      - Once for the list of tasks
      - Once for the Owner object
      - Once for the Owner’s User object
      - Once for the Editor object
      - Once for the Editor’s User object

    Obviously, this could be problematic looping through a large set.  Using the subclass method is only 3 queries, unless you need access to the EditingUser has_one or has_many elements.

  • #469 / Feb 02, 2009 6:07pm

    bEz

    110 posts

    @OverZealous.com
    Actually, I thought I formatted it with a whisper tag 😉

    Seriously though, thanks for the reply.  I’m trying to utilize DM with my app, however the user table is in another database (phpBB3). 

    Keep in mind I’m new to the CI (frameworks as a whole) method of coding.  So my concern was that in the learning curve, whether the DM (ORM) was capable of doing multiple DBs like CI core does it.

    I’m more likely to need access to the forum db for more than just login purposes, so “ALWAYS ON” method is what I’m trying to acheive.  My alternative solution would be to develop my with one of the “AUTH” library releases.

    I will head back to my dev lab and test the methods provided.  It’s likely that my ALWAYS ON choice is actually not that, but rather a quick read to synchonize with the main/default db.

    -bEz

  • #470 / Feb 02, 2009 10:09pm

    OES

    127 posts

    OK related Object Count is just not playing ball for me.

    I have 3 tables as follows

    ads | zones | ads_zones (relationship table)

    They relate no prolem for normal gets etc but I cannot count the ads which are active related to the zone. Folling the User guide on the I just keep gettings erros.

    Here is my code.

    // Create new Ad Object
    $a = new Ad();
    
    // Create new Zone Object and get by the ID of 1
    $z = new Zone();
    $z->get_by_id(1);
    
    // Populate my var with the count where ad is active.
    $count = $z->ad->where('active', TRUE)->count();

    SQL error is showing its trying to count the active field in the related table ??  I know in needs to count the reated fields but the where should be on the ad table.

    Here is SQL error.

    Unknown column 'ads.active' in 'where clause'
    
    SELECT COUNT(*) AS `numrows` FROM (`ads_zones`) WHERE `ads`.`active` = 1 AND `zone_id` = '1'

    I just cant understand why its creating this query.

    Really hope you can help. Have lost a few hours on this one.

    Thank you in advance.

  • #471 / Feb 02, 2009 10:15pm

    OverZealous

    1030 posts

    I don’t remember, but I think this is a bug in DM I’ve had as well.

    I’m pretty sure the best solution is to write the query like this:

    // notice, using where_related instead of $z->ad
    $count = $ad->where_related($z)->where('active', TRUE)->count();

    This should run without error.  You might also need to write it as:

    $count = $ad->where_related($z)->where('ads.active', TRUE)->count();

    If the active column occurs in both zones and ads.

  • #472 / Feb 03, 2009 7:51am

    macigniter

    244 posts

    has anyone had problems that timestamps wouldn’t be saved although the fields are named “created” and “updated” in the database?

    it seems that dm just wouldn’t save or update my timestamps :(

    this is what i have in my config file:

    $config['created_field'] = 'created';
    $config['updated_field'] = 'updated';
    $config['local_time'] = FALSE;
    $config['unix_timestamp'] = TRUE;
  • #473 / Feb 03, 2009 11:04am

    tdktank59

    322 posts

    @ the guy with the SQL problem

    Check the mysql driver ( the troubleshoot page ibln the manual has the info)


    @macigniter
    Timestamps are working fine for me. At least I think they are.

  • #474 / Feb 03, 2009 1:25pm

    OverZealous

    1030 posts

    @macigniter
    Are you getting an error, or are they just not being updated?
    If the latter, are they never being updated, or just some of the time?
    What is the type of field are you using?

  • #475 / Feb 03, 2009 1:37pm

    macigniter

    244 posts

    I did not get an error message. They just haven’t been updated (actually they showed up as “0000-00-00 00:00:00” in PhpMyAdmin.

    Weird enough (although I didn’t change anything) it does work now. The field type is “timestamp”.

    Very strange… I’ll keep an eye on it!

  • #476 / Feb 03, 2009 1:39pm

    tdktank59

    322 posts

    Is there a way to get thing out of the relation table?

    For example: I need to get start_date and end_date from the roles_users table

    users
    id
    username
    email
    password
    etc…

    join_roles_users
    id
    role_id
    user_id
    created_on
    updated_on
    start_date
    end_date

    roles
    id
    name
    description
    created_on

    UPDATE:
    For the moment im just using custom queries attached to the page and method models. If there is a way to do it with Datamapper sweet otherwise this is a work around for it at the moment…

  • #477 / Feb 03, 2009 1:41pm

    OverZealous

    1030 posts

    @tdtank59
    You’re using the only working solution for now.  (I’m doing likewise.)

    The closest alternative (and I don’t recommend it) is to create a dedicated model that contains the relationships and the extra info.  That ends up generating even more queries.

  • #478 / Feb 04, 2009 7:51am

    stensi

    109 posts

    @tdktank59: Yeah, there’s currently no way to grab items from the joining tables.  That’s one of the many features I want to include in coming versions.  You might want to look back through the posts at what commandercool has posted since I think he might have posted something relating to this issue.

    ____________________________

    Just as an update to everyone, sorry for not being that active lately!  I’m currently busy with 2 projects and as such I’ll have very little spare time for DataMapper until they’re complete.

    I’m yet to really have a proper look at the added functionality by commandercool and OverZealous.com but they’ll be the first things I get into after the project work is over, which may be a while…

  • #479 / Feb 04, 2009 5:30pm

    bEz

    110 posts

    ❓ Not to distract to much from DM development, so I apologize if I’m asking for help by replying to the DM thread.

    In a previous app, I managed a league DB using the following setup (just a few of the many tables used).

    seasons (seasonID, ..., ..., ...)
    conferences (conferenceID, seasonID, ..., ..., ...)
    divisions (divisionID, conferenceID, ..., ..., ...)
    teams (teamID, divisionID, ..., ..., ...)
    players (playerID, teamID, ..., ..., ...)

    Now that I’m utilizing DM, I’m restructuring this schema as follows:

    NORMAL TABLES

    seasons (id, ...)
    conferences (id, ...)
    divisions (id, ...)
    teams (id, ...)
    players (id, ...)

    apparently I would need the following as well:

    JOIN TABLES

    conferences_seasons (id, conference_id, season_id)
    conferences_divisions (id, conference_id, division_id)
    divisions_teams (id, division_id, team_id)
    players_teams (id, player_id, team_id)

    What I need to confirm are the following:
    (A) Am I required to create other join tables (divisions_seasons, seasons_teams, ...) in order to pull data? (the user guide has been helpful, but I’m still in learning mode)

    (B) Can I possibly get a few examples, best approaches, to populating the join tables.

    i.e: 
    I create a new season (object & data of course).
    for that season I create two conferences
    in each conference I create two divisions
    in each division I create approxitmately 6 teams
    on each team I create 15 players.

    The entire approach can be inserted using cookie cutter data (season1, conference1, conference2, division1, division2, team1, team2, ...) or each entity 1 at a time.
    If cookie cutter, I could utilize a datagrid to update the data after temporary data has been “saved”, which brings up another request:

    (C) Has anyone used a datagrid CI implementation with DM?

  • #480 / Feb 04, 2009 8:47pm

    bEz

    110 posts

    Addendum to (A) above:
    Would additional join tables be required to access relationships of another object?
    Conferences => Divisions
    Divisions => Teams
    Teams => Players

    In order to determine all the players in a conference, would it require
    conferences_players (id, conferences_id, players_id)

    or would the association of the other join tables be enough to somehow access that relationship?

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

ExpressionEngine News!

#eecms, #events, #releases