Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]MeanStudios[/eluser]
[quote author="OverZealous.com" date="1246525010"]You got it! I didn't have a chance to respond earlier, but that's exactly how I would have done it.

I don't know how often you are editing / looking up these relationships, but this might end up being query-intensive. Luckily you can use include_related to get items from the 'tree' 2 at a time.

Good Luck![/quote]
Yay Smile - I got it right haha.
Now for another question (don't shoot me heh). How do I access those or save data to them?
Code:
$r = new Role();
$r->where('id', '2')->get();
//Then to get the child roles or parent role of '2' I just do?
$child_roles = $r->child_role->get(); //<-- Could be 0 or more rows.
$parent_role = $r->child_role->get(); //<-- Should only be 0 or 1 row.

I'm not sure about the saving though Undecided. How would I make a role of child of another?

Thanks!!

[eluser]OverZealous[/eluser]
@MeanStudios

You access them probably exactly as you'd expect. You've got it correct for child_role, but parent_role is
Code:
$parent_role = $r->parent_role->get();

Saving advanced relationships is explained here, but an example:
Code:
$r->save_parent_role($parent_role);
// OR
$r->save($parent_role, 'parent_role');
// OR
$r->save(array(
    // save as parent
    'parent_role' => $parent_role,
    // save as child
    'child_role' => array($child_role1, $child_role2), // $child_role->all would also work
    $something_else // normal relationship
));

I recommend using the third option unless you are specifically saving a single relationship.

[eluser]MeanStudios[/eluser]
*DING*
The light bulb just went on Smile. Thank you! Just took me some extra time of reading it over and over again till it clicked heh.

Yay for Phil!!

[eluser]cberk[/eluser]
This could definitely be my error, rather than a bug, but I'm having some trouble with the _count_related function in DMZ.

Here's what I'm trying to do: I'm creating two new objects, one after the other. The first object is a required relationship for the second object. I thought that instead of checking both $object->save() functions for success/failure, I would take a shortcut and just use the first object in the second object's save() function without checking to make sure it exists first. But when the first object fails to save, the second object is still passing validation and saving to the database, without the required relationship. Here's a truncated version of my code:

Code:
// Record has_one Citation (required)

$citation = new Citation();
$citation->ref = 'some string';
$citation->save();

$record = new Record();
... // Record fields
$record->save($citation); // Unfortunately, record saves, even if ! $citation->exists()

The _count_related function description says:
Quote:"Returns the number of related items in the database and in the related object."
The problem is, it counts related objects even if they don't exist in the database, so if you accidentally pass in an empty object, it will cause your validation rules to return TRUE, even though the relationship cannot be saved.

Here's an excerpt from _count_related which shows that it is only checking the object's model name, not whether or not it exists:

Code:
if (strtolower(get_class($obj)) == $class)
{
    $count++;
}

Perhaps I should be checking all my objects before saving, but I think as a precaution that _count_related should only count objects that exist. Or am I missing something?

[eluser]OverZealous[/eluser]
This is a bug, cberk. I found it when researching a previous issue. I haven't had time post the updated version of DMZ, and since this bug has existed since the original DM, I figured it must not be causing too much of a problem.

It's fixed on the trunk, but I also haven't tested the fix yet. I also have fixed a DMZ-only bug that is related.

Update: removed previous beta, see below

Vacation Notice
I won't be available much for the next week, as I will be on vacation. Please feel free to respond to other user's questions while I am away. I'll do my best to answer questions when I have time.

I won't be releasing another full update until after I return.

[eluser]OverZealous[/eluser]
Update
Hmm... I just realized that I didn't read your message completely, cberk. My updates don't fix that bug!

They focus more on correctly counting the number of unique items. I'll have to look into adding an exists() check.

[eluser]OverZealous[/eluser]
Ok, here's an updated beta version with the fix in place as recommended by cberk.

[eluser]MeanStudios[/eluser]
Got another n00b question if anyone would like to answer.

I've got 3 tables:
- clients <-- Has many user
- users <-- Has one client and many subscriber_group
- subscriber_groups <-- Has one user

How would I run a query that grabs all of the subscriber_groups for the client of the user currently logged in?

So I know the user ID and I need to get all the subscriber_groups attached to all the users under the client that is attached to the user I know the ID of.

Confusing enough? heh

[eluser]OverZealous[/eluser]
If client_id is stored on the users table, then I would do it like this:
Code:
$user = ... // look up user
$sg = new Subscriber_Group()
$sg->where_related_user('client_id', $user->client_id)->get();

If not, then do something like this
Code:
$user->client->select('id')->get();
$all_users = new User();
$all_users->where_related($user->client)->select('id')->get();
$au = array();
foreach($all_users->all as $u) {
    $au[] = $u->id;
}
$sg = new Subscriber_Group();
$sg->where_in_related_user('id', $au)->get();

The first is preferable, but depends on your DB layout.

[eluser]MeanStudios[/eluser]
Yup, it's definitely being held in the users table so I will be using your first example.
Man, I'm realizing that I am still so very new to this datamapper concept. Everytime I think I have handle on it, I learn that I've only grasped the tip of the iceberg heh.

Thanks for the save again!




Theme © iAndrew 2016 - Forum software by © MyBB