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

isUsed() public method

Test is used
public isUsed ( string $key ) : boolean
$key string
return boolean
    public function isUsed($key)
    {
        $em = $this->getEntityManager();
        $template = $this->findOneBy(array('key' => $this->formatKey($key)));
        if (!$template) {
            return false;
        }
        $dql = "SELECT COUNT(i.number)\n                FROM Newscoop\\Entity\\Issue i\n                WHERE i.template = ?1\n                    OR i.sectionTemplate = ?1\n                    OR i.articleTemplate = ?1";
        $query = $em->createQuery($dql);
        $query->setParameter(1, $template);
        if ($query->getSingleScalarResult()) {
            return true;
        }
        $dql = "SELECT COUNT(s.number)\n                FROM Newscoop\\Entity\\Section s\n                WHERE s.template = ?1\n                    OR s.articleTemplate = ?1";
        $query = $em->createQuery($dql);
        $query->setParameter(1, $template);
        if ($query->getSingleScalarResult()) {
            return true;
        }
        return false;
    }

Usage Example

Example #1
0
 /**
  * Delete item
  *
  * @param string $key
  * @return void
  * @throws InvalidArgumentException
  */
 public function deleteItem($key)
 {
     if ($this->repository->isUsed($key) || $this->storage->isUsed($key)) {
         throw new \InvalidArgumentException(sprintf("The template object %s is in use and can not be deleted.", $key));
     }
     try {
         $this->storage->deleteItem($key);
         $this->repository->delete($key);
     } catch (\InvalidArgumentException $e) {
         throw new \InvalidArgumentException(sprintf("Can't remove non empty directory '%s'", basename($key)), $e->getCode(), $e);
     }
 }