TomTees Posted September 29, 2010 Share Posted September 29, 2010 In a relational database, you "link" tables together. For example, Customer (parent) ---> Product (child) How do you do that with Objects? I have a "User" class and an "AddressBook" class. A User can have zero or more Addresses (in their AddressBook). If I create a User object and an AddressBook object, I need a way to link them up similar to how they are linked up in the database. Make sense?! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/ Share on other sites More sharing options...
trq Posted September 29, 2010 Share Posted September 29, 2010 You would have a method within the User object that creates (and returns) an AddressBook object. Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1116987 Share on other sites More sharing options...
TomTees Posted September 29, 2010 Author Share Posted September 29, 2010 You would have a method within the User object that creates (and returns) an AddressBook object. Would you please make up a simple example and show me some code since I'm a newbie to OOP?! Also, in this scenario, I guess "User", "AddressBook" and "Address" would all be objects?! TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1116991 Share on other sites More sharing options...
trq Posted September 29, 2010 Share Posted September 29, 2010 Keep in mid that this is a VERY simple example of ONE way to do it. if you want multiple address books per User you would need to make the private $_addressBook variable an array and populate that with AddressBook objects but I'll leave that for you to play with. <?php class AddressBook { private $_street; public function __construct($id) { // some code that populates the AddressBook object from a database. } public function getStreet() { return $this->_street; } } class User { private $_addressBook = null; private $_id; private $_name; public function __construct($id) { // some code that populates the User object from a database. } public function getName() { return $this->_name; } public function getAddressBook() { if (is_null($this->_addressBook)) { $this->_addressBook = new AddressBook($this->_id); } return $this->_addressBook; } } $user = new User(22); echo $user->getAddressBook()->getStreet(); Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117001 Share on other sites More sharing options...
trq Posted September 29, 2010 Share Posted September 29, 2010 I should mention that for stuff like this (objects being populated from data within a database), you should likely be using an ORM instead. I understand your only in the learning process, but just though I should point them out. Doctrine is one of the more popular. Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117004 Share on other sites More sharing options...
TomTees Posted September 29, 2010 Author Share Posted September 29, 2010 I should mention that for stuff like this (objects being populated from data within a database), you should likely be using an ORM instead. I understand your only in the learning process, but just though I should point them out. Doctrine is one of the more popular. First, thanks for the sample code!! (I'll look at it tomorrow when my brain is fresh, and see if I can actually understand it?!) Second, since you brought it up, can you please explain what ORM is and why you recommend it? (I know it has something to do with how you lay out your data between the Object and Database Worlds, but how does it relate to things like ActiveRecord and why is one better for me than the other??) Thanks, TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117013 Share on other sites More sharing options...
KevinM1 Posted September 29, 2010 Share Posted September 29, 2010 I should mention that for stuff like this (objects being populated from data within a database), you should likely be using an ORM instead. I understand your only in the learning process, but just though I should point them out. Doctrine is one of the more popular. First, thanks for the sample code!! (I'll look at it tomorrow when my brain is fresh, and see if I can actually understand it?!) Second, since you brought it up, can you please explain what ORM is and why you recommend it? (I know it has something to do with how you lay out your data between the Object and Database Worlds, but how does it relate to things like ActiveRecord and why is one better for me than the other??) Thanks, TomTees ORM stands for Object-Relational Mapping (or, in this case, Mapper). It's a system that maps relational data (e.g., your db data) to objects in code. This saves one from having to manually write and execute CRUD functionality, as these objects will have CRUD implemented within them, which are invoked through their methods. It's much easier to do something like*: $person = ORM::factory('person', 3); // get the person with the id of 3 from the person table $person->name = "Forrest Gump"; $person->age = 43; $person->save(); Than to deal with the db directly. It's an abstraction layer. Also, many ORM systems use the Active Record pattern to do their thing, like the example above shows. *Code is a canned example of Kohana's built-in ORM. Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117142 Share on other sites More sharing options...
TomTees Posted September 29, 2010 Author Share Posted September 29, 2010 I should mention that for stuff like this (objects being populated from data within a database), you should likely be using an ORM instead. I understand your only in the learning process, but just though I should point them out. Doctrine is one of the more popular. First, thanks for the sample code!! (I'll look at it tomorrow when my brain is fresh, and see if I can actually understand it?!) Second, since you brought it up, can you please explain what ORM is and why you recommend it? (I know it has something to do with how you lay out your data between the Object and Database Worlds, but how does it relate to things like ActiveRecord and why is one better for me than the other??) Thanks, TomTees ORM stands for Object-Relational Mapping (or, in this case, Mapper). It's a system that maps relational data (e.g., your db data) to objects in code. This saves one from having to manually write and execute CRUD functionality, as these objects will have CRUD implemented within them, which are invoked through their methods. It's much easier to do something like*: $person = ORM::factory('person', 3); // get the person with the id of 3 from the person table $person->name = "Forrest Gump"; $person->age = 43; $person->save(); Than to deal with the db directly. It's an abstraction layer. Also, many ORM systems use the Active Record pattern to do their thing, like the example above shows. *Code is a canned example of Kohana's built-in ORM. I was under the impression from others that ORM and Active Record and Data Access Objects were 3 separate things?! You make it sound like The 1st two are synonymous? I thought the idea of ORM was to take complex mappings from the Object to the Relational and simplify them? For instance, maybe my "Order" object is really 3 tables in my Database. By contrast, I thought the idea of Active Record (over ORM) was that it was always a 1-to-1 mapping, e.g. User class to User table. TomTees Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117242 Share on other sites More sharing options...
ignace Posted September 29, 2010 Share Posted September 29, 2010 By contrast, I thought the idea of Active Record (over ORM) was that it was always a 1-to-1 mapping, e.g. User class to User table record. I just noticed thorpe already added a simple example, here's mine: class User { function getAddressBook() { $addressBook = new AddressBook(); // ORM/DBAL operations foreach($this->db->fetchAll($sql) as $row) { $addressBook->add(new Address($row)); } return $addressBook; } } $user = UserFactory::makeUser($username, $password); $addressBook = $user->getAddressBook(); Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117287 Share on other sites More sharing options...
KevinM1 Posted September 29, 2010 Share Posted September 29, 2010 Active Record is a design pattern. Many ORMs use the pattern to keep it simple on their end. A 1-1 mapping is easier to create than something that picks and chooses from various tables. See: http://en.wikipedia.org/wiki/Active_record_pattern I believe you can use an ORM on a database view, which would give you the best of both worlds. The ORM would map 1-1 with the view, which could be constructed out of data from multiple tables. Updates could also take place with the view, where the updated view passes the update to the db itself. This is possible with .NET and the Entity Framework, but I'm not sure about PHP ORM solutions and MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/214680-linking-objects-together/#findComment-1117288 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.