public function associatedRecords(ActiveRecord $Record) { $referenceRecord = $Record->getRecord(); $foreignKey = $this->_Association->getDefinition('foreignKey'); if (is_null($referenceRecord[$foreignKey])) { return array(null); } // The record has a foreign key, but has not the associated Active Record. // First try to find the Active Record in the pool, if not query it. $relatedRecord = ActiveRecordManager::findActiveRecordInPool($this->_Association->getModel(), $referenceRecord[$foreignKey]); if ($relatedRecord === false) { $relatedRecord = $this->_Association->getModel()->find('first', array('conditions' => array($this->_Association->getPrimaryKey() => $referenceRecord[$foreignKey]), 'recursive' => -1, 'activeRecord' => false)); if ($relatedRecord) { $relatedRecord = ActiveRecordManager::getActiveRecord($this->_Association->getModel(), $relatedRecord); } else { $relatedRecord = null; } } return array($relatedRecord); }
public function associatedRecords(ActiveRecord $Record) { $record = $Record->getRecord(); $Model = $Record->getModel(); $RelatedRecord = false; // If the association has no condition, try first to find it in the pool if ($this->_Association->getDefinition('conditions')) { $RelatedRecord = ActiveRecordManager::findActiveRecordInPoolWithSecondaryKey($this->_Association->getModel(), $this->_Association->getDefinition('foreignKey'), $record[$Model->primaryKey]); } if ($RelatedRecord !== false) { return array($RelatedRecord); } if (!$Model->Behaviors->attached('Containable')) { $Model->Behaviors->load('Containable'); } $result = $Model->find('first', array('conditions' => array($Model->alias . '.' . $Model->primaryKey => $record[$Model->primaryKey]), 'contain' => array($this->_Association->getName()), 'activeRecord' => false)); if (!empty($result[$this->_Association->getName()][$this->_Association->getPrimaryKey()])) { $RelatedRecord = ActiveRecordManager::getActiveRecord($this->_Association->getModel(), $result[$this->_Association->getName()]); } else { $RelatedRecord = null; } return array($RelatedRecord); }
/** * setUp method * * @return void */ public function setUp() { parent::setUp(); ActiveRecordManager::clearPool(); $this->TWriter = ClassRegistry::init('TWriter'); }
public function refresh($records) { if ($this->_initialized) { if ($this->isBelongsTo() || $this->isHasOne()) { if (count($this->_associated) === 1) { $this->_associated[0]->refresh($records); } else { $this->_associated = array(ActiveRecordManager::getActiveRecord($this->_Model, $records)); } } else { $oldRecords = array(); foreach ($this->_associated as $Record) { $oldRecords[$Record->{$this->getPrimaryKey()}] = $Record; } $result = array(); foreach ($records as $record) { if (array_key_exists($record[$this->getPrimaryKey()], $oldRecords)) { $result[] = $Record->refresh($record); } else { $result[] = ActiveRecordManager::getActiveRecord($this->_Model, $record); } } $this->_associated = $result; } } else { $this->_initializeWithRecord($records); } $this->_changed = false; }
/** * Test refresh on create */ public function testRefreshOnCreate() { $record = array('name2' => 'dsfaedsjgfbdsjygfvb', 'id' => 100); $Record = new ARTRecord($record); $Record->save(); ActiveRecordManager::clearPool(); $RecordRefreshed = new ARTRecord(array('id' => $record['id'])); $this->assertEqual($RecordRefreshed->name2, $record['name2']); $this->assertEqual($RecordRefreshed->id, $record['id']); ActiveRecordManager::clearPool(); $RecordNotRefreshed = new ARTRecord(array('id' => $record['id']), array('norefresh' => true)); $this->assertEmpty($RecordNotRefreshed->name2); $this->assertEqual($RecordNotRefreshed->id, $record['id']); ActiveRecordManager::clearPool(); $RecordSaved = ClassRegistry::init('TRecord')->find('first', array('conditions' => array('id' => $record['id']), 'fields' => array('id'), 'activeRecord' => true)); $this->assertEmpty($RecordSaved->name2); $this->assertEqual($RecordSaved->id, $record['id']); ActiveRecordManager::clearPool(); $RecordSaved2 = ClassRegistry::init('TRecord')->find('first', array('conditions' => array('id' => $record['id']), 'activeRecord' => true)); $this->assertEqual($RecordSaved2->name2, $record['name2']); $this->assertEqual($RecordSaved2->id, $record['id']); }
/** * if the record is associated with another record and this record * not the same as referenced record: find this record, and remove the association * * @see AssociationsTest::testCrossAssociation * * @param ActiveRecord $Record * @param ActiveRecord $ReferenceRecord * @param string $foreignKey * @return void */ protected function _removeOldAssociation(ActiveRecord $Record, ActiveRecord $ReferenceRecord, $foreignKey) { $foreignKeyValue = $Record->getRecord()[$foreignKey]; if (empty($foreignKeyValue)) { return; } $AssociatedRecord = ActiveRecordManager::findActiveRecordInPool($this->_Association->getRecord()->getModel(), $foreignKeyValue); if (!$AssociatedRecord || $ReferenceRecord === $AssociatedRecord) { return; } $assoctiation = $AssociatedRecord->{$this->_Association->getName()}; if ($assoctiation) { $assoctiation->remove($Record); } }
/** * Create proper ActiveRecord object for given arguments * * @param Model $Model * @param array $record * @param array $options * @return ActiveRecord */ public function createActiveRecord(Model $Model, array $record, array $options = null) { return ActiveRecordManager::createActiveRecord($Model, $record, $options); }
public function testCreateSaveAllAndUpdate() { $post = new ARTPost(array('title' => 'TestTitle', 'message' => 'TestMessage', 'writer_id' => 1)); ActiveRecordManager::saveAll(); $newTitle = 'Test2Title'; $post->title = $newTitle; ActiveRecordManager::saveAll(); $id = $post->id; ActiveRecordManager::clearPool(); $post = $this->TPost->find('first', array('recursive' => -1, 'conditions' => array('id' => $id), 'activeRecord' => true)); $this->assertEquals($newTitle, $post->title); }
public static function undoAll() { foreach (self::$_pool as $records) { foreach ($records['records'] as $Record) { $Record->undo(); } } self::$_pendingCreate = array(); }
protected function _delete() { if (!$this->_created) { ActiveRecordManager::delete($this); $model = $this->_Model; if ($this->_directDelete) { // This avoid 2 select statements $result = $model->getDataSource()->delete($model, array($model->alias . '.' . $model->primaryKey => $this->_Record[$model->primaryKey])); } else { $result = $model->delete($this->_Record[$model->primaryKey]); } } else { ActiveRecordManager::remove($this); $result = true; } $this->_resetState(); return $result; }
/** * Test save with HABTM association */ public function testHABTM() { $ARTPost = new ARTPost(array('title' => 'lala', 'message' => '', 'writer_id' => 1)); $Comment1 = new ARTComment(array('message' => 'coment1 lala1', 'post_id' => 0)); $Comment2 = new ARTComment(array('message' => 'coment1 lala2', 'post_id' => 0)); $Comment3 = new ARTComment(array('message' => 'coment1 lala3', 'post_id' => 0)); $Comment4 = new ARTComment(array('message' => 'coment1 lala4', 'post_id' => 0)); $Comment1->Post = $ARTPost; $Comment2->Post = $ARTPost; $Comment3->Post = $ARTPost; $Comment4->Post = $ARTPost; $Comment2->Parents[] = $Comment1; $Comment3->Parents[] = $Comment1; $Comment4->Parents[] = $Comment3; $Comment3->Childrens[] = $Comment4; $Comment1->Childrens[] = $Comment3; $Comment1->Childrens[] = $Comment2; $Comment1->save(); $this->_assertHABTM($Comment1); ActiveRecordManager::clearPool(); $this->_assertHABTM($Comment1); $ARTComment = $this->TComment->find('first', array('conditions' => array('TComment.id' => $Comment1->id), 'activeRecord' => true)); $this->_assertHABTM($ARTComment); }