paolo@bimodesign.com | +34 608 61 64 10

Framework

        

Zend2-Doctrine 2 and MongoDb (Upd/Del)

This is the fourth and last posts about Zend 2, Doctrine 2 and MongoDB installation and CRUD examples. In this article I'll show update and delete document. The next posts about these tecnologies will be an example of a blog's structure.
These examples based on these my previous posts:

Installation and Configuration
CRUD - Insert/create document
CRUD - Select document

Of course you can find all the methods and all the documentation at the Doctrine Oficial link
I created a edit and delete action into the user controller to make my tests.

The Edit action use the Doctrine-MongoDb function called FindAndUpdate.

    ...
    ... The header code of controller
    ...
	
    public function editAction()
    {
        $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
        $queryBuilder = $dm->createQueryBuilder('User\Document\User');
        $queryUpdate = $queryBuilder->findAndUpdate()
                       ->field('name')->equals('Luca')
                       ->field('age')->set('65')
                       ->field('started')->set(new \MongoDate())
                       ->getQuery()
                       ->execute();
        
        
        return new ViewModel();
    }

Before executing this action, the collection User had these data

> db.user.find().pretty();
{ "_id" : ObjectId("55b78999e0cf201b19f1696b"), "name" : "Paolo" }
{ "_id" : ObjectId("55b78c32e0cf203019f1696b"), "name" : "Luca" }
{ "_id" : ObjectId("55b9f561e0cf205a128b4567"), "name" : "clementina" }
{ "_id" : ObjectId("55b9f624e0cf2062128b4567"), "name" : "giulio" }
{
	"_id" : ObjectId("55b9f676e0cf20c9138b4567"),
	"name" : "paolo",
	"surname" : "bindi",
	"age" : "45"
}
{
	"_id" : ObjectId("55ba1e0be0cf20ed068b4567"),
	"name" : "Paolo",
	"surname" : "bindi",
	"age" : "45"
}

After

> db.user.find().pretty();
{ "_id" : ObjectId("55b78999e0cf201b19f1696b"), "name" : "Paolo" }
{ "_id" : ObjectId("55b9f561e0cf205a128b4567"), "name" : "clementina" }
{ "_id" : ObjectId("55b9f624e0cf2062128b4567"), "name" : "giulio" }
{
	"_id" : ObjectId("55b9f676e0cf20c9138b4567"),
	"name" : "paolo",
	"surname" : "bindi",
	"age" : "45"
}
{
	"_id" : ObjectId("55ba1e0be0cf20ed068b4567"),
	"name" : "Paolo",
	"surname" : "bindi",
	"age" : "45"
}
{
	"_id" : ObjectId("55b78c32e0cf203019f1696b"),
	"name" : "Luca",
	"age" : "65",
	"started" : ISODate("2015-08-01T14:02:20.977Z")
}

You can notice that the field "age" and "started" aren't into the previous structure.

The Delete action use the Doctrine-MongoDb function called findAndRemove.

    public function deleteAction()
    {
        $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
        $queryBuilder = $dm->createQueryBuilder('User\Document\User');
        
        $queryUpdate = $queryBuilder->findAndRemove()
                    ->field('name')->equals('giulio')
                    ->getQuery()
                    ->execute();
        
        return new ViewModel();
    }    

Note: You can download the entire proyect from my github account.