Newscoop\Services\RelatedArticlesService::positionRelateArticle PHP 메소드

positionRelateArticle() 개인적인 메소드

private positionRelateArticle ( $relatedArticles, $relatedArticle, $position )
    private function positionRelateArticle($relatedArticles, $relatedArticle, $position)
    {
        if ($position == false) {
            return;
        }
        $this->initOrderOnRelatedArticles($relatedArticles);
        try {
            $this->em->getConnection()->exec('LOCK TABLES context_articles WRITE, context_articles as c0_ WRITE;');
            // check if position isn't bigger that max one;
            $maxPosition = $this->em->createQuery('SELECT COUNT(r) FROM Newscoop\\Entity\\RelatedArticle r WHERE r.articleListId = :articleListId AND r.order > 0 ORDER BY r.order ASC')->setParameter('articleListId', $relatedArticles->getId())->getSingleScalarResult();
            if ($position > (int) $maxPosition) {
                $position = (int) $maxPosition;
            }
            // get article - move to position 0
            $oldOrder = $relatedArticle->getOrder();
            $relatedArticle->setOrder(0);
            $this->em->flush();
            // move all bigger than old position up (-1)
            $this->em->createQuery('UPDATE Newscoop\\Entity\\RelatedArticle r SET r.order = r.order-1 WHERE r.order > :oldPosition')->setParameter('oldPosition', $oldOrder)->execute();
            // move all bigger than new position down (+1)
            $this->em->createQuery('UPDATE Newscoop\\Entity\\RelatedArticle r SET r.order = r.order+1 WHERE r.order >= :newPosition')->setParameter('newPosition', $position)->execute();
            // move changed element from position 0 to new position
            $relatedArticle->setOrder($position);
            $this->em->flush();
            $this->em->getConnection()->exec('UNLOCK TABLES;');
        } catch (\Exception $e) {
            $this->em->getConnection()->exec('UNLOCK TABLES;');
        }
    }