Part of the EllisLab Network
   
24 of 80
24
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)
Posted: 02 July 2009 11:19 PM   [ Ignore ]   [ # 231 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  887
Joined  10-08-2008

@MeanStudios

You access them probably exactly as you’d expect.  You’ve got it correct for child_role, but parent_role is

$parent_role = $r->parent_role->get();

Saving advanced relationships is explained here, but an example:

$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.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss NEW 1.7

Profile
 
 
Posted: 03 July 2009 12:46 AM   [ Ignore ]   [ # 232 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  421
Joined  11-17-2008

*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!!

 Signature 

- cody

Plugins: AJAX Pagination | AJAX Comments
Extensions: jQuery Quick Save (updated) | Goto Latest Comment
FieldTypes: MS Img Saver
MeanStudios.com

Profile
 
 
Posted: 04 July 2009 01:07 PM   [ Ignore ]   [ # 233 ]  
Summer Student
Total Posts:  14
Joined  02-27-2009

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:

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

“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:

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?

Profile
 
 
Posted: 04 July 2009 01:20 PM   [ Ignore ]   [ # 234 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  887
Joined  10-08-2008

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.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss NEW 1.7

Profile
 
 
Posted: 04 July 2009 01:22 PM   [ Ignore ]   [ # 235 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  887
Joined  10-08-2008

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.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss NEW 1.7

Profile
 
 
Posted: 04 July 2009 01:26 PM   [ Ignore ]   [ # 236 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  887
Joined  10-08-2008

Ok, here’s an updated beta version with the fix in place as recommended by cberk.

File Attachments
DMZ-1.3.3-beta2.zip  (File Size: 30KB - Downloads: 10)
 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss NEW 1.7

Profile
 
 
Posted: 06 July 2009 08:52 PM   [ Ignore ]   [ # 237 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  421
Joined  11-17-2008

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

 Signature 

- cody

Plugins: AJAX Pagination | AJAX Comments
Extensions: jQuery Quick Save (updated) | Goto Latest Comment
FieldTypes: MS Img Saver
MeanStudios.com

Profile
 
 
Posted: 07 July 2009 04:54 PM   [ Ignore ]   [ # 238 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  887
Joined  10-08-2008

If client_id is stored on the users table, then I would do it like this:

$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

$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.

 Signature 

Phil DeJarnett
  OverZealous Creations, LLC
  ZESTYJOBS - Job Management Powered by CI/DMZ

  DataMapper OverZealous Edition: Advanced ORM for CodeIgniter Discuss NEW 1.7

Profile
 
 
Posted: 07 July 2009 05:56 PM   [ Ignore ]   [ # 239 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  421
Joined  11-17-2008

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!

 Signature 

- cody

Plugins: AJAX Pagination | AJAX Comments
Extensions: jQuery Quick Save (updated) | Goto Latest Comment
FieldTypes: MS Img Saver
MeanStudios.com

Profile
 
 
Posted: 07 July 2009 06:11 PM   [ Ignore ]   [ # 240 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  421
Joined  11-17-2008

Now for another question regarding turning an object into an array heh.

Now that I’m grabbing those Subscriber Groups, I need to turn the results into an array so I can pass it to a smarty template.  I tried passing $sg->all and then use {section} to loop through the results but smarty is outputting it wrong. I’m assigning it like so:

$this->mysmarty->assign('subscriber_groups', $sg->all);

My smarty code looks like this:

{section name=i loop=$subscriber_groups}
    
<input type="radio" name="subscriber_group_id" value="{$subscriber_groups[i]->id}" /> {$subscriber_groups[i]->name}<br />
{/section}

And the output is:

<input name="subscriber_group_id" value="" type="radio"> <br />
<
input name="subscriber_group_id" value="1" type="radio"> Test Subscriber Group<br />
<
input name="subscriber_group_id" value="2" type="radio"> Another Test Group<br />

It should be outputting 3 records:
ID: 1 - Name: Test Subscriber Group
ID: 2 - Name: Another Test Group
ID: 3 - Name: Third Test Group

As you can see it is outputting 3 radio buttons but the first one is blank.  This might be a problem with smarty?  That’s why I decided to convert the object into an array and then pass it to the smarty template that way.  I tried passing $sg->_to_array() but that only takes the first record and turns it into an array, which is not what I wanted.  Is there another hidden function that will turn the object values into an associative array?  Or should I do something like:

$subscriber_groups = array();
foreach (
$sg->all as $subscriber_group)
{
  $subscriber_groups[]
= $subscriber_group->_to_array();
}

Would that be the more efficient way of doing it?

 Signature 

- cody

Plugins: AJAX Pagination | AJAX Comments
Extensions: jQuery Quick Save (updated) | Goto Latest Comment
FieldTypes: MS Img Saver
MeanStudios.com

Profile
 
 
   
24 of 80
24
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120605 Total Logged-in Users: 50
Total Topics: 126643 Total Anonymous Users: 2
Total Replies: 665706 Total Guests: 488
Total Posts: 792349    
Members ( View Memberlist )
Newest Members:  PietervanDambell143paololukDarleneChadbourneRashadSargIvar89lhumphris18aHonschowmt