Skip to content

Instantly share code, notes, and snippets.

@do-aki
Created September 3, 2014 08:34
Show Gist options
  • Save do-aki/b36c64e066bb947ea825 to your computer and use it in GitHub Desktop.
Save do-aki/b36c64e066bb947ea825 to your computer and use it in GitHub Desktop.
<?php
namespace sample;
use dooaki\Phroonga\Groonga;
use dooaki\Phroonga\GroongaEntity;
class Message {
use GroongaEntity;
public static function _schema() {
self::Table('Messages', ['flags' => 'TABLE_HASH_KEY', 'key_type' => 'ShortText']);
self::Column('accout' , 'ShortText');
self::Column('name', 'ShortText');
self::Column('chat' , self::Reference(Chat::class));
self::Column('message' , 'Text');
self::Column('created_at' , 'Time');
self::Column('updated_at' , 'Time');
}
}
class Chat {
use GroongaEntity;
public static function _schema() {
self::Table('Chats', ['flags' => 'TABLE_HASH_KEY', 'key_type' => 'ShortText']);
self::Column('topic' , 'ShortText');
self::Column('created_at' , 'Time');
self::Column('updated_at' , 'Time');
}
public static function findByChatId($chat_id) {
return self::select()->query('_id:?', $chat_id)->findFirst();
}
public function findAllMessage() {
return Message::select()->query('chat._id:?', $this->_id)->sortby(['-created_at'])->findAll();
}
}
(new Groonga('http://localhost:10043'))->activate();
$chat_id = intval(@$_GET['chat_id']);
$chat = Chat::findByChatId($chat_id);
if ($chat) {
echo "Chat Topic is {$chat->topic}";
$chat->findAllMessage()->each(
function(Message $m) {
echo(
"{$m->message} by {$m->name} at ",
date('Y/m/d H:i:s', $m->created_at),
"\n"
);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment