/** Returns cache or creates object * Used to load models from the database; ensures all php models for one record are shared */ public static function loadCached($id, $data = null) { $cache_id = implode("\\A", $id); $object_type = get_called_class(); if (isset(self::$cached[$object_type][$cache_id])) { return self::$cached[$object_type][$cache_id]; } // create and populate new object $obj = new static(); if (is_array($data)) { $obj->loadFromArray($data); } else { $obj->loadFromDatabase($id, false); } // cache and return return self::$cached[$object_type][$cache_id] = $obj; }
/** * Returns the first match for the $filters given * */ public static function loadFirstMatch($filters, $order = null) { return static::buildCollection($filters, $order)->current(); $sql = static::sqlWhere($params, $filters); if ($order === null) { $order = static::$default_order; } if ($order) { $sql .= " ORDER BY {$order}"; } $sql .= " LIMIT 1"; $row = static::getConnection()->fetchRowAssoc(new \ArtfulRobot\PDO_Query("Fetch records from " . static::TABLE_NAME, "SELECT * FROM `" . static::TABLE_NAME . "` {$sql}", $params)); if (!$row) { return null; } // create an object of the class $obj = new static(); $obj->loadFromArray($row, false, self::CAST_DB); return $obj; }
public function loadMultiple($params = array()) { //Get definition $def = $this->getDefinition(); //Construct query $sql = "SELECT * from {$def['table']}"; if (!empty($params['joins'])) { $sql .= " " . implode(" ", $params['joins']); } if (!empty($params['where'])) { $sql .= " WHERE " . (is_array($params['where']) ? implode(" AND ", $params['where']) : $params['where']); } $sql .= " ORDER BY " . (empty($params['orderBy']) ? $def['defaultOrder'] : $params['orderBy']); if (!empty($params['limit'])) { $sql .= " LIMIT {$params['limit']}"; } //Get the records $results = $this->getAdapter()->query($sql, isset($params['data']) ? $params['data'] : array()); if (isset($params['useArrays']) && $params['useArrays']) { $rows = $results; } else { $rows = new \ATP\ActiveRecord\ModelList($def['table']); foreach ($results as $row) { $obj = new static(); $obj->loadFromArray($row); $rows[] = $obj; } } return $rows; }