Merge pull request #899 from stephenwaite/admin_fix
[openemr.git] / repositories / VersionRepository.php
blob0492a71a1675212dc8b1c195082ca6d0e010fbc5
1 <?php
2 /**
3 * Version repository.
5 * Copyright (C) 2016 Matthew Vita <matthewvita48@gmail.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Matthew Vita <matthewvita48@gmail.com>
20 * @link http://www.open-emr.org
23 namespace repositories;
25 use entities\Version;
26 use Doctrine\ORM\EntityRepository;
28 class VersionRepository extends EntityRepository {
29 /**
30 * Updates the sole version entry in the database. The version
31 * table doesn't use unique keys, so a special merger function
32 * is used to ensure Doctrine doesn't insert a new entry and
33 * only updates the original one.
35 * @param $version the new version entry.
36 * @return true/false for if the update went through.
38 public function update(Version $version) {
39 $response = false;
41 try {
42 $objectToBeUpdated = $this->updateNonKeyedEntityObject($this->findFirst(), $version);
43 $updateInformation = $this->_em->persist($objectToBeUpdated);
44 $this->_em->flush();
45 $response = true;
46 } catch (Exception $e) {}
48 return $response;
51 /**
52 * Finds the sole version entry in the database.
54 * @return version.
56 public function findFirst() {
57 $results = $this->_em->getRepository($this->_entityName)->findAll();
58 if (!empty($results)) {
59 return $results[0];
62 return null;
65 /**
66 * Uses MySQL-specific check to see if the version table exists.
68 * @return bool
70 public function doesTableExist() {
71 $query = $this->_em->getConnection()->prepare("SHOW TABLES LIKE 'version'");
72 $query->execute();
73 $results = $query->fetch();
75 return !empty($results);
78 /**
79 * Special merger function to ensure Doctrine doesn't insert a new entry and
80 * only updates the original one.
82 * @param $objectToBeUpdated the current entry in the table that needs to be updated.
83 * @param $newObject the new value object.
84 * @return the updated version ready to override the current version entry in the table.
86 private function updateNonKeyedEntityObject(Version $objectToBeUpdated, Version $newObject) {
87 if (!empty($objectToBeUpdated)) {
88 $objectToBeUpdated->setAcl($newObject->getAcl());
89 $objectToBeUpdated->setDatabase($newObject->getDatabase());
90 $objectToBeUpdated->setTag($newObject->getTag());
91 $objectToBeUpdated->setRealPatch($newObject->getRealPatch());
92 $objectToBeUpdated->setPatch($newObject->getPatch());
93 $objectToBeUpdated->setMinor($newObject->getMinor());
94 $objectToBeUpdated->setMajor($newObject->getMajor());
96 return $objectToBeUpdated;
99 return null;