/** * Construct association object * * @param string $assocName * @param array $options * @param object $model */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'foreignKey', 'primaryKey', 'include', 'order', 'dependent' => 'nullify'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $toClass = ucfirst($toMethod); $this->_methods = array($toMethod => 'getObject', $toMethod . '=' => 'setObject', 'build' . $toClass => 'buildObject', 'create' . $toClass => 'createObject'); }
/** * Construct association object * * @param string $assocName * @param array $options * @param object $model */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'conditions', 'order', 'foreignKey', 'primaryKey', 'associationForeignKey', 'associationPrimaryKey', 'joinTable', 'uniq', 'include', 'finderSql', 'deleteSql', 'insertSql'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $singular = Mad_Support_Inflector::singularize($toMethod); $toClass = ucfirst($singular); $this->_methods = array($toMethod => 'getObjects', $toMethod . '=' => 'setObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Ids=' => 'setObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('replace' . $toClass) => 'replaceObjects', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects'); }
public function testDisconnect() { $this->_connect(); $this->assertTrue(Mad_Model_Base::isConnected()); $this->_disconnect(); $this->assertFalse(Mad_Model_Base::isConnected()); }
/** * Proxy methods over to the connection * @param string $method * @param array $args */ public function __call($method, $args) { foreach ($args as $arg) { if (is_array($arg)) { $vals = array(); foreach ($arg as $key => $value) { $vals[] = "{$key} => " . var_export($value, true); } $a[] = 'array(' . join(', ', $vals) . ')'; } else { $a[] = $arg; } } $this->say("{$method}(" . join(", ", $a) . ")"); // benchmark method call $t = new Horde_Support_Timer(); $t->push(); $connection = Mad_Model_Base::connection(); $result = call_user_func_array(array($connection, $method), $args); $time = $t->pop(); // print stats $this->say(sprintf("%.4fs", $time), 'subitem'); if (is_int($result)) { $this->say("{$result} rows", 'subitem'); } return $result; }
/** * Construct association object * * @param string $assocName * @param array $options */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'foreignKey', 'associationForeignKey', 'primaryKey', 'associationPrimaryKey', 'include', 'select', 'conditions', 'order', 'finderSql', 'through', 'dependent' => 'nullify'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // throw fatal error if through option is invalid $this->_throughClass = Mad_Support_Inflector::classify($this->_options['through']); class_exists($this->_throughClass); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $singular = Mad_Support_Inflector::singularize($toMethod); $toClass = ucfirst($singular); $this->_methods = array($toMethod => 'getObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects'); }
/** * Instantiate the model with the attributes * @param array $row * @return object */ public function instantiate($row) { if (!isset($this->_cachedRecord[$this->recordId($row)])) { $model = $this->_model->instantiate($this->extractRecord($row)); $this->_cachedRecord[$this->recordId($row)] = $model; } return $this->_cachedRecord[$this->recordId($row)]; }
/** * Returns all the full error messages in an array. * * @return array */ public function fullMessages() { $fullMessages = array(); foreach ($this->_errors as $attr => $messages) { foreach ($messages as $msg) { if ($attr != 'base') { $msg = $this->_base->humanAttributeName($attr) . ' ' . $msg; } $fullMessages[] = $msg; } } return $fullMessages; }
public function testToJsonIncludeRoot() { Mad_Model_Base::$includeRootInJson = true; $record = $this->users('mike'); $options = array('only' => 'name'); $expected = '{ "user": {"name":"Mike Naberezny"} }'; try { $this->assertEquals($expected, $record->toJson($options)); } catch (Mad_Model_Exception $e) { if (function_exists('json_encode')) { throw $e; } else { $this->assertRegExp('/json_encode/', $e->getMessage()); } } }
/** * Finish up config by connecting to db */ public function end() { // set config for each component foreach ((array) $this->controller as $key => $value) { Mad_Controller_Base::${$key} = $value; } foreach ((array) $this->mailer as $key => $value) { Mad_Mailer_Base::${$key} = $value; } foreach ((array) $this->view as $key => $value) { Mad_View_Base::${$key} = $value; } foreach ((array) $this->model as $key => $value) { Mad_Model_Base::${$key} = $value; } foreach ((array) $this->test as $key => $value) { Mad_Test_Base::${$key} = $value; } // database connection Mad_Model_Base::establishConnection(MAD_ENV); }
/** * Initialize result set into object collection * * @param object $model * @param array $results */ protected function _initResults(Mad_Model_Base $model, $results) { while ($row = current($results)) { $this->_collection[] = $model->instantiate($row); next($results); } }
/** * Returns the connection currently associated with the class. This can * also be used to "borrow" the connection to do database work unrelated * to any of the specific Active Records. * * @return Mad_Model_ConnectionAdapter_Abstract */ public static function connection() { if (self::$_activeConnection) { return self::$_activeConnection; } else { return self::$_activeConnection = self::retrieveConnection(); } }
/** * Construct associations for model from record/row * * @param object $record * @param object $join * @param array $row */ protected function _constructAssociation(Mad_Model_Base $record, Mad_Model_Join_Base $join, $row) { // set that we've loaded this association $record->setAssociationLoaded($join->reflection()->getAssocName()); if ($record->id != $join->parent()->recordId($row) || empty($row[$join->aliasedPrimaryKey()])) { return; } $association = $join->instantiate($row); $macro = $join->reflection()->getMacro(); $singular = Mad_Support_Inflector::singularize($join->reflection()->getAssocName()); if ($macro == 'hasAndBelongsToMany' || $macro == 'hasMany' || $macro == 'hasManyThrough') { $addMethod = Mad_Support_Inflector::camelize('add' . ucfirst($singular), 'lower'); $addMethod = str_replace('/', '_', $addMethod); // make sure object isn't already included $getter = Mad_Support_Inflector::camelize($join->reflection()->getAssocName(), 'lower'); $getter = str_replace('/', '_', $getter); $exists = array(); foreach ($record->{$getter} as $val) { $exists[] = $val->id; } if (!in_array($association->id, $exists)) { $record->{$addMethod}($association); } } elseif ($macro == 'belongsTo' || $macro == 'hasOne') { $assignMethod = Mad_Support_Inflector::camelize($singular, 'lower'); $record->{$assignMethod} = $association; } return $association; }
public function testEstablishConnectionArray() { $conn = Mad_Model_Base::establishConnection(array()); }
/** * Disconnect from the database. */ protected function _disconnect() { // run 'teardown' sql from the fixture if ($this->_fixtures) { $this->_fixtures->teardown(); } if (Mad_Model_Base::isConnected()) { Mad_Model_Base::removeConnection(); } $this->_conn = null; }
/** * @param integer $version */ protected function _setSchemaVersion($version) { $version = $this->_isDown() ? $version - 1 : $version; $sql = "UPDATE schema_info SET version = " . (int) $version; Mad_Model_Base::connection()->update($sql); }
/** * Disconnect from the database. */ protected function _disconnect() { if (Mad_Model_Base::isConnected()) { $this->_connection->disconnect(); } }