_tableName_ = 'points'; parent::__construct($fromArray); $this->setRelation('category', 'categoryId', 'Category', 'id', self::TO_ONE); $this->setRelation('authorUser', 'author', 'User', 'id', self::TO_ONE); $this->setRelation('photos', 'id', 'Photo', 'pointId', self::TO_MANY); } static function model() { return new self(); } public function getImg() { $img = $this->img; if ($img == '') { $photos = $this->photos; $img = array_shift($photos); if (!$img) return null; $img = '/photos/small/'.$img->name; } return $img; } public function getRandom() { $res = App::DB()->query("SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `" . $this->_tableName_ . "`")->fetch(PDO::FETCH_ASSOC); $offset = $res['offset']; $res = App::DB()->query('SELECT `id` FROM ' . $this->_tableName_ . ' LIMIT ' . $offset . ',1;')->fetch(PDO::FETCH_ASSOC); $pointId = $res['id']; return $this->getByPK($pointId); } public function getCountByUser($userId) { $res = App::DB()->query('SELECT count(`id`) AS `cnt` FROM ' . $this->_tableName_ . ' WHERE `author` = ' . $userId . ' ;')->fetch(PDO::FETCH_ASSOC); return $res['cnt']; } }