diff --git a/ground/classes/Model.php b/ground/classes/Model.php index 6340f97..0258fee 100644 --- a/ground/classes/Model.php +++ b/ground/classes/Model.php @@ -8,8 +8,9 @@ class Model private $_id_; private $_vals_ = array(); - function __construct() + function __construct($table) { + $this->_tableName_ = $table; $this->_primaryKey_ = $this->getPk(); } @@ -45,7 +46,13 @@ class Model return $this; } - /** + static function getByPrimaryKey($id) + { + $instance = new self(); + return $instance->getByPK($id); + } + + /** * Возвращает массив с ассоциативными массивами, соответствующими записями в БД. * @param string $where просто строка с условиями: (`id` > 1) AND (`name` == 'TEST') * @param string $order строка с именем толбца и напрабления: `summ` DESC @@ -62,8 +69,8 @@ class Model $this->_id_ = NULL; // Это больше не конкретная запись $ready = array(); - - while ($row = $res->fetch(PDO::FETCH_ASSOC)) +//print_r($res);die; + while ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) { $ready[$row[$this->_primaryKey_]] = $row; } diff --git a/ground/controllers/IndexController.php b/ground/controllers/IndexController.php index 5305f8b..422da6f 100644 --- a/ground/controllers/IndexController.php +++ b/ground/controllers/IndexController.php @@ -36,7 +36,7 @@ class IndexController extends Controller if (!App::$user ) self::addScript('//ulogin.ru/js/ulogin.js'); - $category = new Category(); + $category = Category::model()->getAll(); $routes = array(); if (App::$user) @@ -69,7 +69,7 @@ class IndexController extends Controller $og['url'] = 'http://wikipoints.ru/?point='.$id; $og['image'] = $point->img; } - else if($_GET['point'] && strtolower($_GET['point']) == 'random') + else if(isset($_GET['point']) && $_GET['point'] && strtolower($_GET['point']) == 'random') { $og['title'] = 'Случайная точка на wikipoints.ru'; $og['description'] = 'Загляните сюда: http://vk.com/wikipoints Случайная точка на карте - лучший способ узнать что-то новое!'; @@ -80,7 +80,7 @@ class IndexController extends Controller } self::render('indexTemplate.php', array( - 'categories' => $category->getAll('', 'ord'), + 'categories' => $category, 'user' => App::$user, 'routes' => $routes, 'og' => $og, diff --git a/ground/controllers/JsonController.php b/ground/controllers/JsonController.php index 73682f3..af2ddd8 100644 --- a/ground/controllers/JsonController.php +++ b/ground/controllers/JsonController.php @@ -8,11 +8,8 @@ class JsonController extends Controller */ static function actionPoints() { - $points = new Point(); - $points = $points->getAll(); - - $categories = new Category(); - $categories = $categories->getAll(); + $points = Point::model()->getAll(); + $categories = Category::model()->getAll(); $result = array(); @@ -38,13 +35,34 @@ class JsonController extends Controller $id = (int) App::getParam('id'); if ($id) { - $point = new Point(); - $point = $point->getByPK($id); + //$point = Point::getByPrimaryKey($id); + //$point = $point->getByPK($id); + +// $point = new Point(); +// $point->name = 'TEST'; +// $point->lat = 12; +// $point->lng = 44; +// $point->img = 'sdfsdfsdf'; +// $point->description = 'sdfsdfsdfsdfsdfsdfsdfsfd'; +// $point->categoryId = 1; +// $point->source = ''; +// $point->author = 2; +// $point->date = time(); +// +// print_r($point->save()); +// +// print '



'; +// +// +// print_r($point); +// die; + + $point = Point::model()->getByPK($id); + $point->descriptionHtml = nl2br($point->description); $point->name = htmlspecialchars($point->name); - $categories = new Category(); - $categories = $categories->getAll(); + $categories = Category::model()->getAll(); $point->categoryIcon = $categories[$point->categoryId]['icon']; $point->categoryName = $categories[$point->categoryId]['name']; @@ -61,8 +79,7 @@ class JsonController extends Controller */ static function actionCategories() { - $categories = new Category(); - $categories = $categories->getAll(); + $categories = Category::model()->getAll(); self::renderPartial('json.php', array( 'data' => $categories, diff --git a/ground/models/Category.php b/ground/models/Category.php index ad0e06c..c74ce13 100644 --- a/ground/models/Category.php +++ b/ground/models/Category.php @@ -4,10 +4,12 @@ class Category extends Model { function __construct() { - - $this->_tableName_ = 'categories'; - parent::__construct(); + return self::model(); } + static function model() + { + return new Model('categories'); + } } diff --git a/ground/models/Point.php b/ground/models/Point.php index da7e0fc..712136c 100644 --- a/ground/models/Point.php +++ b/ground/models/Point.php @@ -4,10 +4,12 @@ class Point extends Model { function __construct() { - - $this->_tableName_ = 'points'; - parent::__construct(); + return self::model(); } + static function model() + { + return new Model('points'); + } }