function installAndIncludeModels($models = array()) { $args = func_get_args(); $models = !empty($args) ? (is_array($args[0]) ? $args[0] : (count($args) > 1 ? $args : Ak::toArray($args[0]))) : array(); $default_options = array('instantiate' => true); $options = is_array($models[count($models)-1]) ? array_pop($models) : array(); $options = array_merge($default_options, $options); foreach ($models as $model){ require_once(AK_APP_DIR.DS.'installers'.DS.(empty($this->module)?'':$this->module.DS).AkInflector::underscore($model).'_installer.php'); require_once(AK_MODELS_DIR.DS.AkInflector::underscore($model).'.php'); $installer_name = $model.'Installer'; $installer = new $installer_name(); $installer->uninstall(); $installer->install(); if($this->insert_models_data || !empty($options['populate'])){ $this->populateTables(AkInflector::tableize($model)); } if($this->instantiate_models || !empty($options['instantiate'])){ $this->instantiateModel($model); } } if(isset($_SESSION['__activeRecordColumnsSettingsCache'])){ unset($_SESSION['__activeRecordColumnsSettingsCache']); } }
function _preloadPaths() { $this->class_name = AkInflector::camelize($this->class_name); $this->assignVarToTemplate('class_name', $this->class_name); $this->table_name = AkInflector::tableize($this->class_name); $this->underscored_model_name = AkInflector::underscore($this->class_name); $this->model_path = 'app' . DS . 'models' . DS . $this->underscored_model_name . '.php'; $this->installer_path = 'app' . DS . 'installers' . DS . $this->underscored_model_name . '_installer.php'; }
function _createNewTestingModelDatabase($test_model_name) { static $shutdown_called; // Create a data dictionary object, using this connection $db =& AK::db(); //$db->debug = true; $table_name = AkInflector::tableize($test_model_name); if (in_array($table_name, (array) $db->MetaTables())) { return false; } switch ($table_name) { case 'ak_test_people': $table = array('table_name' => 'ak_test_people', 'fields' => 'id I AUTO KEY, user_name C(32), first_name C(200), last_name C(200), phone_number I(18), city C(40), state C(40), email C(150), country C(2), sex C(1), birth T, age I(3), password C(32), tos L(1), score I(3), comments X, created_at T, updated_at T, expires T', 'index_fileds' => 'id', 'table_options' => array('mysql' => 'TYPE=InnoDB', 'REPLACE')); break; default: return false; break; } $dict = NewDataDictionary($db); $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); $dict->ExecuteSQLArray($sqlarray); if (isset($table['index_fileds'])) { $sqlarray = $dict->CreateIndexSQL('idx_' . $table['table_name'], $table['table_name'], $table['index_fileds']); $dict->ExecuteSQLArray($sqlarray); } $db->CreateSequence('seq_' . $table['table_name']); $this->_testing_model_databases_to_delete[] = $table_name; if (!isset($shutdown_called)) { $shutdown_called = true; register_shutdown_function(array(&$this, '_deleteTestingModelDatabases')); } //$db->debug = false; return true; }
function _createNewTestingModelDatabase($test_model_name) { static $shutdown_called; // Create a data dictionary object, using this connection $db =& AK::db(); //$db->debug = true; $table_name = AkInflector::tableize($test_model_name); if(in_array($table_name, (array)$db->MetaTables())){ return false; } switch ($table_name) { case 'ak_test_todo_items': $table = array( 'table_name' => 'ak_test_todo_items', 'fields' => 'id I AUTO KEY, position I(20), task X, due_time T, created_at T, expires T, updated_at T, new_position I(10)', 'index_fileds' => 'id', 'table_options' => array('mysql' => 'TYPE=InnoDB', 'REPLACE') ); break; default: return false; break; } $dict = NewDataDictionary($db); $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); $dict->ExecuteSQLArray($sqlarray); if(isset($table['index_fileds'])){ $sqlarray = $dict->CreateIndexSQL('idx_'.$table['table_name'], $table['table_name'], $table['index_fileds']); $dict->ExecuteSQLArray($sqlarray); } $db->CreateSequence('seq_'.$table['table_name']); $this->_testing_model_databases_to_delete[] = $table_name; if(!isset($shutdown_called)){ $shutdown_called = true; register_shutdown_function(array(&$this,'_deleteTestingModelDatabases')); } //$db->debug = false; return true; }
function _setupCloner() { $this->clone_setup_done = true; $this->class_to_clone = AkInflector::underscore($this->class_to_clone); $this->class_name = AkInflector::underscore($this->class_name); $this->clone_replacements = array(AkInflector::camelize($this->class_to_clone) . 'Controller' => AkInflector::camelize($this->class_name) . 'Controller', AkInflector::humanize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::humanize(AkInflector::pluralize($this->class_name)), AkInflector::titleize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::titleize(AkInflector::pluralize($this->class_name)), AkInflector::humanize($this->class_to_clone) => AkInflector::humanize($this->class_name), AkInflector::titleize($this->class_to_clone) => AkInflector::titleize($this->class_name), AkInflector::camelize(AkInflector::pluralize($this->class_to_clone)) => AkInflector::camelize(AkInflector::pluralize($this->class_name)), AkInflector::pluralize($this->class_to_clone) => AkInflector::pluralize($this->class_name), AkInflector::camelize($this->class_to_clone) => AkInflector::camelize($this->class_name), $this->class_to_clone => $this->class_name); //AK_VIEWS_DIR.DS.AkInflector::underscore($this->class_name).DS.$action.'.tpl' $this->files_to_clone = array(AkInflector::toModelFilename($this->class_to_clone) => AkInflector::toModelFilename($this->class_name), AK_TEST_DIR . DS . 'unit' . DS . 'test_' . $this->class_to_clone . '.php' => AK_TEST_DIR . DS . 'unit' . DS . 'test_' . $this->class_name . '.php', AK_TEST_DIR . DS . 'fixtures' . DS . AkInflector::tableize($this->class_to_clone) . '.yml' => AK_TEST_DIR . DS . 'fixtures' . DS . AkInflector::tableize($this->class_name) . '.yml', AkInflector::toControllerFilename($this->class_to_clone) => AkInflector::toControllerFilename($this->class_name), AK_TEST_DIR . DS . 'functional' . DS . 'test_' . AkInflector::camelize($this->class_to_clone . '_controller') . '.php' => AK_TEST_DIR . DS . 'functional' . DS . 'test_' . AkInflector::camelize($this->class_name . '_controller') . '.php', AK_HELPERS_DIR . DS . AkInflector::underscore("{$this->class_to_clone}_helper") . '.php' => AK_HELPERS_DIR . DS . AkInflector::underscore("{$this->class_name}_helper") . '.php'); foreach ($this->_getControllerViews() as $view_file) { $this->files_to_clone[AK_VIEWS_DIR . DS . $this->class_to_clone . DS . $view_file . '.tpl'] = AK_VIEWS_DIR . DS . $this->class_name . DS . $view_file . '.tpl'; } $this->files_to_clone[AK_VIEWS_DIR . DS . 'layouts' . DS . $this->class_to_clone . '.tpl'] = AK_VIEWS_DIR . DS . 'layouts' . DS . $this->class_name . '.tpl'; foreach (Ak::dir(AK_APP_DIR . DS . 'locales' . DS . $this->class_to_clone, array('dirs' => false)) as $locale_file) { $this->files_to_clone[AK_APP_DIR . DS . 'locales' . DS . $this->class_to_clone . DS . $locale_file] = AK_APP_DIR . DS . 'locales' . DS . $this->class_name . DS . $locale_file; } }
function &addAssociated($association_id, $options = array()) { $default_options = array('class_name' => empty($options['class_name']) ? AkInflector::camelize($association_id) : $options['class_name'], 'foreign_key' => empty($options['foreign_key']) ? AkInflector::singularize($this->Owner->getTableName()) . '_id' : $options['foreign_key'], 'remote' => false, 'instantiate' => false, 'conditions' => false, 'include_conditions_when_included' => true, 'order' => false, 'include_order_when_included' => true, 'dependent' => false, 'counter_cache' => false); $options = array_merge($default_options, $options); $options['table_name'] = empty($options['table_name']) ? AkInflector::tableize($options['class_name']) : $options['table_name']; $this->setOptions($association_id, $options); $this->addModel($association_id, new AkAssociatedActiveRecord()); $associated =& $this->getModel($association_id); $this->setAssociatedId($association_id, $associated->getId()); $associated =& $this->_build($association_id, &$associated, false); $this->_saveLoadedHandler($association_id, $associated); if ($options['instantiate']) { $associated =& $this->addModel($association_id, new $options['class_name']($options['foreign_key'] . ' = ' . $this->Owner->quotedId())); } return $associated; }
function clear($table, $environment = AK_ENVIRONMENT) { $modelName = AkInflector::singularize(AkInflector::classify($table)); $cacheFileName = AkDbSchemaCache::_generateCacheFileName($modelName, $environment); //echo "Cleaning cache: $cacheFileName\n"; if (file_exists($cacheFileName)) { @unlink($cacheFileName); } AkDbSchemaCache::_get($modelName, $environment, false, false); $tableName = AkInflector::tableize($table); $databaseInternalsFileName = AkDbSchemaCache::_generateCacheFileName('database_table_internals_' . $tableName); //echo "Cleaning cache: $databaseInternalsFileName\n"; if (file_exists($databaseInternalsFileName)) { @unlink($databaseInternalsFileName); } AkDbSchemaCache::_get('database_table_internals_' . $tableName, $environment, false, false); }
public function _preloadPaths() { if (!isset($this->active_document)) { $this->active_document = isset($this->activedocument) || isset($this->ad) || isset($this->ActiveDocument); } $this->class_name = AkInflector::camelize($this->class_name); $this->table_columns = trim(join(' ', (array) @$this->table_columns)); $this->assignVarToTemplate('table_columns', $this->table_columns); $this->table_name = AkInflector::tableize($this->class_name); $this->underscored_model_name = AkInflector::underscore($this->class_name); $this->model_path = 'app' . DS . 'models' . DS . $this->underscored_model_name . '.php'; $this->installer_path = 'app' . DS . 'installers' . DS . $this->underscored_model_name . '_installer.php'; $this->test_file_name = AkInflector::underscore($this->class_name) . '_test.php'; $this->assignVarToTemplate('class_name', $this->class_name); $this->assignVarToTemplate('test_file_name', $this->test_file_name); $this->assignVarToTemplate('model_type', $this->active_document ? 'AkActiveDocument' : 'ActiveRecord'); }
private function _setDefaults() { $this->class_name = AkInflector::camelize($this->class_name); $this->table_name = AkInflector::tableize($this->class_name); $this->file_name = AkInflector::underscore($this->class_name); $this->controller_class_name = AkInflector::pluralize($this->class_name); $this->controller_file_name = AkInflector::toControllerFilename($this->controller_class_name); $this->table_columns = trim(join(' ', (array) @$this->table_columns)); $this->assignVarToTemplate('attributes', $this->_getModelAttributesForViews()); $this->assignVarToTemplate('class_name', $this->class_name); $this->assignVarToTemplate('table_name', $this->table_name); $this->assignVarToTemplate('plural_name', $this->table_name); $this->assignVarToTemplate('singular_name', AkInflector::singularize($this->table_name)); $this->assignVarToTemplate('file_name', $this->file_name); $this->assignVarToTemplate('table_columns', $this->table_columns); $this->assignVarToTemplate('controller_class_name', $this->controller_class_name); }
public function test_simple_tableize() { $this->assertEqual('Pictures',AkInflector::pluralize('Picture')); $this->assertEqual('pictures',AkInflector::tableize('picture')); }
/** * This method retrieves current class name that will be used to map * your database to this object. */ function getClassForDatabaseTableMapping() { $class_name = get_class($this); if(is_subclass_of($this,'akactiverecord') || is_subclass_of($this,'AkActiveRecord')){ $parent_class = get_parent_class($this); while (substr(strtolower($parent_class),-12) != 'activerecord'){ $class_name = $parent_class; $parent_class = get_parent_class($parent_class); } } $class_name = $this->_getModelName($class_name); // This is an Active Record Inheritance so we set current table to parent table. if(!empty($class_name) && strtolower($class_name) != 'activerecord'){ $this->_inheritanceClassName = $class_name; @$this->setTableName(AkInflector::tableize($class_name), false); } return $class_name; }
function findFixtureForModel($model_name) { $fixture_file_name = AkInflector::tableize($model_name) . '.yaml'; $include_path = array(AK_PHPUNIT_TESTSUITE_FIXTURES, AK_APP_DIR . DS . 'data'); return PHPUnit_Akelos_autoload::searchFilenameInPath($include_path, $fixture_file_name); }
function Test_of_tableize() { foreach ($this->ClassNameToTableName as $class_name => $table_name) { $this->assertEqual($table_name, AkInflector::tableize($class_name)); } }
public function tableize() { return AkInflector::tableize($this->value); }
function &addAssociated($association_id, $options = array()) { $default_options = array('class_name' => empty($options['class_name']) ? AkInflector::modulize($association_id) : $options['class_name'], 'table_name' => false, 'join_table' => false, 'join_class_name' => false, 'foreign_key' => false, 'association_foreign_key' => false, 'conditions' => false, 'order' => false, 'join_class_extends' => 'AkActiveRecord', 'finder_sql' => false, 'delete_sql' => false, 'insert_sql' => false, 'include' => false, 'group' => false, 'limit' => false, 'offset' => false, 'handler_name' => strtolower(AkInflector::underscore(AkInflector::singularize($association_id))), 'select' => false, 'instantiate' => false); $options = array_merge($default_options, $options); $owner_name = $this->Owner->getModelName(); $owner_table = $this->Owner->getTableName(); $associated_name = $options['class_name']; $associated_table_name = $options['table_name'] = empty($options['table_name']) ? AkInflector::tableize($associated_name) : $options['table_name']; $join_tables = array($owner_table, $associated_table_name); sort($join_tables); $options['join_table'] = empty($options['join_table']) ? join('_', $join_tables) : $options['join_table']; $options['join_class_name'] = empty($options['join_class_name']) ? join(array_map(array('AkInflector', 'modulize'), array_map(array('AkInflector', 'singularize'), $join_tables))) : $options['join_class_name']; $options['foreign_key'] = empty($options['foreign_key']) ? AkInflector::underscore($owner_name) . '_id' : $options['foreign_key']; $options['association_foreign_key'] = empty($options['association_foreign_key']) ? AkInflector::underscore($associated_name) . '_id' : $options['association_foreign_key']; $Collection =& $this->_setCollectionHandler($association_id, $options['handler_name']); $Collection->setOptions($association_id, $options); $this->addModel($association_id, $Collection); if ($options['instantiate']) { $associated =& $Collection->load(); } $this->setAssociatedId($association_id, $options['handler_name']); $Collection->association_id = $association_id; $Collection->_loadJoinObject() ? null : trigger_error(Ak::t('Could find join model %model_name for hasAndBelongsToMany association %id', array('%table_name' => $options['join_class_name'], 'id' => $this->association_id)), E_USER_ERROR); return $Collection; }
function setTableName($table_name = null, $check_for_existence = AK_ACTIVE_RECORD_VALIDATE_TABLE_NAMES, $check_mode = false) { static $available_tables; if (empty($table_name)) { $table_name = AkInflector::tableize($this->getModelName()); } if ($check_for_existence) { if (!isset($available_tables) || $check_mode) { if (!isset($this->_db)) { $this->setConnection(); } if (empty($_SESSION['__activeRecordColumnsSettingsCache']['available_tables']) || AK_ENVIRONMENT != 'development' && !defined('AK_AVOID_ACTIVE_RECORD_DB_SCHEMA_CACHE')) { $_SESSION['__activeRecordColumnsSettingsCache']['available_tables'] = $this->_db->MetaTables(); } $available_tables = $_SESSION['__activeRecordColumnsSettingsCache']['available_tables']; } if (!in_array($table_name, (array) $available_tables)) { if (!$check_mode) { trigger_error(Ak::t('Unable to set "%table_name" table for the model "%model".' . ' There is no "%table_name" available into current database layout.' . ' Set AK_ACTIVE_RECORD_VALIDATE_TABLE_NAMES constant to false in order to' . ' avoid table name validation', array('%table_name' => $table_name, '%model' => $this->getModelName())), E_USER_WARNING); } return false; } } $this->_tableName = $table_name; return true; }
function __fix_for_PHP4($model_name) { $table_name = AkInflector::tableize($model_name); return "function {$model_name}()\r\n{\r\n \$this->setModelName('{$model_name}');\r\n \$attributes = (array)func_get_args();\r\n \$this->setTableName('{$table_name}');\r\n \$this->init(\$attributes);\r\n}"; }
public function &addAssociated($association_id, $options = array()) { $default_options = array('class_name' => empty($options['class_name']) ? AkInflector::classify($association_id) : $options['class_name'], 'table_name' => false, 'join_table' => false, 'join_class_name' => false, 'foreign_key' => false, 'association_foreign_key' => false, 'conditions' => false, 'order' => false, 'join_class_extends' => AK_HAS_AND_BELONGS_TO_MANY_JOIN_CLASS_EXTENDS, 'join_class_primary_key' => 'id', 'finder_sql' => false, 'delete_sql' => false, 'insert_sql' => false, 'include' => false, 'group' => false, 'limit' => false, 'offset' => false, 'handler_name' => strtolower(AkInflector::underscore(AkInflector::singularize($association_id))), 'select' => false, 'instantiate' => false, 'unique' => false); $options = array_merge($default_options, $options); $owner_name = $this->Owner->getModelName(); $owner_table = $this->Owner->getTableName(); $associated_name = $options['class_name']; $associated_table_name = $options['table_name'] = empty($options['table_name']) ? AkInflector::tableize($associated_name) : $options['table_name']; $join_tables = array($owner_table, $associated_table_name); sort($join_tables); $options['join_table'] = empty($options['join_table']) ? join('_', $join_tables) : $options['join_table']; $options['join_class_name'] = empty($options['join_class_name']) ? join(array_map(array('AkInflector', 'classify'), array_map(array('AkInflector', 'singularize'), $join_tables))) : $options['join_class_name']; $options['foreign_key'] = empty($options['foreign_key']) ? AkInflector::underscore($owner_name) . '_id' : $options['foreign_key']; $options['association_foreign_key'] = empty($options['association_foreign_key']) ? AkInflector::underscore($associated_name) . '_id' : $options['association_foreign_key']; $Collection =& $this->_setCollectionHandler($association_id, $options['handler_name']); $Collection->setOptions($association_id, $options); $this->addModel($association_id, $Collection); if ($options['instantiate']) { $associated =& $Collection->load(); } $this->setAssociatedId($association_id, $options['handler_name']); $Collection->association_id = $association_id; $Collection->_loadJoinObject(); return $Collection; }
public function uninstallModel($model) { $this->log('Uninstalling model:' . $model); if (!$this->uninstallMigration($model)) { $table_name = AkInflector::tableize($model); $installer = new AkInstaller(); $installer->skip_db_sql = true; $installer->dropTable($table_name, array('sequence' => true)); } }
public function getCollectionName() { if (empty($this->_internals['collection_name'])) { $this->_internals['collection_name'] = AkInflector::tableize($this->getModelName()); } return $this->_internals['collection_name']; }
function __fix_for_PHP4($model_name) { $table_name = AkInflector::tableize($model_name); return "function $model_name() { \$this->setModelName('$model_name'); \$attributes = (array)func_get_args(); \$this->setTableName('$table_name'); \$this->init(\$attributes); }"; }
function _createNewTestingModelDatabase($test_model_name) { static $shutdown_called; // Create a data dictionary object, using this connection $db =& AK::db(); //$db->debug = true; $table_name = AkInflector::tableize($test_model_name); if(in_array($table_name, (array)$db->MetaTables())){ return false; } switch ($table_name) { case 'ak_test_users': $table = array( 'table_name' => 'ak_test_users', 'fields' => 'id I AUTO KEY, user_name C(32), first_name C(200), last_name C(200), email C(150), country I, password C(32), created_at T, updated_at T, expires_on T', 'index_fileds' => 'id', 'table_options' => array('mysql' => 'TYPE=InnoDB', 'REPLACE') ); break; case 'ak_test_members': $table = array( 'table_name' => 'ak_test_members', 'fields' => 'ak_test_user_id I, role C(25)', 'table_options' => array('mysql' => 'TYPE=InnoDB', 'REPLACE') ); break; case 'ak_test_comments': $table = array( 'table_name' => 'ak_test_comments', 'fields' => 'id I AUTO KEY, ak_test_user_id I, private_comment L(1), birth_date T', 'index_fileds' => 'id', 'table_options' => array('mysql' => 'TYPE=InnoDB', 'REPLACE') ); break; case 'ak_test_fields': $table = array( 'table_name' => 'ak_test_fields', 'fields' => 'id I AUTO KEY, varchar_field C(255), longtext_field XL, text_field X, logblob_field B, date_field D, datetime_field T, tinyint_field L(2), integer_field I, smallint_field I2, bigint_field I8, double_field F, numeric_field N, bytea_field B, timestamp_field T, boolean_field L(1), int2_field I2, int4_field I4, int8_field I8, foat_field F, varchar4000_field X, clob_field XL, nvarchar2000_field X2, blob_field B, nvarchar_field C2(255), decimal1_field L(2), decimal3_field I1, decimal5_field I2, decimal10_field I4, decimal20_field I8, decimal_field N, created_at T, updated_at T, expires_on T', 'index_fileds' => 'id', 'table_options' => array('mysql' => 'TYPE=InnoDB', 'REPLACE') ); break; default: return false; break; } $dict = NewDataDictionary($db); $sqlarray = $dict->CreateTableSQL($table['table_name'], $table['fields'], $table['table_options']); $dict->ExecuteSQLArray($sqlarray); if(isset($table['index_fileds'])){ $sqlarray = $dict->CreateIndexSQL('idx_'.$table['table_name'], $table['table_name'], $table['index_fileds']); $dict->ExecuteSQLArray($sqlarray); } strstr($db->databaseType,'sqlite') ? $db->CreateSequence('seq_'.$table['table_name']) : null; $this->_testing_model_databases_to_delete[] = $table_name; if(!isset($shutdown_called)){ $shutdown_called = true; register_shutdown_function(array(&$this,'_deleteTestingModelDatabases')); } //$db->debug = false; return true; }
/** * This method retrieves current class name that will be used to map * your database to this object. */ protected function _getClassForDatabaseTableMapping() { $class_name = get_class($this); if ($this instanceof AkActiveRecord) { $parent_class = get_parent_class($this); while (substr($parent_class, -12) != 'ActiveRecord') { $class_name = $parent_class; $parent_class = get_parent_class($parent_class); } } // This is an Active Record Inheritance so we set current table to parent table. if (!empty($class_name) && $class_name != 'ActiveRecord') { $this->_inheritanceClassName = $class_name; @$this->setTableName(AkInflector::tableize($class_name), false); } return $class_name; }
function createVersioningModel() { $class_name = $this->options['class_name']; $table_name = AkInflector::tableize($class_name); eval(" class $class_name extends ActiveRecord { var \$_recordTimestamps = false; var \$_avoidTableNameValidation = true; function $class_name() { \$this->setModelName('$class_name'); \$attributes = (array)func_get_args(); \$this->setTableName('$table_name', true, true); \$this->init(\$attributes); } function getVersionedAttributes() { if(!empty(\$this->VersionHandler)){ return \$this->VersionHandler->getFilteredAttributesFromVersion(parent::getAttributes()); } return parent::getAttributes(); } function &getPrevious() { \$Version =& \$this->_getPreviousOrNext(); return \$Version; } function &getNext() { \$Version =& \$this->_getPreviousOrNext(false); return \$Version; } function &_getPreviousOrNext(\$previous = true) { \$Version = false; if(!empty(\$this->VersionHandler)){ \$this->VersionHandler->load(); \$Owner =& \$this->VersionHandler->_ActiveRecordInstance; \$versions =& \$Owner->versions; \$options =& \$this->VersionHandler->options; \$version_keys = array_keys(\$versions); if(!\$previous){ rsort(\$version_keys); } foreach (\$version_keys as \$k){ if(\$versions[\$k]->get(\$options['version_column']) == \$this->get(\$options['version_column'])){ return \$Version; } \$Version =& \$versions[\$k]; } } return \$Version; } } "); }