Revision: 83
http://sourceforge.net/p/beeframework/code/83
Author: m_plomer
Date: 2013-09-02 18:56:31 +0000 (Mon, 02 Sep 2013)
Log Message:
-----------
- DaoBase / AbstractGenericDao (input from SGE)
Modified Paths:
--------------
trunk/framework/Bee/Persistence/Doctrine2/DaoBase.php
Added Paths:
-----------
trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php
trunk/framework/Bee/Persistence/Doctrine2/DaoUtils.php
Added: trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php
===================================================================
--- trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php (rev 0)
+++ trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php 2013-09-02 18:56:31 UTC (rev 83)
@@ -0,0 +1,122 @@
+<?php
+namespace Bee\Persistence\Doctrine2;
+/*
+ * Copyright 2008-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+use Doctrine\ORM\Query;
+use Doctrine\ORM\QueryBuilder;
+use UnexpectedValueException;
+
+/**
+ * User: mp
+ * Date: 02.09.13
+ * Time: 20:35
+ */
+
+abstract class AbstractGenericDao extends DaoBase {
+
+ /**
+ * @param mixed $id
+ * @throws UnexpectedValueException
+ * @return mixed
+ */
+ public function getById($id) {
+ $idFields = $this->getIdFieldName();
+
+ $expectedDim = count($idFields);
+ $actualDim = count($id);
+
+ // unpack single-valued id if necessary
+ if(is_array($id) && $actualDim === 1) {
+ $id = $id[0];
+ }
+
+ $baseEntityAlias = $this->getEntityAlias();
+ $qb = $this->getBaseQuery();
+ if($expectedDim > 1) {
+ // composite key
+ if($actualDim === 1) {
+ $id = DaoUtils::explodeScalarId($id, $idFields);
+ } else if($actualDim !== $expectedDim) {
+ throw new UnexpectedValueException('Dimension of given ID ('.count($id).') does not match expected dimension ('.count($idFields).').');
+ }
+
+ // here we can be sure that the dimensions match - both branches above would have thrown otherwise
+ $whereParts = array();
+ array_walk($id, function($value, $key) use($baseEntityAlias, &$whereParts) {
+ $whereParts[] = $baseEntityAlias . '.' . $key . ' = ' . ':' . $key;
+ });
+ $qb->where(implode(' AND ', $whereParts))->setParameters($id);
+ } else {
+ $qb->where($baseEntityAlias . '.' . $idFields . ' = :id')->setParameter('id', $id);
+ }
+
+ return $this->getSingleResult($qb);
+ }
+
+ /**
+ * @param QueryBuilder $qb
+ * @return mixed
+ */
+ protected function getSingleResult(QueryBuilder $qb) {
+ $q = $qb->getQuery();
+ $this->decorateQuery($q);
+ return $q->getSingleResult($this->getHydrationMode());
+ }
+
+ /**
+ * @param Query $q
+ */
+ protected function decorateQuery(Query $q) {
+ }
+
+ /**
+ * @return QueryBuilder
+ */
+ protected function getBaseQuery() {
+ $baseEntityAlias = $this->getEntityAlias();
+ $indexBy = count($this->getIdFieldName()) > 1 ? null : $baseEntityAlias . '.' . $this->getIdFieldName();
+ return $this->getEntityManager()->createQueryBuilder()->select($baseEntityAlias)
+ ->from($this->getEntity(), $baseEntityAlias, $indexBy);
+ }
+
+ /**
+ * @return string
+ */
+ protected function getEntityAlias() {
+ return 'e';
+ }
+
+ /**
+ * @return null
+ */
+ protected function getHydrationMode() {
+ return null;
+ }
+
+ /**
+ * @return string
+ */
+ public abstract function getEntity();
+
+ /**
+ * @return string|array
+ */
+ protected function getIdFieldName() {
+ $classMetadata = $this->getEntityManager()->getClassMetadata($this->getEntity());
+ $idFields = $classMetadata->getIdentifierFieldNames();
+ return count($idFields) > 1 ? $idFields : $idFields[0];
+ }
+}
Modified: trunk/framework/Bee/Persistence/Doctrine2/DaoBase.php
===================================================================
--- trunk/framework/Bee/Persistence/Doctrine2/DaoBase.php 2013-09-02 03:36:46 UTC (rev 82)
+++ trunk/framework/Bee/Persistence/Doctrine2/DaoBase.php 2013-09-02 18:56:31 UTC (rev 83)
@@ -1,7 +1,9 @@
<?php
namespace Bee\Persistence\Doctrine2;
+use Bee_Framework;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
+use Logger;
/**
* User: mp
@@ -11,11 +13,27 @@
class DaoBase extends EntityManagerHolder {
/**
+ * @var Logger
+ */
+ protected $log;
+
+ /**
+ * @return Logger
+ */
+ protected function getLog() {
+ if (!$this->log) {
+ $this->log = Logger::getLogger(get_class($this));
+ }
+ return $this->log;
+ }
+
+ /**
* @param \Doctrine\ORM\QueryBuilder $queryBuilder
* @param \Bee_Persistence_IRestrictionHolder $restrictionHolder
* @param \Bee_Persistence_IOrderAndLimitHolder $orderAndLimitHolder
* @param array $defaultOrderMapping
*
+ * @param null $hydrationMode
* @internal param \Doctrine\ORM\QueryBuilder $query
* @return array
*/
@@ -114,20 +132,22 @@
* @return mixed
*/
public function doInTransaction($func) {
- $this->getDoctrineConnection()->beginTransaction();
+ $this->getLog()->info('Begin transaction.');
+ $this->getEntityManager()->beginTransaction();
try {
- $result = $func($this, self::getLog());
+ $result = $func($this, $this->getLog());
- $this->getDoctrineConnection()->commit();
- $this->getDoctrineConnection()->flush();
+ $this->getLog()->info('Transaction committing...');
+ $this->getEntityManager()->commit();
+ $this->getEntityManager()->flush();
+
+ $this->getLog()->info('Transaction committed!');
return $result;
- } catch(\Exception $e) {
- self::getLog()->debug('exception caught', $e);
- $this->getDoctrineConnection()->rollBack();
+ } catch (\Exception $e) {
+ $this->getLog()->warn('Exception caught, rolling back!', $e);
+ $this->getEntityManager()->rollBack();
throw $e;
}
-
}
-
}
Added: trunk/framework/Bee/Persistence/Doctrine2/DaoUtils.php
===================================================================
--- trunk/framework/Bee/Persistence/Doctrine2/DaoUtils.php (rev 0)
+++ trunk/framework/Bee/Persistence/Doctrine2/DaoUtils.php 2013-09-02 18:56:31 UTC (rev 83)
@@ -0,0 +1,49 @@
+<?php
+namespace Bee\Persistence\Doctrine2;
+/*
+ * Copyright 2008-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+use UnexpectedValueException;
+
+/**
+ * User: mp
+ * Date: 02.09.13
+ * Time: 20:42
+ */
+
+class DaoUtils {
+
+ /**
+ * @param array $ids
+ * @return string
+ */
+ public static function buildScalarId(array $ids) {
+ return implode(':', $ids);
+ }
+
+ /**
+ * @param $scalarId
+ * @param array $idKeys
+ * @return array
+ * @throws \UnexpectedValueException
+ */
+ public static function explodeScalarId($scalarId, array $idKeys) {
+ $res = explode(':', $scalarId);
+ if(!count($res) === count($idKeys)) {
+ throw new UnexpectedValueException('Scalar ID representation "'. $scalarId .'" malformed, must represent values for ' . count($idKeys) . ' fields.');
+ }
+ return array_combine($idKeys, $res);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|