Jump to content

Linking Objects together


TomTees

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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();

 

 

Link to comment
Share on other sites

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.

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.