Welcome Guest, Not a member yet? Register   Sign In
DataMapper 1.6.0

[eluser]wolffc[/eluser]
I see, thanks for the help.

[eluser]bibstha[/eluser]
Stuck with database design to satisfy the Datamapper's structure.

What I have is events table and users table

events
----------
id
name

users_
----------
id
username
password
...

Now a User can be related with an event but the relation can be of several type. Say the relationship between user and event can be invited, confirmed, participated, ...

So i have the table events_users

events_users
------------
id
event_id
user_id
participation_type (ENUM with invited, confirmed, participated)

Now I cannot seem to find a way to populate participation_type field, coz both events_id and user_id are automatically filled by DM.

Also, is there any way to restructure the Database?

Thanks

[eluser]bibstha[/eluser]
Oh I guess DMZ will be able to handle that..
I am taking a look at this. But if DM cannot by default handle it, isn't that a serious limitation?

[eluser]OverZealous[/eluser]
@bibstha
I don't think it is that big of a limitation. You can always come up with different ways to relate information. In the past, I created a dedicated model to handle complex joins. Honestly, the inability to relate the exact same two objects more than once was a bigger limitation.

Anyway, that's why I continued development on DataMapper as DMZ ;-) ? stensi has not been able to work on it in a while.

(Also, in case it isn't obvious, DMZ still does not offer a way to relate the same object multiple times where the primary key would be on [event_id, user_id, participation_type]. Meaning, unless you relate them using different related_fields, each User can only be related once per Event. But I believe you aren't expecting that, here!)

[eluser]camporter1[/eluser]
Hello again. I know that it's possible to get all of my user's associates, but how might I go about doing the reverse, and figuring out which other users have a specific user as an associate? My model and stuff is back on page 83.

Code:
$user->associate->get();
gets the associates I want, but I want to see if the user is the associate for another user.

I was thinking I could do the following:

Code:
$user = new User();
$associate = new User();
        
$user->where('id', '2')->get();
        
$associate->associate->where('id', $user->id)->get();

foreach ($associate->all as $a) {
    echo $a->email;
}
But obviously this doesn't work Smile

Thanks.

[eluser]OverZealous[/eluser]
It's just
Code:
// this is the user
$user = new User();
$user->get_by_id($userid);

// this is the associate to check
$associate = new User();
$associate->get_by_id($as_id);

// get the user for this associate (we just need the id)
$associate->user->select('id')->get();

$is_assoc_for_user = $user->id == $associate->user->id;
because, in this instance, the reciprocal relationship was $user (ie: User->user is the parent, User->associate is the child.)

Think of the relationship like this:
Code:
.-----------. 1   N .-----------.
|   User    |------<| Associate |
'-----------'       '-----------'

Where User and Associate just happen to be the same class.

[eluser]camporter1[/eluser]
What is $as_id if I don't know what associate I'm initially looking for?


Basically all I know is that I have the main user's id (e.g. 2).

I want all other user's associates and if I find that the main user's id is an associate for one of them, then I want to return that user that has them as an associate.

It is similar to a friends relationship, where a person can have someone as a friend, but not until the other person adds them as an friend as well will the application think that they are mutual friends (if that makes any sense). Maybe this database configuration is wrong?

It seemed to work (albeit less gracefully) when I was using just queries, but obviously it was more messy.

[eluser]OverZealous[/eluser]
Let's use the friends as an example - it's easier to understand:
Code:
// This user's friends
$user->friend->get();

// see if the relationship is reciprocated:
foreach($user->friend->all as $f) {
    if($f->user->select('id')->get()->id == $user->id) {
        // OMG! BFF!
    } else {
        // :-(
    }
}

// Single query to only get friends
$user->friend->where_related('friend', 'id', $user->id)->get();

// One-sided friendship:
$user->save_friend($other_user);

// To make two-sided:
$other_user->save_friend($user);

Replace 'friend' with 'associate' as needed. Apply only as directed. Side effects may include awesomeness, rock-star code monkey status, and utter lack of SQL code.

[eluser]camporter1[/eluser]
OK, but I'm trying to check if there are essentially what social websites like to call 'unconfirmed' friends. (Not that the other code wasn't unnecessary, it was actually incredibly useful as well.

Instead of limiting it to only the associates that the user has, I did this to check all associates:
Code:
$user = new User();
        $associate = new User();
        
        $user->get_by_id('2');
        $associate->get();
        foreach($associate->all as $a) {
            if ($a->associate->select('id')->get()->id == $user->id) {
                echo $a->id . "<br>";
            } else {
            }
        }
So I could see whether it was echoing the associates that have my user as an associate. It seems to work. It probably is extremely inefficient (especially with lots of users), but maybe I can try doing some caching magic later on.

Thanks once again. It feels good to take the DM drugs Smile

[eluser]OverZealous[/eluser]
Try this:
Code:
// all two-way associates
$u->associate->where_related('associate', 'id', $u->id)->select('id')->get();
$ids = array();
foreach($u->associate->all as $a) {
    $ids[] = $a->id;
}
// all one-way associates:
$u->associate->where_not_in('id', $ids)->get();

If that works, that's only 2 queries, much more efficient.




Theme © iAndrew 2016 - Forum software by © MyBB