Revision: 84
http://sourceforge.net/p/beeframework/code/84
Author: m_plomer
Date: 2013-09-02 20:36:30 +0000 (Mon, 02 Sep 2013)
Log Message:
-----------
- DaoBase / AbstractGenericDao (input from SGE)
Modified Paths:
--------------
trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php
Modified: trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php
===================================================================
--- trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php 2013-09-02 18:56:31 UTC (rev 83)
+++ trunk/framework/Bee/Persistence/Doctrine2/AbstractGenericDao.php 2013-09-02 20:36:30 UTC (rev 84)
@@ -33,36 +33,8 @@
* @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);
- }
-
+ $this->applyWhereId($id, $qb);
return $this->getSingleResult($qb);
}
@@ -119,4 +91,40 @@
$idFields = $classMetadata->getIdentifierFieldNames();
return count($idFields) > 1 ? $idFields : $idFields[0];
}
+
+ /**
+ * @param mixed $id
+ * @param QueryBuilder $qb
+ * @throws \UnexpectedValueException
+ */
+ protected function applyWhereId($id, QueryBuilder $qb) {
+ $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();
+ 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);
+ }
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|