Entity/DocumentTagRepository.php
author ymh <ymh.work@gmail.com>
Fri, 26 Sep 2014 10:04:40 +0200
changeset 136 f209fcb0df6c
parent 133 550862f07e6a
permissions -rwxr-xr-x
Added tag V00.18 for changeset ab728cb3632c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
74
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     2
/*
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     3
 * This file is part of the WikiTagBundle package.
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     4
 *
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     6
 *
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     8
 * file that was distributed with this source code.
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     9
 */
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Entity;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
133
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    13
use Doctrine\ORM\AbstractQuery;
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    14
use Doctrine\ORM\Query;
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    15
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    16
use Doctrine\ORM\QueryBuilder;
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    17
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use Doctrine\ORM\EntityRepository;
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    19
use Doctrine\ORM\NoResultException;
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
/**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * DocumentTagRepository
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 *
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * This class was generated by the Doctrine ORM. Add your own custom
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * repository methods below.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
class DocumentTagRepository extends EntityRepository
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
{
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    29
   /**
133
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    30
    * Find ordered document tags by document id.
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    31
    * Allow to control the hydration mode of the returned data (c.f. Doctrine\ORM\AbstractQuery::HYDRATE_*)
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    32
    *
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    33
    * @param mixed $doc_id The document id
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    34
    * @param int   $hydrationMode Value from Doctrine\ORM\AbstractQuery::HYDRATE_*. 
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    35
    * @return array (c.f from Doctrine\ORM\AbstractQuery::get_result())
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    */
133
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    37
    public function findOrderedTagsForDoc($doc_id, $hydrationMode=AbstractQuery::HYDRATE_OBJECT)
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        return $this->getEntityManager()
133
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    40
        ->createQuery("SELECT doctag, tag FROM WikiTagBundle:DocumentTag doctag JOIN doctag.tag tag JOIN doctag.document doc WHERE doc.externalId=:doc_id ORDER BY doctag.tagOrder ASC")
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    41
        ->setParameter("doc_id", strval($doc_id))
133
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
    42
        ->getResult($hydrationMode);
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    45
    private function findMaxOrderInUow($entityList, $doc_id, $max_order) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    46
        foreach($entityList as $entity) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    47
            if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface") &&
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    48
            !is_null($entity->getDocument()) &&
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    49
            ( $entity->getDocument() === $doc_id || $entity->getDocument()->getId() === $doc_id ) )
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    50
            {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    51
                $max_order = max(array($max_order, $entity->getTagOrder()));
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    52
            }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    53
        }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    54
        return $max_order;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    55
    }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    56
    
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    57
        
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
     *  Gets the max order of all tags for one document
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    public function getMaxOrder($doc_id)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    {
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    63
        $max_order_res = $this->getEntityManager()
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    64
            ->createQuery("SELECT MAX(doctag.tagOrder) FROM WikiTagBundle:DocumentTag doctag JOIN doctag.document doc WHERE doc.externalId= :doc_id")
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    65
            ->setParameter("doc_id", strval($doc_id))
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    66
            ->getSingleScalarResult();
65
ba6b8e38d90e Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 57
diff changeset
    67
        
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    68
        $max_order = 0;
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    69
        if(!is_null($max_order_res)) {
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    70
            $max_order = intval($max_order_res);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    71
        }
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    72
        
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    73
        $uow = $this->getEntityManager()->getUnitOfWork();
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    74
        
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    75
        $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityInsertions(), $doc_id, $max_order);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    76
        $max_order = $this->findMaxOrderInUow($uow->getScheduledEntityUpdates(), $doc_id, $max_order);
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    77
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
    78
        return $max_order;
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    81
    /**
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    82
     *
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    83
     * Enter description here ...
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    84
     * @param unknown_type $external_id
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    85
     * @param array $filter_array
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    86
     * @return QueryBuilder
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    87
     */
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    88
    private function createQueryBuilderByDocumentExternalId($external_id, array $filter_array=null) {
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    89
        $qb = $this->createQueryBuilder("dt")
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    90
        ->join('dt.document', 'd')
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    91
        ->where('d.externalId = :external_id');
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    92
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    93
        $params = array("external_id"=>strval($external_id));
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    94
        
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    95
        if(!is_null($filter_array)) {
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    96
            foreach ($filter_array as $key => $value) {
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 5
diff changeset
    97
                $qb = $qb->andWhere("dt.$key = :$key");
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 5
diff changeset
    98
                //$params[$key] = $value;
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
    99
            }
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 5
diff changeset
   100
            $params = array_merge($params, $filter_array);
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   101
        }
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   102
        
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   103
        $qb = $qb->setParameters($params);
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   104
        
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   105
        return $qb;
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   106
    }
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   107
    
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   108
    /**
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   109
     *
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   110
     * Enter description here ...
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   111
     * @param unknown_type $external_id
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   112
     * @param array $filter_array
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   113
     */
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   114
    public function findByDocumentExternalId($external_id, array $filter_array=null)
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   115
    {
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   116
        $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array);
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   117
        return $qb->getQuery()->getResult();
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   118
    }
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   119
    
67
989d9e117586 correct bugs on database opt
ymh <ymh.work@gmail.com>
parents: 65
diff changeset
   120
   	/**
34
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   121
     *
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   122
     * Enter description here ...
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   123
     * @param unknown_type $external_id
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   124
     * @param array $filter_array
21fab44f46fe add event on tag changed
ymh <ymh.work@gmail.com>
parents: 8
diff changeset
   125
     */
5
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   126
    public function findOneByDocumentExternalId($external_id, array $filter_array=null) {
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   127
        
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   128
        $qb = $this->createQueryBuilderByDocumentExternalId($external_id, $filter_array)->setMaxResults(1);
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   129
        
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   130
        try {
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   131
            return $qb->getQuery()->getSingleResult();
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   132
        } catch (NoResultException $e) {
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   133
            return null;
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   134
        }
45378793512a Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents: 2
diff changeset
   135
    }
57
186c4121c7b3 add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents: 34
diff changeset
   136
133
550862f07e6a Add version file and add parameter to Entity/DocumentTagRepository::findOrderedTagsForDoc to control data hydration
ymh <ymh.work@gmail.com>
parents: 74
diff changeset
   137
}