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 - 03-13-2009

[eluser]OverZealous[/eluser]
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:
Code:
$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! Tongue


DataMapper 1.6.0 - El Forum - 03-13-2009

[eluser]Roobiz[/eluser]
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?


DataMapper 1.6.0 - El Forum - 03-13-2009

[eluser]OverZealous[/eluser]
Yes. If you need more specialized queries, you'll have to write them yourself.


DataMapper 1.6.0 - El Forum - 03-13-2009

[eluser]Roobiz[/eluser]
Thanks Smile

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


DataMapper 1.6.0 - El Forum - 03-13-2009

[eluser]tdktank59[/eluser]
Im still having an issue OverZealous...

Code:
/**
     * 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


DataMapper 1.6.0 - El Forum - 03-13-2009

[eluser]OverZealous[/eluser]
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.


DataMapper 1.6.0 - El Forum - 03-16-2009

[eluser]OverZealous[/eluser]
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:
Code:
// 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.


DataMapper 1.6.0 - El Forum - 03-16-2009

[eluser]OES[/eluser]
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.

Code:
// 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.


DataMapper 1.6.0 - El Forum - 03-16-2009

[eluser]gyo[/eluser]
@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!


DataMapper 1.6.0 - El Forum - 03-16-2009

[eluser]OES[/eluser]
Because work on the old one stopped. Stensi is to busy I think