/** * Construct new Join Association. * * @param object $reflection * @param object $joinDependency * @param object $parent */ public function __construct(Mad_Model_Association_Base $reflection, Mad_Model_Join_Dependency $joinDependency, Mad_Model_Join_Base $parent) { parent::__construct($reflection->getAssocModel()); $this->_parent = $parent; $this->_reflection = $reflection; $this->_aliasedPrefix = 'T' . sizeof($joinDependency->joins()); $this->_aliasedTableName = $this->tableName(); // start with table name $this->_parentTableName = $parent->model()->tableName(); // if the table name has been used, then use an alias $alias = $this->_aliasedTableName; if ($joinDependency->tableAlias($this->_aliasedTableName)) { $alias = $reflection->getAssocTable() . "_{$this->_parentTableName}"; $this->_aliasedTableName = $this->_model->connection->tableAliasFor($alias); // make sure to get a unique name. careful of name restrictions $alias = $this->_aliasedTableName; $i = $joinDependency->tableAlias($alias); if ($i > 0) { $maxLen = $this->_model->connection->tableAliasLength() - 3; $this->_aliasedTableName = substr($this->_aliasedTableName, 0, $maxLen) . "_" . ($i + 1); } } $joinDependency->addTableAlias($alias); // create alias for join table (only should be executed for hasAndBelongsToMany|belongsTo if ($this->_aliasedJoinTableName = $this->_reflection->getJoinTable()) { if ($joinDependency->tableAlias($this->_aliasedJoinTableName)) { $alias = $reflection->getAssocTable() . '_' . $this->_parentTableName . '_join'; $this->_aliasedJoinTableName = $this->_model->connection->tableAliasFor($alias); // make sure to get a unique name. careful of oracle name restrictions $i = $joinDependency->tableAlias($this->_aliasedJoinTableName); if ($i > 0) { $maxLen = $this->_model->connection->tableAliasLength() - 3; $this->_aliasedJoinTableName = substr($this->_aliasedJoinTableName, 0, $maxLen) . "_" . ($i + 1); } } $joinDependency->addTableAlias($this->_aliasedJoinTableName); } }
/** * Associations are lazy initialized as needed. This function is called when needed * to check if we need an association method */ protected function _initAssociations() { // only initialize if we haven't already if (!isset($this->_associationMethods) && isset($this->_associationList)) { // loop thru each define association foreach ($this->_associationList as $associationId => $info) { list($type, $options) = $info; $association = Mad_Model_Association_Base::factory($type, $associationId, $options, $this); $this->_associations[$associationId] = $association; // add list of dynamic methods this association adds foreach ($association->getMethods() as $methodName => $methodCall) { $this->_associationMethods[$methodName] = $association; } } } }
public function testSetChanged() { $options = array(); $assoc = Mad_Model_Association_Base::factory('belongsTo', 'User', $options, new Article()); $assoc->setChanged(true); $this->assertTrue($assoc->isChanged()); $assoc->setChanged(false); $this->assertFalse($assoc->isChanged()); }
public function testGetMethods() { $assoc = Mad_Model_Association_Base::factory('hasMany', 'Tags', array('through' => 'Taggings'), new Article()); $expected = array('tags' => 'getObjects', 'tagIds' => 'getObjectIds', 'tagCount' => 'getObjectCount', 'addTag' => 'addObject', 'deleteTags' => 'deleteObjects', 'clearTags' => 'clearObjects', 'findTags' => 'findObjects'); $this->assertEquals($expected, $assoc->getMethods()); }
public function testGetMethods() { $assoc = Mad_Model_Association_Base::factory('belongsTo', 'User', array(), new Article()); $expected = array('user' => 'getObject', 'user='******'setObject', 'buildUser' => 'buildObject', 'createUser' => 'createObject'); $this->assertEquals($expected, $assoc->getMethods()); }
public function testGetMethods() { $assoc = Mad_Model_Association_Base::factory('hasAndBelongsToMany', 'Categories', array(), new Category()); $expected = array('categories' => 'getObjects', 'categories=' => 'setObjects', 'categoryIds' => 'getObjectIds', 'categoryIds=' => 'setObjectIds', 'categoryCount' => 'getObjectCount', 'addCategory' => 'addObject', 'replaceCategories' => 'replaceObjects', 'deleteCategories' => 'deleteObjects', 'clearCategories' => 'clearObjects', 'findCategories' => 'findObjects'); $this->assertEquals($expected, $assoc->getMethods()); }
public function testGetMethods() { $assoc = Mad_Model_Association_Base::factory('hasOne', 'Avatar', array(), new User()); $expected = array('avatar' => 'getObject', 'avatar=' => 'setObject', 'buildAvatar' => 'buildObject', 'createAvatar' => 'createObject'); $this->assertEquals($expected, $assoc->getMethods()); }
public function testGetMethods() { $assoc = Mad_Model_Association_Base::factory('hasMany', 'Articles', array(), new User()); $expected = array('articles' => 'getObjects', 'articles=' => 'setObjects', 'articleIds' => 'getObjectIds', 'articleIds=' => 'setObjectIds', 'articleCount' => 'getObjectCount', 'addArticle' => 'addObject', 'replaceArticles' => 'replaceObjects', 'deleteArticles' => 'deleteObjects', 'clearArticles' => 'clearObjects', 'findArticles' => 'findObjects', 'buildArticle' => 'buildObject', 'createArticle' => 'createObject'); $this->assertEquals($expected, $assoc->getMethods()); }