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]
  • #646 / Mar 10, 2009 10:58pm

    tdktank59

    322 posts

    ok guess i didnt read the manual right then.

  • #647 / Mar 10, 2009 11:13pm

    OverZealous

    1030 posts

    http://stensi.com/datamapper/pages/save.html]Save: DataMapper User Guide - scroll down about halfway to Save Multiple Relations.  It shows the ->all method of saving.

  • #648 / Mar 11, 2009 9:28pm

    dmyers

    43 posts

    Any way to combine the active record “sql” builder into the datamapper object?

    You can do this in Datamapper and “push” a raw sql query into a datamapper object

    $sql = "SELECT * FROM `users` WHERE `username` = 'Fred Smith' AND `status` = 'active'";
    
    $u->query($sql);

    and you can do this to dynamically build a sql statement using the built in Active Records

    $query = $this->db->get('auth_user');

    but can you jam the active record “builder” into the datamapper object?

  • #649 / Mar 11, 2009 9:39pm

    OverZealous

    1030 posts

    I don’t know what you are asking, but DataMapper just uses ActiveRecord internally.  Calling $this->db->anything or $obj->db->anything modifies the ActiveRecord query just as it normally would.

    Then calling $obj->get() will process the results.

  • #650 / Mar 13, 2009 7:02am

    Roobiz

    14 posts

    Hi guys,

    I have a little problem with DM and count’s method.

    My tables:

    b_messages
    -id
    -is_view
    -created
    -updated
    -ip

    b_topics
    -id
    -id_user_from
    -id_user_to
    -created
    -updated
    -title

    b_messages_b_topics
    -id
    -b_message_id
    -b_topic_id

    I Want to count all the non viewed messages linked to my topic (where my id == to id_user_from or id_user_to.

    I test this kind of query but without any success:

    $btopic = new B_topic();
    $btopic->where('id_user_from', $myId);
    $btopic->where('id_user_to', $myId);
    $btopic->b_message->where('is_view', 0);
    $btopic->b_message->count();

    Can anyone help me?

  • #651 / Mar 13, 2009 12:47pm

    OverZealous

    1030 posts

    The example you wrote doesn’t make any sense.  Remember, in the end, DM is just converting what you write to SQL queries.

    First, when you call ->where(), that is always an AND in SQL.

    Second, you don’t have a ->get() on your $btopic().

    Finally, ->count() can only count the results of a single query.  This means that if you want to count something recursive - like all the b_message of a set of btopics, you are going to have to loop through each btopic individually and add up the results.

    This gives you something like:

    $btopic = new B_topic();
    $btopic->where('id_user_from', $myId);
    $btopic->or_where('id_user_to', $myId);
    $btopic->get();
    
    $total = 0;
    foreach($btopic->all as $bt) {
        $total += $bt->b_message->where('is_view', 0)->count();
    }
    // $total is all items.

    Don’t expect DataMapper to read your mind! 😛

  • #652 / Mar 13, 2009 1:01pm

    Roobiz

    14 posts

    Thanks for your help

    Ok sorry in my example I forgot the or_where but in my test I write it.

    So with your code, if I have 10 topics, a the end CI will execute 11 query?

    1 to get all my topics, 10 to get the number of non viewed message per topic? Right?

  • #653 / Mar 13, 2009 1:09pm

    OverZealous

    1030 posts

    Yes.  If you need more specialized queries, you’ll have to write them yourself.

  • #654 / Mar 13, 2009 1:20pm

    Roobiz

    14 posts

    Thanks 😊

    So first I will use your method but if I need more performances I will make my own query 😊

  • #655 / Mar 13, 2009 3:13pm

    tdktank59

    322 posts

    Im still having an issue OverZealous…

    /**
         * Join permissions to a role
         *
         * @param varhcar/array $permission, $key=>$value
         *  defaults to 'id' if you do not pass an array with $key=>$value
         * @param array $role - $key=>$value to get the role object
         * @param boolean $create_role TRUE/FALSE to create the role being passed
         * @return boolean TRUE/FALSE
         */
        public function join_permissions_role($permission, $role, $create_role=FALSE)
        {
            if ($create_role)
            {
                if (!$this->create('role',$role))
                {
                    return FALSE;
                }
            }
    
            $r = new Role();
            $r->where('id',$role['id'])->get();
    
            if(!is_array($permission))
            {
                // allow for single fields
                $permission['id'] = $permission;
            }
    
            foreach ($permission as $key => $val)
            {
                $p[$i] = new Permission();
                $p[$i]->where($key,$val)->get();
            }
    
            if ($r->save($p))
            {
                return TRUE;
            }
        }

    What this is doing is created the role object and then creates multiple permission objects to join it (Roles = one to many = Permissions) however im not sure if the above model will work?

    If the above dosnt work I have a work around where I loop everything bascially

  • #656 / Mar 13, 2009 4:20pm

    OverZealous

    1030 posts

    Just try it.  It either works or it doesn’t.  If you are getting an error, or having a problem, I’ll try to help.

  • #657 / Mar 16, 2009 4:37am

    OverZealous

    1030 posts

    DataMapper OverZealous Edition (DMZ) 1.2!

    For those of you who haven’t been following along, I have been making changes to the core of DataMapper that hopefully will help improve DataMapper’s usability.  The new features include:
      • One-to-N relationships without a Join Table
      • Multiple Relationships to the same object type
      • Better Self References
      • Include the fields from a related object in a result

    Recently, I have decided to tackle one of those annoyances that has been requested several times:  Accessing extra information stored on a join table!

    I simplified the problem a bit, so that, instead of supporting a whole range of features, I only support changing, viewing, and querying on extra columns that exist on join tables.

    Some basic examples

    First, assume I have two objects, User and Alarm.  Each alarm can be related to multiple users, and each user can have multiple alarms.  Suppose when a user logs in, or refreshes the page, (or some fantastical AJAX query runs), any current or overdue alarms are shown to the user so they can react.

    We want to keep track of which user has seen which alarm, so we don’t show it to the same user more than once.  How do we keep track of this?  By adding a column to alarms_users called viewed, which defaults to FALSE.

    Here’s some examples of how to work with it:

    // get all non-viewed alarms for this user (you'd probably want to add in something to check the timestamp, as well)
    // note: we have to tell the Alarm which table and object id to look at, that's why $user is passed in again
    $user->alarm->where_join_field($user, 'viewed', FALSE)->get();
    foreach($user->alarm->all as $a) {
        echo($a->message);
        // mark this alarm as viewed.
        $a->set_join_field($user, 'viewed', TRUE);
    }
    
    // Query all alarms for this user, and include any extra join columns
    $user->alarm->include_join_fields()->get();
    echo("<ul>");
    foreach($user->alarm->all as $a) {
        $has = $a->join_viewed ? 'has' : 'has not';
        echo("<li>Alarm {$a->id} $has been viewed.</li>");
    }
    echo("</ul>");

    If you want to try the new features, the attached file includes a replacement for DataMapper - simply replace the copy you have in libraries.  There also is a slightly improved document explaining how the new features work, with more examples and less technical information.  It isn’t perfect, because I’m limited in how much time I can provide for this, but hopefully it will get you jump-started.

  • #658 / Mar 16, 2009 10:19am

    OES

    127 posts

    Really nice work OverZealous

    Im still a little confused with what you have added but If I give you my example can you spot a better way of doing it ??

    App like a blog with categories & comments.

    So I would have a controler and a function to collect 20 posts like this.

    // Controller Function
    $p = new Post();
    $p->where_in('category_id', array('football', 'rugby') );
    $p->limit($limit, $offset);
    $p->order_by("created", "desc");
    $p->get();
    
    // Build Posts Related Objects
    foreach($p->all as $post){
      $post->user->get();
      $post->category->get();
      $post->comment->get();
      $post->count = $post->comment->count();
    }
    
    // Pass to view
    $data['posts'] = $p->all;

    So I obviously 2 related tables with categories & comments.

    Could I do this a better way ?

    Hope you can advise.

  • #659 / Mar 16, 2009 11:06am

    gyo

    170 posts

    @OverZealous: why did you fork the library instead of contributing to the existing one? I’m only asking because I currently use DataMapper, but I’m interested in new features and I don’t which one to go with.

    Thanks!

  • #660 / Mar 16, 2009 11:09am

    OES

    127 posts

    Because work on the old one stopped. Stensi is to busy I think

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

ExpressionEngine News!

#eecms, #events, #releases