This commit is contained in:
Marcin Kurczewski 2013-10-21 14:32:47 +02:00
parent ff3e4bc287
commit 6b55706fb4
5 changed files with 34 additions and 14 deletions

View file

@ -680,7 +680,7 @@ class PostController
->innerJoin('tag') ->innerJoin('tag')
->on('post_tag.tag_id = tag.id') ->on('post_tag.tag_id = tag.id')
->where('post_id = post.id') ->where('post_id = post.id')
->and('tag.name = ?')->put($val) ->and('LOWER(tag.name) = LOWER(?)')->put($val)
->close(); ->close();
continue; continue;
} }

View file

@ -1,11 +1,15 @@
<?php <?php
class Model_Comment extends RedBean_SimpleModel class Model_Comment extends RedBean_SimpleModel
{ {
public static function locate($key) public static function locate($key, $throw = true)
{ {
$comment = R::findOne('comment', 'id = ?', [$key]); $comment = R::findOne('comment', 'id = ?', [$key]);
if (!$comment) if (!$comment)
throw new SimpleException('Invalid comment ID "' . $key . '"'); {
if ($throw)
throw new SimpleException('Invalid comment ID "' . $key . '"');
return null;
}
return $comment; return $comment;
} }

View file

@ -1,19 +1,27 @@
<?php <?php
class Model_Post extends RedBean_SimpleModel class Model_Post extends RedBean_SimpleModel
{ {
public static function locate($key, $disallowNumeric = false) public static function locate($key, $disallowNumeric = false, $throw = true)
{ {
if (is_numeric($key) and !$disallowNumeric) if (is_numeric($key) and !$disallowNumeric)
{ {
$post = R::findOne('post', 'id = ?', [$key]); $post = R::findOne('post', 'id = ?', [$key]);
if (!$post) if (!$post)
throw new SimpleException('Invalid post ID "' . $key . '"'); {
if ($throw)
throw new SimpleException('Invalid post ID "' . $key . '"');
return null;
}
} }
else else
{ {
$post = R::findOne('post', 'name = ?', [$key]); $post = R::findOne('post', 'name = ?', [$key]);
if (!$post) if (!$post)
throw new SimpleException('Invalid post name "' . $key . '"'); {
if ($throw)
throw new SimpleException('Invalid post name "' . $key . '"');
return null;
}
} }
return $post; return $post;
} }

View file

@ -1,12 +1,16 @@
<?php <?php
class Model_Tag extends RedBean_SimpleModel class Model_Tag extends RedBean_SimpleModel
{ {
public static function locate($key) public static function locate($key, $throw = true)
{ {
$user = R::findOne('tag', 'name = ?', [$key]); $tag = R::findOne('tag', 'LOWER(name) = LOWER(?)', [$key]);
if (!$user) if (!$tag)
throw new SimpleException('Invalid tag name "' . $key . '"'); {
return $user; if ($throw)
throw new SimpleException('Invalid tag name "' . $key . '"');
return null;
}
return $tag;
} }
public static function insertOrUpdate($tags) public static function insertOrUpdate($tags)
@ -14,7 +18,7 @@ class Model_Tag extends RedBean_SimpleModel
$dbTags = []; $dbTags = [];
foreach ($tags as $tag) foreach ($tags as $tag)
{ {
$dbTag = R::findOne('tag', 'name = ?', [$tag]); $dbTag = self::locate($tag, false);
if (!$dbTag) if (!$dbTag)
{ {
$dbTag = R::dispense('tag'); $dbTag = R::dispense('tag');

View file

@ -1,11 +1,15 @@
<?php <?php
class Model_User extends RedBean_SimpleModel class Model_User extends RedBean_SimpleModel
{ {
public static function locate($key) public static function locate($key, $throw = true)
{ {
$user = R::findOne('user', 'name = ?', [$key]); $user = R::findOne('user', 'name = ?', [$key]);
if (!$user) if (!$user)
throw new SimpleException('Invalid user name "' . $key . '"'); {
if ($throw)
throw new SimpleException('Invalid user name "' . $key . '"');
return null;
}
return $user; return $user;
} }