NotionCommotion Posted June 2, 2016 Share Posted June 2, 2016 In SQL, two (or more) keys can be used to positively identify a record. How can this concept be used regarding an object's property name? Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/ Share on other sites More sharing options...
Jacques1 Posted June 3, 2016 Share Posted June 3, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533380 Share on other sites More sharing options...
NotionCommotion Posted June 3, 2016 Author Share Posted June 3, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533387 Share on other sites More sharing options...
requinix Posted June 3, 2016 Share Posted June 3, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533388 Share on other sites More sharing options...
NotionCommotion Posted June 3, 2016 Author Share Posted June 3, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533391 Share on other sites More sharing options...
maxxd Posted June 3, 2016 Share Posted June 3, 2016 Not entirely sure I'm following what you're asking (been a long week, sorry) but this article on Active Record and Data Mapper patterns may be a good read. Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533393 Share on other sites More sharing options...
requinix Posted June 4, 2016 Share Posted June 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533408 Share on other sites More sharing options...
NotionCommotion Posted June 4, 2016 Author Share Posted June 4, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533411 Share on other sites More sharing options...
Jacques1 Posted June 4, 2016 Share Posted June 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533412 Share on other sites More sharing options...
requinix Posted June 4, 2016 Share Posted June 4, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533413 Share on other sites More sharing options...
NotionCommotion Posted June 10, 2016 Author Share Posted June 10, 2016 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). Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533564 Share on other sites More sharing options...
requinix Posted June 10, 2016 Share Posted June 10, 2016 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 */))) Quote Link to comment https://forums.phpfreaks.com/topic/301288-composite-key-for-object-property/#findComment-1533567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.