entityName = $entityName; $this->entityFactory = $entityFactory; $this->seed = $this->entityFactory->create($entityName); $this->entityClassName = get_class($this->seed); $this->entityManager = $entityManager; $this->init(); } protected function init() { } protected function getMapper() { if (empty($this->mapper)) { $this->mapper = $this->getEntityManager()->getMapper('RDB'); } return $this->mapper; } public function handleSelectParams(&$params) { } protected function getEntityFactory() { return $this->entityFactory; } protected function getEntityManager() { return $this->entityManager; } public function reset() { $this->whereClause = array(); $this->listParams = array(); } protected function getNewEntity() { $entity = $this->entityFactory->create($this->entityName); if ($entity) { $entity->setIsNew(true); $entity->populateDefaults(); return $entity; } } protected function getEntityById($id) { $params = array(); $this->handleSelectParams($params); $entity = $this->entityFactory->create($this->entityName); if ($entity) { if ($this->getMapper()->selectById($entity, $id, $params)) { $entity->setAsFetched(); return $entity; } } return null; } public function get($id = null) { if (empty($id)) { return $this->getNewEntity(); } return $this->getEntityById($id); } protected function beforeSave(Entity $entity, array $options = array()) { } protected function afterSave(Entity $entity, array $options = array()) { } public function save(Entity $entity, array $options = array()) { $this->beforeSave($entity, $options); if ($entity->isNew() && !$entity->isSaved()) { $result = $this->getMapper()->insert($entity); } else { $result = $this->getMapper()->update($entity); } if ($result) { $entity->setIsSaved(true); $this->afterSave($entity, $options); if ($entity->isNew()) { $entity->setIsNew(false); } else { if ($entity->isFetched()) { $entity->updateFetchedValues(); } } } return $result; } protected function beforeRemove(Entity $entity, array $options = array()) { } protected function afterRemove(Entity $entity, array $options = array()) { } public function remove(Entity $entity, array $options = array()) { $this->beforeRemove($entity, $options); $result = $this->getMapper()->delete($entity); if ($result) { $this->afterRemove($entity, $options); } return $result; } public function deleteFromDb($id) { return $this->getMapper()->deleteFromDb($this->entityName, $id); } public function find(array $params = array()) { $this->handleSelectParams($params); $params = $this->getSelectParams($params); $dataArr = $this->getMapper()->select($this->seed, $params); $collection = new EntityCollection($dataArr, $this->entityName, $this->entityFactory); $this->reset(); return $collection; } public function findOne(array $params = array()) { $collection = $this->limit(0, 1)->find($params); if (count($collection)) { return $collection[0]; } return null; } public function findByQuery($sql) { $dataArr = $this->getMapper()->selectByQuery($this->seed, $sql); $collection = new EntityCollection($dataArr, $this->entityName, $this->entityFactory); $this->reset(); return $collection; } public function findRelated(Entity $entity, $relationName, array $params = array()) { if ($entity->isNew()) { return; } $entityName = $entity->relations[$relationName]['entity']; $this->getEntityManager()->getRepository($entityName)->handleSelectParams($params); $result = $this->getMapper()->selectRelated($entity, $relationName, $params); if (is_array($result)) { $collection = new EntityCollection($result, $entityName, $this->entityFactory); return $collection; } else { return $result; } } public function countRelated(Entity $entity, $relationName, array $params = array()) { if (!$entity->id) { return; } $entityName = $entity->relations[$relationName]['entity']; $this->getEntityManager()->getRepository($entityName)->handleSelectParams($params); return $this->getMapper()->countRelated($entity, $relationName, $params); } public function relate(Entity $entity, $relationName, $foreign, $data = null) { if (!$entity->id) { return; } if ($data instanceof \stdClass) { $data = get_object_vars($data); } if ($foreign instanceof Entity) { return $this->getMapper()->relate($entity, $relationName, $foreign, $data); } if (is_string($foreign)) { return $this->getMapper()->addRelation($entity, $relationName, $foreign, null, $data); } return false; } public function unrelate(Entity $entity, $relationName, $foreign) { if (!$entity->id) { return; } if ($foreign instanceof Entity) { return $this->getMapper()->unrelate($entity, $relationName, $foreign); } if (is_string($foreign)) { return $this->getMapper()->removeRelation($entity, $relationName, $foreign); } if ($foreign === true) { return $this->getMapper()->removeAllRelations($entity, $relationName); } return false; } public function updateRelation(Entity $entity, $relationName, $foreign, $data) { if (!$entity->id) { return; } if ($data instanceof \stdClass) { $data = get_object_vars($data); } if ($foreign instanceof Entity) { $id = $foreign->id; } else { $id = $foreign; } if (is_string($foreign)) { return $this->getMapper()->updateRelation($entity, $relationName, $id, $data); } return false; } public function massRelate(Entity $entity, $relationName, array $params = array()) { if (!$entity->id) { return; } return $this->getMapper()->massRelate($entity, $relationName, $params); } public function getAll() { $this->reset(); return $this->find(); } public function count(array $params = array()) { $this->handleSelectParams($params); $params = $this->getSelectParams($params); $count = $this->getMapper()->count($this->seed, $params); $this->reset(); return $count; } public function max($field) { $params = $this->getSelectParams(); return $this->getMapper()->max($this->seed, $params, $field); } public function min($field) { $params = $this->getSelectParams(); return $this->getMapper()->min($this->seed, $params, $field); } public function sum($field) { $params = $this->getSelectParams(); return $this->getMapper()->sum($this->seed, $params, $field); } public function join() { $args = func_get_args(); if (empty($this->listParams['joins'])) { $this->listParams['joins'] = []; } foreach ($args as &$param) { if (is_array($param)) { foreach ($param as $k => $v) { $this->listParams['joins'][] = $v; } } else { $this->listParams['joins'][] = $param; } } return $this; } public function leftJoin() { $args = func_get_args(); if (empty($this->listParams['leftJoins'])) { $this->listParams['leftJoins'] = []; } foreach ($args as &$param) { if (is_array($param)) { foreach ($param as $k => $v) { $this->listParams['leftJoins'][] = $v; } } else { $this->listParams['leftJoins'][] = $param; } } return $this; } public function distinct() { $this->listParams['distinct'] = true; return $this; } public function where($param1 = array(), $param2 = null) { if (is_array($param1)) { $this->whereClause = $param1 + $this->whereClause; } else { if (!is_null($param2)) { $this->whereClause[$param1] = $param2; } } return $this; } public function order($field = 'id', $direction = "ASC") { $this->listParams['orderBy'] = $field; $this->listParams['order'] = $direction; return $this; } public function limit($offset, $limit) { $this->listParams['offset'] = $offset; $this->listParams['limit'] = $limit; return $this; } public function setListParams(array $params = array()) { $this->listParams = $params; } public function getListParams() { return $this->listParams; } protected function getSelectParams(array $params = array()) { if (isset($params['whereClause'])) { $params['whereClause'] = $params['whereClause'] + $this->whereClause; } else { $params['whereClause'] = $this->whereClause; } $params = $params + $this->listParams; return $params; } protected function getPDO() { return $this->getEntityManager()->getPDO(); } }