Example #1
0
 /**
  * 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');
 }
Example #2
0
 /**
  * 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');
 }
Example #3
0
 public function testDisconnect()
 {
     $this->_connect();
     $this->assertTrue(Mad_Model_Base::isConnected());
     $this->_disconnect();
     $this->assertFalse(Mad_Model_Base::isConnected());
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 /**
  * 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');
 }
Example #6
0
 /**
  * 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)];
 }
Example #7
0
 /**
  * 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;
 }
Example #8
0
 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());
         }
     }
 }
Example #9
0
 /**
  * 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);
 }
Example #10
0
 /**
  * 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);
     }
 }
Example #11
0
 /**
  * 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();
     }
 }
Example #12
0
 /**
  * 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;
 }
Example #13
0
 public function testEstablishConnectionArray()
 {
     $conn = Mad_Model_Base::establishConnection(array());
 }
Example #14
0
 /**
  * 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;
 }
Example #15
0
 /**
  * @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);
 }
Example #16
0
 /**
  * Disconnect from the database. 
  */
 protected function _disconnect()
 {
     if (Mad_Model_Base::isConnected()) {
         $this->_connection->disconnect();
     }
 }