From 6470704f4352ce926a65fc4a0c0d6a9d50201636 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 3 Mar 2014 19:27:43 +0100 Subject: [PATCH] Added order:fav_date --- src/Models/SearchParsers/PostSearchParser.php | 3 +++ src/Models/UserModel.php | 1 + src/Upgrades/mysql/Upgrade13.sql | 17 +++++++++++++++++ src/Upgrades/sqlite/Upgrade13.sql | 17 +++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 src/Upgrades/mysql/Upgrade13.sql create mode 100644 src/Upgrades/sqlite/Upgrade13.sql diff --git a/src/Models/SearchParsers/PostSearchParser.php b/src/Models/SearchParsers/PostSearchParser.php index b18e1b5c..130f0fa5 100644 --- a/src/Models/SearchParsers/PostSearchParser.php +++ b/src/Models/SearchParsers/PostSearchParser.php @@ -241,6 +241,9 @@ class PostSearchParser extends AbstractSearchParser elseif (in_array($orderByString, ['commentdate', 'comment_date'])) $orderColumn = 'comment_date'; + elseif (in_array($orderByString, ['favdate', 'fav_date'])) + $orderColumn = 'fav_date'; + elseif ($orderByString == 'random') { //seeding works like this: if you visit anything diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 51d31abd..483188e8 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -156,6 +156,7 @@ class UserModel extends AbstractCrudModel $stmt->setTable('favoritee'); $stmt->setColumn('post_id', new Sql\Binding($post->id)); $stmt->setColumn('user_id', new Sql\Binding($user->id)); + $stmt->setColumn('fav_date', time()); Database::exec($stmt); }); } diff --git a/src/Upgrades/mysql/Upgrade13.sql b/src/Upgrades/mysql/Upgrade13.sql new file mode 100644 index 00000000..34dc94da --- /dev/null +++ b/src/Upgrades/mysql/Upgrade13.sql @@ -0,0 +1,17 @@ +ALTER TABLE favoritee ADD COLUMN fav_date INTEGER DEFAULT NULL; +ALTER TABLE post ADD COLUMN fav_date INTEGER DEFAULT NULL; + +CREATE TRIGGER favoritee_update_date AFTER UPDATE ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; + +CREATE TRIGGER favoritee_insert_date AFTER INSERT ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; + +CREATE TRIGGER favoritee_delete_date AFTER DELETE ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; diff --git a/src/Upgrades/sqlite/Upgrade13.sql b/src/Upgrades/sqlite/Upgrade13.sql new file mode 100644 index 00000000..34dc94da --- /dev/null +++ b/src/Upgrades/sqlite/Upgrade13.sql @@ -0,0 +1,17 @@ +ALTER TABLE favoritee ADD COLUMN fav_date INTEGER DEFAULT NULL; +ALTER TABLE post ADD COLUMN fav_date INTEGER DEFAULT NULL; + +CREATE TRIGGER favoritee_update_date AFTER UPDATE ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; + +CREATE TRIGGER favoritee_insert_date AFTER INSERT ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END; + +CREATE TRIGGER favoritee_delete_date AFTER DELETE ON favoritee FOR EACH ROW +BEGIN + UPDATE post SET fav_date = (SELECT MAX(fav_date) FROM favoritee WHERE favoritee.post_id = post.id); +END;