Jump to content

Composite key for object property


Recommended Posts

I have a hard time making sense of the question.

 

You want to identify an object by multiple properties? In what context? Looking up an entity by a key implies that there's a collection of entities (like a relation in the relational model). What is your collection?

Link to comment
Share on other sites

You want to identify an object by multiple properties? In what context? Looking up an entity by a key implies that there's a collection of entities (like a relation in the relational model). What is your collection?

 

I first thought about this based a previous post where I wanted to identify based on a min and max range.

 

It seems more interesting, however, when applied with relational databases.  I don't think it is too uncommon to make a collection of entities using some single primary key as the object name and other columns as the object's properties.  I then can identify a member in the collection by using $obj->somePrimaryKey (or maybe some derivative of the PK).  But, how can the member be selected if that primary key was a composite?

 

PS.  Some of my OOP terminology might be off as I never new the term "collection" before now.

Link to comment
Share on other sites

I then can identify a member in the collection by using $obj->somePrimaryKey (or maybe some derivative of the PK).  But, how can the member be selected if that primary key was a composite?

The same (basic) way the database does it?

if ($obj->somePrimaryKey1 == $primaryValue1 && $obj->somePrimaryKey2 == $primaryValue2) {
But this isn't making any sense to me either. The index (composite key) in the database is all about searching - are you trying to do a search in code?

 

Oh, and "collection" is less an OOP thing and more a term that fits the usage well. Like in the real world there's rock collections and car collections and bug collections and so on, so sometimes people use it in programming too. For example, .NET uses that word with many of its classes.

Link to comment
Share on other sites

But this isn't making any sense to me either. The index (composite key) in the database is all about searching - are you trying to do a search in code?

 

Well if it doesn't make sense to you and Jacques, then it probably doesn't make sense to me either!

 

Not really a search.  Say I have $obj and $obj has properties "somePrimaryKey1", "somePrimaryKey2", and "somePrimaryKey3", I can access them by using $obj->somePrimaryKey1, etc.  Turns out $obj wasn't created from a class, but was created from a database and PHP script.  Maybe this is why it doesn't make sense.  Is doing this unheard of?

Link to comment
Share on other sites

Say I have $obj and $obj has properties "somePrimaryKey1", "somePrimaryKey2", and "somePrimaryKey3", I can access them by using $obj->somePrimaryKey1, etc.  Turns out $obj wasn't created from a class, but was created from a database and PHP script.  Maybe this is why it doesn't make sense.  Is doing this unheard of?

As maxxd pointed out, it sounds like regular Active Record: you have a class with properties corresponding to all the columns in the table it represents. So besides those three somePrimaryKey* fields you'd have stuff for everything else in the table. With that then, short of indexing and searching, a composite key isn't really anything special - it's just the knowledge that it takes more than one column/property to uniquely identify a record. You might not even have to do anything about it in the code.
Link to comment
Share on other sites

With that then, short of indexing and searching, a composite key isn't really anything special - it's just the knowledge that it takes more than one column/property to uniquely identify a record. You might not even have to do anything about it in the code.

 

I was going to say something needs to be done in code.  Maybe combining the composite key strings as a single unique property name.

 

But then I read more on maxsd's link.  ORM?  Yea, I've heard of them, and they are on my list of things to learn up on.....

 

If my question was posted before ORM's existed, would it make more sense?

Link to comment
Share on other sites

If my question was posted before ORM's existed, would it make more sense?

 

The point is that an object property itself is just a dumb piece of data. It doesn't do anything. When you say that it “uniquely identifies an object”, that's just your human understanding of the context. PHP doesn't know anything about this.

 

So if you want to perform any kind of lookup operations based on object properties, you or somebody else needs to implement a data structure which supports this. It can be as simple as a (nested) associative array or as complex as a full-blown ORM.

Link to comment
Share on other sites

Speaking of doing things,

I was going to say something needs to be done in code.  Maybe combining the composite key strings as a single unique property name.

For what purpose? How are you going to use it? When will this value be more useful than having just the individual components?
Link to comment
Share on other sites

Speaking of doing things,

For what purpose? How are you going to use it? When will this value be more useful than having just the individual components?

 

I have the following array of objects represented by the below JSON.
 
[
   {id: 123, type: "aggr", duration: 10, offset: 30, timestamp: 444, foo: "bar"},
   {id: 223, type: "aggr", duration: 20, offset: 40, timestamp: 333, foo: "bar"},
   {id: 323, type: "aggr", duration: 10, offset: 30, timestamp: 222, foo: "bar"},
   {id: 523, type: "aggr", duration: 20, offset: 40, timestamp: 444, foo: "bar"},
   ......
   {id: 623, type: "aggr", duration: 20, offset: 30, timestamp: 444, foo: "bar"},
   {id: 723, type: "aggr", duration: 10, offset: 30, timestamp: 666, foo: "bar"}
]
 
I need to group them based on common duration and offset pairs.  For instance, element 0, 3, and N-1 all share this pair.
 
Also, I will take different action based on the timestamp value of each object with a given duration/offset pair.
 
I will have built this array of objects in some other code, and could if I want restructure it.
 
Instead of an array of objects, I was thinking of an object who's property names are the duration/offset pair, and their values are arrays which include id, type, etc objects.  But a property name is only a single string so I thought maybe I could concatenate the two pairs into one.  Seems like a hack, and it would been haven nice if the property name was identified by two strings (duration and offset).
Link to comment
Share on other sites

You could build a key like "10,30" if you wanted. You're just using it to determine uniqueness while processing data - that's not quite the same as trying to generically represent a composite key in code.

Myself, since there's only two pieces to the key I might go for nested arrays.

array(10 => array(30 => array(/* 0, 3, N-1 */)))
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.