Merge pull request #1108 from sjpadgett/lbf-nationnote-issue
[openemr.git] / repositories / VersionRepository.php
blobdd1d795da0dacb50ebb1958bff6674ce7038d097
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 OpenEMR\Repositories;
25 use Doctrine\ORM\EntityRepository;
26 use OpenEMR\Entities\Version;
28 class VersionRepository extends EntityRepository
30 /**
31 * Updates the sole version entry in the database. The version
32 * table doesn't use unique keys, so a special merger function
33 * is used to ensure Doctrine doesn't insert a new entry and
34 * only updates the original one.
36 * @param $version the new version entry.
37 * @return true/false for if the update went through.
39 public function update(Version $version)
41 $response = false;
43 try {
44 $objectToBeUpdated = $this->updateNonKeyedEntityObject($this->findFirst(), $version);
45 $updateInformation = $this->_em->persist($objectToBeUpdated);
46 $this->_em->flush();
47 $response = true;
48 } catch (Exception $e) {
51 return $response;
54 /**
55 * Finds the sole version entry in the database.
57 * @return version.
59 public function findFirst()
61 $results = $this->_em->getRepository($this->_entityName)->findAll();
62 if (!empty($results)) {
63 return $results[0];
66 return null;
69 /**
70 * Uses MySQL-specific check to see if the version table exists.
72 * @return bool
74 public function doesTableExist()
76 $query = $this->_em->getConnection()->prepare("SHOW TABLES LIKE 'version'");
77 $query->execute();
78 $results = $query->fetch();
80 return !empty($results);
83 /**
84 * Special merger function to ensure Doctrine doesn't insert a new entry and
85 * only updates the original one.
87 * @param $objectToBeUpdated the current entry in the table that needs to be updated.
88 * @param $newObject the new value object.
89 * @return the updated version ready to override the current version entry in the table.
91 private function updateNonKeyedEntityObject(Version $objectToBeUpdated, Version $newObject)
93 if (!empty($objectToBeUpdated)) {
94 $objectToBeUpdated->setAcl($newObject->getAcl());
95 $objectToBeUpdated->setDatabase($newObject->getDatabase());
96 $objectToBeUpdated->setTag($newObject->getTag());
97 $objectToBeUpdated->setRealPatch($newObject->getRealPatch());
98 $objectToBeUpdated->setPatch($newObject->getPatch());
99 $objectToBeUpdated->setMinor($newObject->getMinor());
100 $objectToBeUpdated->setMajor($newObject->getMajor());
102 return $objectToBeUpdated;
105 return null;