$bob = User::where('username', 'john')->first();
$alice = User::where('username', 'alice')->first();
User::filterFollowingsOf($bob)->get(); // You will get no results.
User::filterUnfollowingsOf($bob)->get(); // You will get Alice.
$bob->follow($alice);
User::filterFollowingsOf($bob)->get(); // You will get Alice as result.
https://github.com/rennokki/befriended
Инициализация
try {
$mongo = new MongoClient("mongodb://localhost:27017/yourtable");
$db= $mongo->selectDB("yourtable");
$collection = $db->yourcollection;
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server. ' . $e->getMessage());
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
http://www.querymongo.com/ - онлайн сервис преобразует sql select в mongo select (если запрос чуть сложнее обычного сразу использует reduce, я же предпочитаю использовать aggregate)
insert($data_insert);
// lastID: (string)$data_insert['_id'];
// SELECT
$query = array(
// where
);
$projection = array(
// return
);
$cursor = $collection->find($query, $projection)
->skip($start)
->limit($limit)
->sort(array('field'=> -1));
foreach ($cursor as $row) {
echo "";
print_r($row);
echo "
";
}
// count
$count = $collection->count();
// SELECT_ONE
$query = array(
// where
);
$projection = array(
// return
);
$row = $collection->findOne($query, $projection);
echo "";
print_r($row);
echo "
";
// UPDATE
$criteria = array(
// where
);
$update = array(
// set
);
$options = array(
// upsert: true, multi: true
);
$collection->update($criteria, $update, $options);
// DELETE
$query = array(
// where
);
$options = array (
'justOne' => true
);
$cursor = $collection->remove($query, $options);
// COMPLEX SELECT
$data = $collection->aggregate(
array(
'$match' => array(
// where
)
),
array(
'$project' => array(
// manipulate fields
)
),
array(
'$group' => array(
// group
)
),
array(
'$skip' => array(
// skip
)
),
array(
'$limit' => array(
// limit
)
),
array(
'$sort' => array(
// sort
)
)
);
// print_r($data);
function seo_url($string, $seperator='-') {
$string = strtolower($string);
$string = preg_replace("/[^a-z0-9_\s-]/", $seperator, $string);
$string = preg_replace("/[\s-]+/", " ", $string);
$string = preg_replace("/[\s_]/", $seperator, $string);
return $string;
}
Делает из "Privet Ivan" => "privet-ivan"
function detect_encoding($string) {
static $list = array('utf-8', 'windows-1251');
foreach ($list as $item) {
$sample = iconv($item, $item, $string);
if (md5($sample) == md5($string))
return $item;
}
return null;
}
Функция детектит кодировку
function full_url()
{
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}
Разберает урл на протокол имя сервера порта и сам запрос
$host = $_SERVER['HTTP_HOST'];
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
Получает имя домена
echo json_decode(str_replace('%u', '\u', json_encode($str_from_js)));
Преобразовует символы Юникода в виде «%uXXXX» в UTF-8
И на закусь некоторые правила htaccess:
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ .php [L,QSA]
# http://domain/about -> http://domain/about.php
--------------------------------------------------
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q= [L,QSA]
# http://domain/about -> http://domain/index.php?q=about
function check_smartphone() {
if ( $_SESSION['mobile_enable'] ) return true;
$phone_array = array('iphone', 'android', 'pocket', 'palm', 'windows ce', 'windowsce', 'cellphone', 'opera mobi', 'operamobi', 'ipod', 'small', 'sharp', 'sonyericsson', 'symbian', 'symbos', 'opera mini', 'nokia', 'htc_', 'samsung', 'motorola', 'smartphone', 'blackberry', 'playstation portable', 'tablet browser', 'android');
$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
foreach ($phone_array as $value) {
if ( strpos($agent, $value) !== false ) return true;
}
return false;
}
$secretkey = date("m.d.y");
//убераем авторизированный токен
setcookie ("token", '', time() - 12200);
//ставим токен авторизированный
setcookie ("token", sha1($username.$secretkey), time() + 12200);
if (!isset($_COOKIE['token'])) {
//нет токена = умри!
die();
} else {
if ( sha1($_COOKIE['user'].$secretkey) !== $_COOKIE['token'] ) {
setcookie ("user", "", time() - 3600);
setcookie ("token", "", time() - 3600);
show_login("Please Login");
die();
}
}