Newscoop\Entity\Repository\TemplateRepository::updateKey PHP Method

updateKey() public method

Update key
public updateKey ( string $old, string $new ) : void
$old string
$new string
return void
    public function updateKey($old, $new)
    {
        $em = $this->getEntityManager();
        $old = $this->formatKey($old);
        $new = $this->formatKey($new);
        $templates = $this->createQueryBuilder('t')->where("t.key LIKE ?1")->setParameter(1, "{$old}%")->getQuery()->getResult();
        foreach ($templates as $template) {
            if (strpos($template->getKey(), $old) === 0) {
                $template->setKey(str_replace($old, $new, $template->getKey()));
                $em->persist($template);
            }
        }
        $em->flush();
    }

Usage Example

Example #1
0
 /**
  * Rename item
  *
  * @param string $src
  * @param string $dest
  * @return void
  * @throws InvalidArgumentException
  */
 public function renameItem($src, $dest)
 {
     try {
         $name = basename($dest);
         $dest = ltrim(dirname($src) . $name, './');
         $this->storage->renameItem($src, $name);
         $this->repository->updateKey($src, $dest);
     } catch (\InvalidArgumentException $e) {
         throw new \InvalidArgumentException(sprintf('The template object %s could not be renamed.', basename($src)), $e->getCode(), $e);
     }
 }