예제 #1
0
 /**
  * get ids for tags and create non-existing ones
  *
  * @param array        $tags Array of strings
  * @param QueryBuilder $qb   Query builder instance
  * @return array (name => id)
  */
 public function getIdsForTags(array $tags, User $user, QueryBuilder $qb = null)
 {
     if ($qb == null) {
         $qb = $this->builder;
     }
     if (!empty($tags)) {
         $qb->add('select', 't.name, t.id')->andWhere($qb->expr()->andX($qb->expr()->in('t.name', ':tags'), $qb->expr()->eq('t.user', ':user')))->setParameters(array('tags' => $tags, 'user' => $user->getId()));
         $existingTags = $qb->getQuery()->getResult();
         $ids = array();
         foreach ($existingTags as $tag) {
             $ids[$tag['name']] = $tag['id'];
         }
         $missingTags = array_diff($tags, array_keys($ids));
         if (0 < count($missingTags)) {
             foreach ($missingTags as $tagName) {
                 $newTag = new Tag();
                 $newTag->setName($tagName);
                 $newTag->setUser($user);
                 $this->getEntityManager()->persist($newTag);
             }
             $this->getEntityManager()->flush();
             $ids = array_merge($ids, $this->getIdsForTags(array_values($missingTags), $user, $qb));
         }
         return array_values($ids);
     } else {
         return array();
     }
 }
예제 #2
0
 /**
  * Called on bind
  *
  * @param mixed $value
  * @return mixed|void
  */
 public function reverseTransform($value)
 {
     $result = array();
     if (null == $value) {
         return $result;
     }
     if (is_string($value)) {
         $value = explode(' ', $value);
     }
     $tagNames = array();
     $tagIds = array();
     foreach ($value as $tag) {
         if (is_numeric($tag)) {
             $tagIds[] = $tag;
         } else {
             $tagNames[] = $tag;
         }
     }
     $repository = $this->em->getRepository('DimeTimetrackerBundle:Tag');
     if (!empty($tagIds)) {
         $qb = $repository->createQueryBuilder('t');
         $qb->andWhere($qb->expr()->andX($qb->expr()->in('t.id', ':ids'), $qb->expr()->eq('t.user', ':user')));
         $qb->setParameters(array('ids' => $tagIds, 'user' => $this->user->getId()));
         $dbResults = $qb->getQuery()->getResult();
         foreach ($dbResults as $tag) {
             $result[] = $tag;
         }
     }
     if (!empty($tagNames)) {
         $qb = $repository->createQueryBuilder('t');
         $qb->andWhere($qb->expr()->andX($qb->expr()->in('t.name', ':tags'), $qb->expr()->eq('t.user', ':user')));
         $qb->setParameters(array('tags' => $tagNames, 'user' => $this->user->getId()));
         $existingTags = array();
         $dbResults = $qb->getQuery()->getResult();
         foreach ($dbResults as $tag) {
             $result[] = $tag;
             $existingTags[] = $tag->getName();
         }
         $missingTags = array_diff($tagNames, $existingTags);
         if (count($missingTags) > 0) {
             foreach ($missingTags as $name) {
                 $name = trim($name);
                 if (!empty($name)) {
                     $newTag = new Tag();
                     $newTag->setName($name);
                     $newTag->setUser($this->user);
                     $result[] = $newTag;
                 }
             }
         }
     }
     return $result;
 }
예제 #3
0
 /**
  * Load data fixtures with the passed EntityManager
  *
  * @param Doctrine\Common\Persistence\ObjectManager $manager
  */
 public function load(ObjectManager $manager)
 {
     $defaultUser = new User();
     $defaultUser->setUsername('admin');
     $defaultUser->setPlainPassword('kitten');
     $defaultUser->setEnabled(true);
     $defaultUser->addRole(User::ROLE_SUPER_ADMIN);
     $defaultUser->setFirstname('Default');
     $defaultUser->setLastname('User');
     $defaultUser->setEmail('*****@*****.**');
     $manager->persist($defaultUser);
     $testUser = new User();
     $testUser->setUsername('test');
     $testUser->setPlainPassword('kitten');
     $testUser->setEnabled(true);
     $testUser->addRole(User::ROLE_DEFAULT);
     $testUser->setFirstname('Test');
     $testUser->setLastname('User');
     $testUser->setEmail('*****@*****.**');
     $manager->persist($testUser);
     $manager->flush();
     $this->addReference('default-user', $defaultUser);
     $this->addReference('test-user', $testUser);
 }