private function &_value_to_has_many_object($model, &$value) { $object = NULL; if (!isset($value) || is_mdinull($value)) { $object = new $model(); } else { if (is_object($value)) { if ($value->model != $model) { mdi::error(get_class($this) . '::_value_to_has_many_object' . ' - model type of value is different(' . $value->model . ')'); /// } $object =& $this->_modelarray_to_object($model, array($value)); } else { if (is_array($value)) { if (empty($value)) { $object = new $model(); } else { if (is_numeric_array($value)) { $object = new $model(); $object->where_in('id', $value)->get(); } else { $object =& $this->_modelarray_to_object($model, $value); } } } else { if (is_numeric($value)) { $object = new $model(); $object->where_in('id', array($value))->get(); } else { mdi::error(get_class($this) . '::_value_to_has_many_object' . ' - type that is not allowed in related field(' . gettype($value) . ')'); /// } } } } return $object; }
protected function _MDIRelation($object, &$outQueryDict) { $dbbuilder = self::getCI()->mdi->dbbuilder; $querydict_group_rel =& $dbbuilder->get_group($outQueryDict, 'RELATED'); $this_object = $object; $this_model = strtolower(get_class($this_object)); $this_table = $this_object->table; foreach (array('has_one', 'has_many') as $arr) { foreach ($this_object->{$arr} as $this_field => $this_field_attr) { $other_class = NULL; $other_field = NULL; $join_table = NULL; if (is_int($this_field)) { $this_field = $this_field_attr; $other_class = $this_field_attr; $other_field = $this_model; $this_field_attr = array('class' => $other_class, 'other_field' => $this_model, 'related_type' => $arr); } else { if (!empty($this_field_attr['class'])) { $other_class = $this_field_attr['class']; } else { mdi::error("DBSyncher::_MDIRelation Error - class did not defined" . "(field:" . $this_field . ")" . "(table:" . $this_table . ")"); /// } if (!empty($this_field_attr['other_field'])) { $other_field = $this_field_attr['other_field']; } else { $other_field = $this_model; } if (!empty($this_field_attr['join_table'])) { $join_table = $this_field_attr['join_table']; } //middle.fixme /* if (!empty($this_field_attr['model_path'])) { $other_class_path = $this_field_attr['model_path'].'/'.$other_class; } else { $other_class_path = strtolower($other_class); } */ $this_field_attr['related_type'] = $arr; } // other class model $other_object = new $other_class(NULL, TRUE); $other_object_related_fields = array(); $other_table = $other_object->table; DBSyncher::_getMDIRelationFields($other_object, $other_object_related_fields); if (!array_key_exists($other_field, $other_object_related_fields)) { mdi::error("DBSyncher::_MDIRelation Error - reverse relationship of field must exist at " . $other_class . " model" . "(field:" . $this_field . ")"); } else { $other_field_attr = $other_object_related_fields[$other_field]; } if (empty($other_field_attr['class']) || $other_field_attr['class'] !== $this_model) { mdi::error("DBSyncher::_MDIRelation Error - did not matched class of " . $other_field . " field in " . $other_class . " model " . "(field:" . $this_field . ")" . "(table:" . $this_table . ")"); /// } if (!empty($other_field_attr['other_field']) && $other_field_attr['other_field'] !== $this_field) { mdi::error("DBSyncher::_MDIRelation Error - current field did not matched 'other_field' option of " . $other_field . " field in " . $other_class . " model" . "(field:" . $this_field . ")" . "(table:" . $this_table . ")"); /// } if (!empty($other_field_attr['join_table'])) { if ($join_table == NULL) { mdi::error("DBSyncher::_MDIRelation Error - 'join_table' field did not matched " . $other_field . " field in " . $other_class . " model" . "(field:" . $this_field . ")" . "(table:" . $this_table . ")"); /// } } else { if ($join_table != NULL) { mdi::error("DBSyncher::_MDIRelation Error - 'join_table' field did not matched " . $other_field . " field in " . $other_class . " model" . "(field:" . $this_field . ")" . "(table:" . $this_table . ")"); ///// } } $join_table = $this->_getMDIJoinTable($this_object, $other_object, $this_field_attr); if ($this_model <= $other_class) { $target_object = $object; } else { $target_object = $other_object; } if (!empty($this_field_attr['join_self_as'])) { $this_db_field_name = $this_field_attr['join_self_as']; } else { $this_db_field_name = $other_field; } if (!empty($other_field_attr['join_self_as'])) { $other_db_field_name = $other_field_attr['join_self_as']; } else { $other_db_field_name = $this_field; } if ($arr == 'has_one') { if ($other_field_attr['related_type'] == 'has_one') { // oto /////////////////////////////////////////////////////////// if ($target_object === $object) { $new_querydict = $dbbuilder->make_field_querydict($this_table, $other_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } else { if ($dbbuilder->is_table_exists($target_object->table)) { if (!$dbbuilder->is_field_exists($other_table, $this_db_field_name . '_id')) { $new_querydict = $dbbuilder->make_field_querydict($other_table, $this_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } } else { // sync of other class will generate relation ID, instead of } } } else { // otm /////////////////////////////////////////////////////////// $new_querydict = $dbbuilder->make_field_querydict($this_table, $other_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } } else { if ($other_field_attr['related_type'] == 'has_one') { // m2o /////////////////////////////////////////////////////////// if ($dbbuilder->is_table_exists($other_table)) { if (!$dbbuilder->is_field_exists($other_table, $this_db_field_name . '_id')) { $new_querydict = $dbbuilder->make_field_querydict($other_table, $this_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } } else { // sync of other class will generate relation ID, instead of } } else { // m2m /////////////////////////////////////////////////////////// if ($dbbuilder->is_table_exists($join_table)) { if (!$dbbuilder->is_field_exists($join_table, $this_db_field_name . '_id')) { // add field $new_querydict = $dbbuilder->make_field_querydict($join_table, $this_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } if (!$dbbuilder->is_field_exists($join_table, $other_db_field_name . '_id')) { $new_querydict = $dbbuilder->make_field_querydict($join_table, $other_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } } else { // create table $new_querydict = $dbbuilder->make_table_querydict($join_table, 'CREATE', array('_IF_NOT_EXISTS' => TRUE)); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); // add field $new_querydict = $dbbuilder->make_field_querydict($join_table, $this_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); $new_querydict = $dbbuilder->make_field_querydict($join_table, $other_db_field_name . '_id', 'ADD', array('_OPTION' => 'COLUMN', '_DATATYPE' => 'KEY')); $querydict_group_rel = array_merge($querydict_group_rel, $new_querydict); } } } } } return TRUE; }
protected function _field_option_column($table, $label, $attribute) { if (!array_key_exists('_DATATYPE', $attribute)) { mdi::error('DBBuilder::_field_option_column Error - "_DATATYPE" key does not exist' . "(field:" . $label . ")" . "(table:" . $table . ")"); /// } $field = array(); $datatype = strtoupper($attribute['_DATATYPE']); switch ($datatype) { case 'KEY': $field['type'] = 'INT'; $field['unsigned'] = TRUE; break; case 'BOOLEAN': case 'SMALLINT': case 'BIGINT': case 'TINYINT': case 'INT': if ($datatype == 'BOOLEAN') { $field['type'] = 'INT'; $field['constraint'] = 1; $field['unsigned'] = TRUE; } else { $field['type'] = $attribute['_DATATYPE']; if ($attribute['_DATATYPE'] == 'INT') { if (array_key_exists('_DATASIZE', $attribute) && !empty($attribute['_DATASIZE'])) { $field['constraint'] = $attribute['_DATASIZE']; } } if (array_key_exists('_UNSIGNED', $attribute)) { if ($attribute['_UNSIGNED']) { $field['unsigned'] = TRUE; } } } break; case 'CHAR': case 'VARCHAR': if (!array_key_exists('_DATASIZE', $attribute) || empty($attribute['_DATASIZE'])) { mdi::error('DBBuilder::_field_option_column Error - "_DATASIZE" key does not exist' . "(field:" . $label . ")" . "(table:" . $table . ")"); /// } $field['type'] = $attribute['_DATATYPE']; $field['constraint'] = $attribute['_DATASIZE']; break; case 'TEXT': case 'DATE': case 'DATETIME': if (array_key_exists('_DATASIZE', $attribute) && !empty($attribute['_DATASIZE'])) { mdi::error('DBBuilder::_field_option_column Error - "_DATASIZE" key is deprecated' . "(field:" . $label . ")" . "(table:" . $table . ")"); /// } $field['type'] = $attribute['_DATATYPE']; break; default: mdi::error("DBBuilder::_field_option_column Error - unknown data type" . "(" . $datatype . ")" . "(field:" . $label . ")" . "(table:" . $table . ")"); /// } if (array_key_exists('_DEFAULT', $attribute)) { if (array_key_exists('_AUTO_INCREMENT', $attribute)) { mdi::error('DBBuilder::_field_option_column Error - You can not use the "DEFAULT" with "AUTO_INCREMENT" attribute at the same time' . "(" . $datatype . ")" . "(field:" . $label . ")" . "(table:" . $table . ")"); /// } $field['default'] = NULL; if (!is_null($attribute['_DEFAULT'])) { $default = $attribute['_DEFAULT']; switch ($datatype) { case 'TINYINT': case 'BOOLEAN': $default = $default ? 1 : 0; break; } $field['default'] = $default; } } if (array_key_exists('_AUTO_INCREMENT', $attribute)) { if ($attribute['_AUTO_INCREMENT']) { $field['auto_increment'] = TRUE; } } $field['null'] = TRUE; if (array_key_exists('_NULL', $attribute)) { if (!$attribute['_NULL']) { $field['null'] = FALSE; } } return $field; }
protected function _save_model_edit($model, $action) { $object = new $model(); $class = get_class($object); $save_object = NULL; if (is_numeric($action)) { $id = $action; $object->get_by_id($id); if (!$object->exists()) { mdi::error("MDI_Admin_Controller::_save_model_edit Error - Object did not found(id:{$id})"); /// } $save_object = $object; } else { if ($action == 'NEW') { $save_object = new $class(); } else { mdi::error("MDI_Admin_Controller::_save_model_edit Error - Unknown action({$action})"); /// } } if (is_request_method('POST')) { $save_object->set_fields($this->input->post()); } if ($save_object->save()) { return TRUE; } $save_object->skip_validation(FALSE); $this->error = $save_object->error->all; return FALSE; }
protected function _annotation_error($annotation, $message, $status = 500) { if (method_exists($this, '_annotation_error_' . $annotation)) { call_user_func_array(array(&$this, '_annotation_error_' . $annotation), array($annotation, $message, $status)); } else { mdi::error('request error : ' . $message . '(' . $annotation . ")", $status); } throw new MDI_ControllerAnnotation_Exception(); }
public function load($apps_name, $terminate = TRUE) { if (array_key_exists($apps_name, mdi::$is_loaded)) { return TRUE; } $filepath = strtolower(mdi::$PATH_APP . $apps_name . '.php'); if (!file_exists($filepath)) { if ($terminate) { mdi::error("mdi::load Error - " . "file not found" . "(" . $filepath . ")"); /// } return FALSE; } else { $class = $apps_name; if (($last_slash = strrpos($class, '/')) !== FALSE) { // The path is in front of the last slash //$path = substr($model, 0, $last_slash + 1); // And the model name behind it $class = substr($class, $last_slash + 1); } /** @noinspection PhpIncludeInspection */ include $filepath; if (class_exists($class)) { $varname = strtolower($class); $this->{$varname} = new $class(); } mdi::$is_loaded[$apps_name] = TRUE; } return TRUE; }