protected static function _generateHelperMethods(SimDAL_Mapper_Entity $mapping) { $associations = $mapping->getAssociations(); $output = ''; $output .= ' public function __construct($data, SimDAL_Session $session, $id=null) {' . PHP_EOL; $output .= ' if (!is_null($id)) {' . PHP_EOL; $output .= ' $this->id = $id;' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' if (!is_array($data) && !is_object($data)) {' . PHP_EOL; $output .= ' return false;' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' $this->_session = $session;' . PHP_EOL; $output .= ' if (is_array($data)) {' . PHP_EOL; $output .= ' foreach ($data as $key=>$value) {' . PHP_EOL; $output .= ' if (property_exists($this, $key)) {' . PHP_EOL; $output .= ' $this->$key = $value;' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' } else if (is_object($data)) {' . PHP_EOL; foreach ($mapping->getColumns() as $columnName => $column) { if ($column->isAutoIncrement()) { continue; } $output .= ' if (method_exists($data, \'get' . $columnName . '\')) {' . PHP_EOL; $output .= ' $this->' . $columnName . ' = $data->get' . $columnName . '();' . PHP_EOL; $output .= ' }' . PHP_EOL; } /* @var $association SimDAL_Mapper_Association */ foreach ($associations as $association) { $method = ucfirst($association->getMethod()); $property = $association->getProperty(); $setter = 'set' . $method; $getter = 'get' . $method; $foreignKey = $association->getForeignKey(); $parentKey = $association->getParentKey(); $parentKeyGetter = 'get' . ucfirst($parentKey); $output .= ' if (method_exists($data, \'' . $getter . '\')) {' . PHP_EOL; $output .= ' $reference = $data->' . $getter . '();' . PHP_EOL; if ($association->getType() == 'many-to-one' || $association->getType() == 'one-to-one' && $association->isDependent()) { $output .= ' if (!is_null($reference) && !$this->_getSession()->isLoaded($reference) && !$this->_getSession()->isAdded($reference)) {' . PHP_EOL; $output .= ' $this->_getSession()->addEntity($reference);' . PHP_EOL; $output .= ' }' . PHP_EOL; } if ($association->getType() == 'many-to-one' || $association->getType() == 'one-to-one') { $output .= ' $this->' . $property . ' = $reference;' . PHP_EOL; if ($association->isDependent()) { $output .= ' if (!is_null($reference)) {' . PHP_EOL; $output .= ' $this->' . $foreignKey . ' = !is_null($reference)?$reference->' . $parentKeyGetter . '():null;' . PHP_EOL; $output .= ' }' . PHP_EOL; } } else { if ($association->getType() == 'one-to-many') { $output .= ' $this->' . $property . ' = $reference;' . PHP_EOL; $output .= ' if (!$reference instanceof SimDAL_Persistence_Collection) {' . PHP_EOL; $output .= ' $this->' . $getter . '();' . PHP_EOL; $output .= ' }' . PHP_EOL; } } $output .= ' $this->_SimDALAssociationIsLoaded(\'' . $association->getMethod() . '\');' . PHP_EOL; $output .= ' }' . PHP_EOL; } $output .= ' }' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' public function __destruct() {' . PHP_EOL; $output .= ' $this->_session = null;' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' private function _isSimDALAssociationLoaded($association_name) {' . PHP_EOL; $output .= ' if (!array_key_exists($association_name, $this->_loadedSimDALEntities)) {' . PHP_EOL; $output .= ' throw new Exception(__METHOD__ . \' called with invalid association name\');' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' return $this->_loadedSimDALEntities[$association_name];' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' private function _SimDALAssociationIsLoaded($association_name) {' . PHP_EOL; $output .= ' if (!array_key_exists($association_name, $this->_loadedSimDALEntities)) {' . PHP_EOL; $output .= ' throw new Exception(__METHOD__ . \' called with invalid association name\');' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' $this->_loadedSimDALEntities[$association_name] = true;' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' private function _getSession() {' . PHP_EOL; $output .= ' return $this->_session;' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' public function _SimDAL_setPrimaryKey($values, &$session) {' . PHP_EOL; $primary_key = $mapping->getPrimaryKey(); $output .= ' if ($session !== $this->_getSession()) {' . PHP_EOL; $output .= ' throw new Exception(__METHOD__ . \' called from outside of library scope\');' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' $this->' . $primary_key . ' = $values;' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' public function _SimDAL_diff($data) {' . PHP_EOL; $output .= ' $output = array();' . PHP_EOL; $output .= ' foreach ($this as $key=>$value) {' . PHP_EOL; $output .= ' if ($key == \'id\') {' . PHP_EOL; $output .= ' continue;' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' $method = \'get\' . ucfirst($key);' . PHP_EOL; $output .= ' if (method_exists($data, $method) && ((is_scalar($this->$key) || is_scalar($data->$method())) && (!is_null($this->$key) || !is_null($data->$method()))) && method_exists($data, $method)) {' . PHP_EOL; $output .= ' if ($this->$key != $data->$method()) {' . PHP_EOL; $output .= ' $output[$key] = $this->$key;' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' return $output;' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; $output .= ' public function _SimDAL_GetAssociation($identifier) {' . PHP_EOL; $output .= ' $mapping = $this->_getSession()->getMapper()->getMappingForEntityClass(get_class($this));' . PHP_EOL; $output .= ' return $mapping->getAssociation($identifier);' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' public function _SimDAL_SetReference($entity, $otherside_association) {' . PHP_EOL; $output .= ' $association = $otherside_association->getMatchingAssociationFromAssociationClass();' . PHP_EOL; $output .= ' $method = $association->getMethod();' . PHP_EOL; $output .= ' if ($association->isOneToMany()) {' . PHP_EOL; $output .= ' $getter = \'get\' . ucfirst($method);' . PHP_EOL; $output .= ' $this->$getter()->add($entity);' . PHP_EOL; $output .= ' } else {' . PHP_EOL; $output .= ' $setter = \'set\' . ucfirst($method);' . PHP_EOL; $output .= ' $property = $association->getProperty();' . PHP_EOL; $output .= ' if ($this->$property !== $entity) {' . PHP_EOL; $output .= ' $this->$setter($entity);' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' }' . PHP_EOL; $output .= ' }' . PHP_EOL . PHP_EOL; return $output; }
protected function _arrayForStorageFromEntity(SimDAL_Mapper_Entity $mapping, $entity, $includeNull = false, $transformData = false) { $array = array(); /* @var SimDAL_Mapper_Column */ foreach ($mapping->getColumns() as $key => $column) { if ($column->isPrimaryKey() && $column->isAutoIncrement()) { continue; } $method = 'get' . ucfirst($column->getProperty()); if (!method_exists($entity, $method)) { continue; } if (!$includeNull && is_null($entity->{$method}())) { continue; } if ($transformData) { $array[$column->getColumn()] = $this->transformData($column, $entity->{$method}(), $mapping); } else { $array[$column->getColumn()] = $entity->{$method}(); } } return $array; }
public function getTableColumns() { $this->_from->getColumns(); }