wikipoints/ground/models/Favorite.php

50 lines
1.4 KiB
PHP

<?php
class Favorite extends Model
{
function __construct($fromArray = array())
{
$this->_tableName_ = 'favorites';
parent::__construct($fromArray);
$this->setRelation('user', 'userId', 'User', 'id', self::TO_ONE);
$this->setRelation('point', 'pointId', 'Point', 'id', self::TO_ONE);
}
static function model()
{
return new self();
}
public static function addRemove($pointId)
{
if (!App::$user || !App::$user->id || !$pointId)
return false;
$res = self::model()->getAll(array('where' => '`userId` = '.(int)App::$user->id.' AND `pointId` = '.(int)$pointId, 'limit'=>1));
if ($res)
{
return boolval(Favorite::model()->delete($res->id));
} else {
return Favorite::model()->add(array('userId'=>App::$user->id, 'pointId'=>$pointId));
}
}
public function getAllMy()
{
if (!App::$user || !App::$user->id)
return false;
return $this->getAll(array('where' => '`userId` = '.(int)App::$user->id));
}
public function checkIsMy($pointId)
{
if (!App::$user || !App::$user->id || !$pointId)
return false;
return boolval($this->getAll(array('where' => '`userId` = '.(int)App::$user->id.' AND `pointId` = '.(int)$pointId)));
}
}