/** * test Counter Cache With Self Joining table * * @return void */ public function testCounterCacheWithSelfJoin() { $this->skipIf($this->db instanceof Sqlite, 'SQLite 2.x does not support ALTER TABLE ADD COLUMN'); $this->loadFixtures('CategoryThread'); $column = 'COLUMN '; if ($this->db instanceof Sqlserver) { $column = ''; } $column .= $this->db->buildColumn(array('name' => 'child_count', 'type' => 'integer')); $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . ' ADD ' . $column); $this->db->flushMethodCache(); $Category = new CategoryThread(); $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); $this->assertFalse(empty($result)); $Category = new CategoryThread(); $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; $Category->updateCounterCache(array('parent_id' => 5)); $result = Hash::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count'); $expected = array(1); $this->assertEquals($expected, $result); }
/** * test Counter Cache With Self Joining table * * @return void */ public function testCounterCacheWithSelfJoin() { $skip = $this->skipIf($this->db->config['driver'] == 'sqlite', 'SQLite 2.x does not support ALTER TABLE ADD COLUMN'); if ($skip) { return; } $this->loadFixtures('CategoryThread'); $this->db->query('ALTER TABLE ' . $this->db->fullTableName('category_threads') . " ADD COLUMN child_count INTEGER"); $Category = new CategoryThread(); $result = $Category->updateAll(array('CategoryThread.name' => "'updated'"), array('CategoryThread.parent_id' => 5)); $this->assertFalse(empty($result)); $Category = new CategoryThread(); $Category->belongsTo['ParentCategory']['counterCache'] = 'child_count'; $Category->updateCounterCache(array('parent_id' => 5)); $result = Set::extract($Category->find('all', array('conditions' => array('CategoryThread.id' => 5))), '{n}.CategoryThread.child_count'); $expected = array_fill(0, 1, 1); $this->assertEqual($result, $expected); }