Composite key type duplicate key check with ZF2 Doctrine

Their are instances when we need to add some composite keys in our DBs. Then we also need a check on application side too. This is also same as duplicate check on multiple fields.

In ZF2, we know about Zend\Validator\Db\RecordExists & Zend\Validator\Db\NoRecordExists combo for duplicate check on database side (Zend DB). Their are also respective relative in doctrine called DoctrineModule\Validator\ObjectExists & DoctrineModule\Validator\NoObjectExists. They do the same job as Zend DB counter part with one caveat.

They do not support multiple fields when work with InputFilters. You can pass array but that gave error "zf2 Provided values count is 1, while expected number of fields to be matched is 2". The solution are using your custom validator.

I tried here to write a Validator Plugin which extends DoctrineModule\Validator\NoObjectExists and have support for multiple fields. See implementation code .

$inputFilter->add($factory->createInput(array(
   'name' => 'studentId',
   'required' => true,
   'filters' => array(
   array('name' => 'StripTags'),
   array('name' => 'StringTrim'),
 ),
 'validators' => array(
    array(
     'name' => 'Student\Validate\NoObjectExists',
     'options' => array(
     'object_repository' => $this->getObjectManager()->getRepository('Student\Entity\Student'),
     'fields' => array('studentId', 'class'),
    )
  )
 ),
 )));

Validator Plugin Code are as :

namespace Student\Validate;

use DoctrineModule\Validator\NoObjectExists as DoctrineModuleNoObjectExists;

class NoObjectExists extends DoctrineModuleNoObjectExists
{
  protected $messageTemplates = array(
   self::ERROR_OBJECT_FOUND => "An object matching combination of fields was found",
  );

  public function isValid($value, $context = null)
  {
    $valueArray = array();
    foreach($this->fields as $name => $val)
    {
       $valueArray[] = $context[$val];
    }
    $value = $this->cleanSearchValue($valueArray);

    $match = $this->objectRepository->findOneBy($value);

    if (is_object($match)) {
       $this->error(self::ERROR_OBJECT_FOUND, $value);
       return false;
    }

    return true;
  }
}

Further link can be found at : Github link

Assign role to user in doctrine

From start of days of symfony sfauth plugin play a special role in making developer’s life easy. It give structured way to assign roles and permissions to user.
So at the time of creation of user add/edit forms you can see the groups (roles) and permissions to choose. Also permissions can also direct work for whole group.
But if we need to set permissons/roles to user without using forms then how it can be implemented?
Well yesterday i am searching any workaround for this.
Normally we can use code like that
$sf_user = new sfGuardUser();
$sf_user->setUsername($username);
$sf_user->save();
But i need to set groups against user. Here are a problem, group have many to many relationship with user. Si how i can set the group using similiar code. First i tried following combinations but no one seems to work. Some gave errors some not.
Here $group is not a normal variable but a doctrine collection of group.
$sf_user->setSfUserGroup($group);
$sf_user->setGroup($group);
$sf_user->Groups = $group;

As user can have multiple groups so setting group also reflect the same way.
$sf_user->Groups[] = $group;
Here we can see we are not assigning but adding a group against a user. So above example works.