Tag Archives: sonata admin

Using Sonata admin bundle with multiple connections

Last month I was participating in developing solution which provides reporting features using Symfony that deals with data partitioned over multiple MySQL servers. Data was sharded among two reporting databases that have identical structure, each database store trading history for a separate company branch/trading system.
So basically, there is a bundle that deals with multiple connections – thus different entity managers- dynamically. Handling those connections in Symfony is straight forward, but for sonata admin bundle, it needed some work to get it work.

Project Structure

The project contains bundle called WebitReportingBundle which have the entities related to reporting database, I mainly use entity called MT4Trades for this post (it holds trades information performed by clients)
This bundle uses two entity managers, depending on the application context, here is portion of config.yml containing entity managers/connection mappings:

    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    FOSUserBundle: ~
                    ApplicationSonataUserBundle: ~
                    SonataUserBundle: ~
                    AppBundle: ~                    
            reporting_real:
                connection: reporting_real
                mappings:
                    WebitReportingBundle: ~
            reporting_real2:
                connection: reporting_real2
                mappings:
                    WebitReportingBundle: ~

In the config file above, there are two extra entity managers (real_reporting & real_reporting2), each handles connection to separate database.

In Symfony, the entity manager can be specified with doctrine repository simply as following:

<?php

namespace Webit\ReportingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller {
    
    public function IndexAction(Request $request) {
        //retrieve trades from server 1
        $obj_server1 = $this->getDoctrine()
            ->getRepository('WebitReportingBundle:MT4Trades', 'reporting_real')
            ->findOneBy(['ticket'=>42342]);
                
        //retrieve trades from server 2
        $obj_server1 = $this->getDoctrine()
            ->getRepository('WebitReportingBundle:MT4Trades', 'reporting_real2')
            ->findOneBy(['ticket'=>123123]);
    }
}

but when I wanted to do the same on sonata admin module, there was no easy way to handle that.

How Sonata Admin Bundle handle multiple connections

When you create sonata admin file for certain entity with “SonataDoctrineORMAdminBundle“, “Admin” class will determine the entity manager to use by calling “getManagerForClass()” method in the associated “ModelManager” object.

As in the following code, which is taken from ModelManager class inside sonataDoctrineORMAdminBundle:

<?php 
namespace Sonata\DoctrineORMAdminBundle\Model;

class ModelManager implements ModelManagerInterface, LockInterface
{
    public function getEntityManager($class)
    {
        if (is_object($class)) {
            $class = get_class($class);
        }

        if (!isset($this->cache[$class])) {
            //detect entity manager based on class name automatically
            $em = $this->registry->getManagerForClass($class);

            if (!$em) {
                throw new \RuntimeException(sprintf('No entity manager defined for class %s', $class));
            }

            $this->cache[$class] = $em;
        }

        return $this->cache[$class];
    }
}

However, this method will be an issue for entities when dealing with sharded databases, so in my case, the first matching entity manager will be always selected – according to mappings defined in config.yml – and there is no direct way to specify the preferred entity manager for the admin class.

Solution

In order to work around this problem, I performed the following:

  1. Define custom model manager extending the ModelManager class, and override methods “getEntityManager” and “createQuery” to use specific entity manager if it is defined in the class, as following:
    <?php 
    namespace Webit\ReportingBundle\Model;
    
    use Sonata\DoctrineORMAdminBundle\Model\ModelManager as BaseModelManager;
    use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
    
    class CustomModelManager extends BaseModelManager
    {
        /* @var $emName stores the preferred entity manager name */
        protected $emName;
        
        /**
         * set preferred entity manager name to be used in ModelManager
         * @param string $name
         */
        public function setEntityManagerName($name){        
            $this->emName = $name;
        }
    
        /**
         * {@inheritdoc}
         */    
        public function createQuery($class, $alias = 'o') {
            //adding second parameter to getRepository method specifying entity manager name
            $repository = $this->getEntityManager($class)
                               ->getRepository($class,$this->emName);
    
            return new ProxyQuery($repository->createQueryBuilder($alias));
        }
        
        /**
         * {@inheritdoc}
         */
        public function getEntityManager($class) {        
            if (is_object($class)) {
                $class = get_class($class);
            }                
            if (isset($this->cache[$class]) === false) {
                //return fixed value if preferred entity manager name specified
                if (isset($this->emName) === true) {
                    $this->cache[$class] = $this->registry->getEntityManager($this->emName);
                } else {
                    $this->cache[$class] = parent::getEntityManager($class);
                }
            }
    
            return $this->cache[$class];
        }
    }
    
  2. Add new model manager as a service and associate it with the admin service entries of the entity:
    services:        
        reporting.model_manager:
             class: Webit\ReportingBundle\Model\CustomModelManager
             arguments:
                 - '@doctrine'
    
        reporting.admin.trades:
            class: Webit\ReportingBundle\Admin\MT4TradesAdmin
            tags:
               - { name: sonata.admin, manager_type: orm, group: webit.admin.group.reporting, label: "Trades (server 1)", pager_type: "simple" }
            arguments: [null,Webit\ReportingBundle\Entity\MT4Trades, WebitReportingBundle:Admin\MT4Trades]        
            calls:
               - [setModelManager, ['@reporting.model_manager'] ]  
               
    
        reporting.admin.trades2:
            class: Webit\ReportingBundle\Admin\MT4Trades2Admin
            tags:
               - { name: sonata.admin, manager_type: orm, group: webit.admin.group.reporting, label: "Trades (server 2)", pager_type: "simple" }
            arguments: [null,Webit\ReportingBundle\Entity\MT4Trades, WebitReportingBundle:Admin\MT4Trades]        
            calls:
               - [setModelManager, ['@reporting.model_manager'] ]
               
                       
    Here I am having two admins for the same entity, each admin shall use different connection.
  3. Define the entity manager name inside the admin classes by overriding setModelManager method, as following:
    <?php
    
    namespace Webit\ReportingBundle\Admin;
    
    use Sonata\AdminBundle\Admin\Admin;
    
    /**
     * admin for MT4Trades (Server 1)
     */
    class MT4TradesAdmin extends Admin{
    
        protected $emName = 'reporting_real';
        protected $baseRouteName = 'admin_webit_reporting_mt4trades';    
        protected $baseRoutePattern = 'admin-reporting-mt4trades-server';
         
        public function setModelManager(\Sonata\AdminBundle\Model\ModelManagerInterface $modelManager) {
            parent::setModelManager($modelManager);
            //override setModelManager to specify the preferred entity manager for this admin class
            $modelManager->setEntityManagerName($this->emName); 
        } 
        
        //remaining admin class methods...
    }
       
        
       

    and here the other admin class that is connecting to the second database:
    <?php
    
    namespace Webit\ReportingBundle\Admin;
    
    /**
     * admin for MT4Trades (Server 2)
     */
    class MT4Trades2Admin extends MT4TradesAdmin{
    
        protected $emName = 'reporting_real2'; //second entity manager will be used
        
        //specify route name and pattern to get two admin classes work with same entity
        protected $baseRouteName = 'admin_webit_reporting_mt4trades2';    
        protected $baseRoutePattern = 'admin-reporting-mt4trades-server2';
         
        //...
    }

    I extended second admin class from the first, so they have identical configurations/logic, with different connection used.

Conclusion

Now, I have two identical admin classes for the same entity, each using different entity manager correctly. and if additional server is deployed in the future, I just have to define new admin classes -along with new connection/entity manager – with placing the correct $emName value.

multi-connection-sonata

Applying version stamp to Symfony & SonataAdmin

I spend a lot of time in my job in developing and maintaining backoffice systems to process some workflow logic for Forex portals. Our systems are developed using Symfony2 and depend highly on SonataAdminBundle with deep customization to impose business rules for Forex companies.

Recently some data inconsistency appeared in a system of one of our clients, and after digging into logs, I found that the cause of the problem is two users processing same user application in almost same time range, and that caused one edit operation to override the other one, and many other undesired consequences occurs after that.

so in order to fix this issue, and prevent it from happening again, I worked on adding “Version Stamps” to my entity to maintain offline data consistency within the application, and I would like to share here what I learned.

 

Version Stamps and offline consistency

Version Stamps is a field that changes every time a writing operation is performed on the data, it is used to ensure that no one else has changed data of that row before applying your modification.
There is several ways to implement version stamps, and the simplest way is an integer value which is noted on the read, its value is compared to the submitted data before write, and once validated, the write operation take place with increasing version stamp value.
Let’s say there is a form bind to an entity in Symfony application, version stamp column will be present in the entity and added as a hidden field in the form, once submitted version stamp value submitted will be compared to the one in the database currently, to ensure that no other edit is performed on that entity -in the time between displaying your form initially and submitting it-, if the condition fails, the update operation will be rejected, thus by adding error via constraint.

Implementation in SonataAdmin

The implementation of this concept is quite simple as following:
In the desired entity, I applied those modifications:

  1. Define new field to hold stamp value.
  2. Mark the field to be a version stamp, using doctrine @Version annotation; this will cause doctrine to update versionStamp field every time the object is persisted to the database
<?php

namespace Webit\ForexCoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * RealProfile
 *
 * @ORM\Table(name="forex_real_profile")
 * @ORM\Entity(repositoryClass="Webit\ForexCoreBundle\Repository\RealProfileRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class RealProfile
{
    
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
    
    /**
     * @var integer
     *
     * @ORM\Version 
     * @ORM\Column(name="version_stamp", type="integer")
     */
    private $versionStamp;
    
    /* Other columns, getters, setters here */

    
}

In Admin class, the following is added:

  1. In configureFormFields() method, the version stamp is added as hidden field, also I set mapped option to false, to prevent persisting its value along the form. version stamp value must be modified only via PreUpdate() method inside the entity.
    <?php
    
    namespace Webit\ForexCoreBundle\Admin;
    
    use Sonata\AdminBundle\Admin\Admin;
    use Sonata\AdminBundle\Datagrid\ListMapper;
    use Sonata\AdminBundle\Datagrid\DatagridMapper;
    use Webit\ForexCoreBundle\Entity\RealProfile;
    
    class RealAccountsAdmin extends Admin
    {
        protected function configureFormFields(\Sonata\AdminBundle\Form\FormMapper $formMapper)
        {
            $formMapper->add('versionStamp','hidden',array('attr'=>array("hidden" => true, 'mapped'=>false)))
            //other fields and groups...
        }
    }
  2. Here is the important point, which is validating version_stamp posted from the form against the one that is already saved in the database. There is two methods to apply that, one if by using doctrine locking mechanism, and the other is using sonata inline validation to add extra validation layer by implementing validate() method in order to apply additional validation layer.

    option 1:

    class RealAccountsAdmin extends Admin
    {
        /**
         * {@inheritdoc}
         */    
        public function getObject($id)
        {
            $uniqid = $this->getRequest()->query->get('uniqid');
            $formData = $this->getRequest()->request->get($uniqid);        
            
            $object = $this->getModelManager()->find($this->getClass(), 
                    $id, 
                    \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE, 
                    $formData['versionStamp']);
            
            foreach ($this->getExtensions() as $extension) {
                $extension->alterObject($this, $object);
            }
    
            return $object;
        }
    
        /**
         * {@inheritdoc}
         */      
        public function update($object) {
            try{
                parent::update($object);
            }catch(\Doctrine\ORM\OptimisticLockException $e) {
                $this->getConfigurationPool()->getContainer()->get('session')
                        ->getFlashBag()->add('sonata_flash_error', 'someone modified the object in between');
            }
        }

    This approach will take advantage of doctrine locking support. Here is brief explanation:

    • I have overridden getObject() method in admin class, to add two extra parameters for getModelManager()->find() method;
      third parameter indicates locking type, I used here LockMode::PESSIMISTIC_WRITE
      fourth parameter represents the expected version stamp value -to compare with database value before flushing.
    • I have overridden update($object) method so I catch OptimisticLockException exception and add error flash message to handle it

    option 2:
    In this approach, I used sonata inline validation to detect the form as invalid before even trying to persist and flush the object to the database:

        /**
         * {@inheritdoc}
         */
        public function validate(\Sonata\AdminBundle\Validator\ErrorElement $errorElement, $object) { 
            //get all submitted data (with non-mapped fields)
            $uniqid = $this->getRequest()->query->get('uniqid');
            $formData = $this->getRequest()->request->get($uniqid);
            $submitted_version_stamp = $formData['versionStamp'];        
            
            $em = $this->getConfigurationPool()
                  ->getContainer()->get('doctrine')->getManager();
            
            //get up-to-date version stamp value from the database
            $class_name = get_class($object);        
            $q = $em->createQuery("select partial o.{id,versionStamp} from $class_name o"
                                    . " where o.id=".$object->getId());        
            $saved_data = $q->getArrayResult();        
            $saved_version_stamp = $saved_data[0]['versionStamp'];
            
            //compare version stamps and add violation in case it didn't match
            if($saved_version_stamp != $submitted_version_stamp){
                $errorElement->addViolation('Record data seems outdated, probably someone else modified it, please refresh and try again.')->end();
            }
        }
        
    Here is more details about the operations performed inside this method:

    • To get versionStamp value submitted via form inside that method, I used:
      $uniqid = $this->getRequest()->query->get('uniqid');
      $formData = $this->getRequest()->request->get($uniqid);
      $submitted_version_stamp = $formData['versionStamp'];
    • To get an updated value of versionStamp that is stored in the database, I used doctrine query that retrieve partial object
      $em = $this->getConfigurationPool()
                 ->getContainer()->get('doctrine')->getManager();
              
      $class_name = get_class($object);        
      $q = $em->createQuery("select partial o.{id,versionStamp} from $class_name o"
              . " where o.id=".$object->getId());        
      $saved_data = $q->getArrayResult();        
      $saved_version_stamp = $saved_data[0]['versionStamp'];

      *If you retrieve the whole object from the database again, it will cause many issues specially if doctrine result cache is enabled.
    • Then compare the two values with each other, if those values are not equal, an error shall be appear to the user, and that is performed by calling $errorElement->addViolation() method
      if($saved_version_stamp != $submitted_version_stamp){
          $errorElement->addViolation('Record data seems outdated, probably someone else modified it, please refresh and try again.')->end();
      }

That’s all, now I can perform some basic test.

Test Solution

In order to verify that this mechanism solved the issue, I emulated the inconsistency behavior, by opening sonata admin edit page on two browsers, and then try to modify the data and submitted on each browser consequently.
The browser that got submitted last, will not save data and error will appear
Record data seems outdated, probably someone else modified it, please refresh and try again.”
versionstamp test
In that way, the inconsistency is prevented by stopping the second user from overriding the information modified by the other user.

At last

“Version Stamp” approach helped me in preventing data inconsistency in my Symfony/SonataAdmin application, hope it will help others who face similar scenario. I would like to know if anyone else has other idea or better way to handle that issue.