Hi,
I’ve spent some time reading and trying to understand this and I’m finally putting in the call for help.
I have a number of tables that are all related. Because xPDO and some of it’s concepts are new to me, I’m going slow and trying to build the relationships between the tables slowly, while trying to understand what the xPDO code is doing and is capable of doing.
I have built a relationship between two tables. Table1 is the referenced table, with a unique key `group_id`. Table 2 is the referencing table, and also contains a field `group_id` which relates to the first table. There can be many rows in Table2 referencing Table1.
I have been able to successfully load one object from Table1 (Object1) and one object from Table2 (Object2), and add Object2 to Object1 as a related object. This suggests to me that I have the mapping correct, I think.
What I am not able to do is to take a whole collection of Table2’s objects (Collection2) and relate them to Object1. I have tried $Object1->addMany($Collection2) and some variations, trying different aliases, no alias, configuring the composites and aggregates differently and regenerating the classes.
Not all of Table2’s objects would be related to Object1, but some are - those where the group_id field matches. I was expecting/hoping that by calling $Object1->addMany() that any of the objects in the collection that matched would become related objects.
It may just be that I don’t understand the idea of related objects well enough. I can say that after reading as much as I could find, I still don’t quite understand how the composites and aggregates work, or what those terms even mean.
If someone can kick my butt in some general direction, I’d appreciate it.
In simplest terms, an aggregate object is one that simply contains supplemental or summary data (i.e. can exist without the parent) while a composite object contains data that is useless without the parent object (i.e. should not exist without the parent). Objects related by composite relationships would be deleted if you deleted an object that owned the key in that relationship. Think of TV values for a specific document being deleted when a document is deleted in the MODx paradigm.
The addMany function is for actually creating a relationship between a new object or collection of objects and the object the function is called on, assuming they are in a one to many relationship (cardinality="many"). addOne does the same for one to one (cardinality="one"). The result of calling addMany and including existing objects from the database with keys to various object instances other than the one you are calling addMany from, would be to change the keys on those objects. If you save()’d your object, all those objects you added using addMany() that did not already have the same key would be updated with the key from your object.
The getMany function is used to retrieve a collection of existing related objects in a one to many relationship (getOne for one to one) from the database.
It sounds like you are on the right track to understand the related objects, just not how to use them yet.
Thanks OpenGeek. I will have more questions, I’m sure. I appreciate you taking the time to help.
This is a paradigm shift for me. I have no understanding of object orientation or the relational data model in an academic sense. It’s all practical and survival-based knowledge. So in trying to absorb a new way of thinking, mixing the two up into ORB-acious wonder, I often find myself in the creamy middle between web tutorials like "Here’s how to write a SELECT statement" and the academic stuff that assumes knowledge that I just don’t have.
I have put some work into a home-brewed framework which attempted to be able to represent any number of objects and the relationships between them in a static set of tables. Although designing it and trying to code it has been a blast, I have not had the time to bring it to the point where it is consistently easy to implement. Since I am now a hardcore MODx devotee I am going to read up on some of the theory behind your 097 design decisions. I look forward to having my mind and preconceptions blown. West African polyrhythm did it for me in the 90’s, now 10 years later I have xPDO.
-
- 415 Posts
Once again, thank you for your help, I will use [’cardinality’] check on getFKdefinition() to use the correct function: getOne/getMany
-
- 415 Posts
A new question about related objects,
I have a table of document with a many relation to document group.
When I use
$graph=’{"DocumentGroups":{}}’;
$object=$xpdo->getObjectGraph(’SiteContent’,$graph,array("SiteContent.id"=>1));
If the document has a documentGroup set, the result is ok and the related object is loaded in $object, but when document has no documentGroup, a new related Object is created with empty index (’__new0’ in beta rev 75) and default value.
I need related object only if they exists.
Is it normal ? Could I only check if the index of the related object is not null ?
soda:
I agree, it should not be creating empty records for one-to-many relationships here. I’ll take a look at this today to author a test for the issue, see why it’s happening, and fix it.