Skip to content

DAO, Data Access Object

Daniel Kobler edited this page Jul 18, 2013 · 3 revisions

The DAO engine is used to populate the collection of objects with the given data.

The objects present in the collections are PoPo (Plain old PHP objects) with properties and methods. The DAO engine uses the reflection to populate the object by trying to access the public member properties or the setter and getter if available. On top of this the values are casted to the right type.

All those operations are performed by using the tagging.

Table of Contents

Setter/Getter how DAO manipulates object collections

The main idea behind the DAO is to be able to easily access object's data. The DAO engine offers a setValue and getValue method to access an object property value. Those two method try to access the property by its setter or getter and if none has been found and the property is declared as public it access to the property directly. On top of this the setValue and getValue methods cast the value regarding the type that has been defined using the @var tag.

Here is a short example of how it works:

    class User {
    
        /**
         * @var string
         */
        protected $username;
    
        /**
         * Return the username
         * @return string
         */
        public function getUsername() {
            return $this->username;
        }
        
        /**
         * Set the user name
         * @param string $username The username
         */
        public function setUsername($username){
            $this->username = $username;
        }
    }
    
    // The DAO object is created 
    $userDao = Noee_Dao::factory('User');
    
    $user = new User();
    $userDao->setValue($user, 'username', 'myusername');
    
    echo $userDao->getValue($user, 'username);
    // Is in this case equals to
    echo $user->getUsername();

Populate

Often it is required to populate an object with values coming from an other object (a "copy"), from array or from JSON. This is where DAO start to be interesting !

The DAO object offers different methods to accomplish this task.

  • populate Populates the object using one of the three methods described below regarding the given data.
  • populateFromObject Populates the object from the given object. This is a copy style population.
  • populateFromArray Populates the object from the given array.
  • populateFromJson Populates the object from the given JSON string.
Here is some examples:
    $user = new User();
    $userDao->populateFromObject($refUserObj, $user);
    $user = new User();
    $userDao->populateFromJson('{"username":"myusername"}', $user);
    $user = new User();
    $userDao->populateFromArray(Array("username" => "myusername"), $user);

If the second parameter (here the $user) is not defined, a new object is automatically created. This is very useful in collection population as we will see in the next section.

    $user = $userDao->populateFromArray(Array("username" => "myusername"));

Collection manipulation

The DAO real power comes from the collection data manipulation. The DAO can browse or recreate the tree objects recursively by populating each objects with the given values.

Here is an example of a user having invoices which have invoice items:

    class User {
    
        /**
         * @var string
         */
        protected $username;
        
        /**
         * @var Invoice[]
         */
        protected $invoices = array();
        
    
        /**
         * Return the username
         * @return string
         */
        public function getUsername() {
            return $this->username;
        }
        
        /**
         * Set the user name
         * @param string $username The username
         */
        public function setUsername($username){
            $this->username = $username;
        }
        
        /**
         * Set the invoices
         * @param Invoice[] $invoices The invoices
         */
        public function setInvoices($invoices) {
            $this->invoices = $invoices;
        }
        
        /**
         * Return the user invoices
         * @return Invoice[]
         */
        public function getInvoices() {
            return $this->invoices;
        }
    }
    
    
    class Invoice {
        
        /**
         * @var date
         */
        protected $creationDate;
        
        /**
         * @var InvoiceItem[]
         */
        protected $items = array();
        
        /**
         * Set the invoice creation date
         * @param date $creationDate The invoice creation date
         */
        public function setCreationDate($creationDate) {
            $this->creationDate = $creationDate;
        }
            
        /**
         * Return the invoice creation date
         * @return date
         */
        public function getCreationDate() {
            return $this->creationDate;
        }
        
        
        /**
         * Set the invoice items
         * @param InvoiceItem $invoiceItems The invoice items
         */
        public function setInvoiceItems($invoiceItems) {
            $this->invoiceItems = $invoiceItems;
        }
            
        /**
         * Return the invoice items
         * @return InvoiceItem
         */
        public function getInvoiceItems() {
            return $this->invoiceItems;
        }    
    }
    
    
    class InvoiceItem {
        
        /**
         * @var string
         */
        public $description;
        
        /**
         * @var int
         */
        public $quantity = 0;
        
        /**
         * @var float
         */
        public $price = 0;
    }
    // Here is how we can easily populate the entire collection from the given JSON data
    $userDao = Noee_Dao::factory('User');
    $user = $userDao->populateFromJson('{
        "username":"myusername",
        "invoices": [
            {
                "creationDate":"2013-01-01",
                "items": [
                    {    
                        "description":"My product 1",
                        "quantity":10,
                        "price": 19.99
                    },
                    {    
                        "description":"My product 2",
                        "quantity":5,
                        "price": 35.99
                    }
                ]
            }
        ]
    }');

Export collection data

Now that we have seen how easy it is to populate a collection of objects with data it is interesting to see that we can also export those data to array or JSON with ease.

    print_r($userDao->toArray($user));
    print($userDao->toJson($user));

Clone this wiki locally