public function test_fetch_all_helper() { // Simple ID lookup. $params = array('id' => $this->grade_items[0]->id); $items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params); $this->assertCount(1, $items); $item = array_shift($items); $this->assertInstanceOf('grade_item', $item); $this->assertEquals($item->id, $this->grade_items[0]->id); // Various parameters lookup, multiple results. $params = array('courseid' => $this->course->id, 'categoryid' => $this->grade_categories[1]->id); $items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params); $this->assertCount(2, $items); $expecteditems = array($this->grade_items[0]->id => true, $this->grade_items[1]->id => true); foreach ($items as $item) { $this->assertInstanceOf('grade_item', $item); $this->assertArrayHasKey($item->id, $expecteditems); unset($expecteditems[$item->id]); } // Text column lookup. $params = array('iteminfo' => $this->grade_items[2]->iteminfo); $items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params); $this->assertCount(1, $items); $item = array_shift($items); $this->assertInstanceOf('grade_item', $item); $this->assertEquals($item->id, $this->grade_items[2]->id); // Lookup using non-existing columns. $params = array('doesnotexist' => 'ignoreme', 'id' => $this->grade_items[0]->id); $items = grade_object::fetch_all_helper('grade_items', 'grade_item', $params); $this->assertCount(1, $items); $item = array_shift($items); $this->assertInstanceOf('grade_item', $item); $this->assertEquals($item->id, $this->grade_items[0]->id); }
/** * Factory method - uses the parameters to retrieve matching instance from the DB. * @static final protected * @return mixed object instance or false if not found */ protected static function fetch_helper($table, $classname, $params) { if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) { if (count($instances) > 1) { // we should not tolerate any errors here - problems might appear later print_error('morethanonerecordinfetch', 'debug'); } return reset($instances); } else { return false; } }
/** * Finds and returns all grade_category instances based on params. * * @param array $params associative arrays varname=>value * @return array array of grade_category insatnces or false if none found. */ public static function fetch_all($params) { return grade_object::fetch_all_helper('grade_categories', 'grade_category', $params); }
/** * Finds and returns all grade_scale instances based on params. * @static * * @param array $params associative arrays varname=>value * @return array array of grade_scale insatnces or false if none found. */ function fetch_all($params) { return grade_object::fetch_all_helper('scale', 'grade_scale', $params); }
/** * Finds and returns all grade_outcome instances based on params. * @static * * @param array $params associative arrays varname=>value * @return array array of grade_outcome insatnces or false if none found. */ function fetch_all($params) { return grade_object::fetch_all_helper('grade_outcomes', 'grade_outcome', $params); }
/** * Finds and returns all grade_item instances based on params. * @static * * @param array $params associative arrays varname=>value * @return array array of grade_item insatnces or false if none found. */ function fetch_all($params) { return grade_object::fetch_all_helper('grade_items', 'grade_item', $params); }
/** * Finds and returns all grade_category instances based on params. * * @param array $params associative arrays varname=>value * @return array array of grade_category insatnces or false if none found. */ public static function fetch_all($params) { if ($records = self::retrieve_record_set($params)) { return $records; } $records = grade_object::fetch_all_helper('grade_categories', 'grade_category', $params); self::set_record_set($params, $records); return $records; }
/** * Factory method - uses the parameters to retrieve matching instance from the DB. * @static final protected * @return mixed object insatnce or false if not found */ function fetch_helper($table, $classname, $params) { // we have to do use this hack because of the incomplete OOP implementation in PHP4 :-( // in PHP5 we could do it much better if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) { if (count($instances) > 1) { // we should not tolerate any errors here - problems might appear later error('Found more than one record in fetch() !'); } return reset($instances); } else { return false; } }