Advent Calendar[25æ¥]ï¼ã®äºå®ã ã£ããã®ï¼
- 24æ¥ã®ã¨ã³ããªï¼はじめての rhaco3(2) - @sugamasao.blog.title # => ”コードで世界を変えたい”
ä»æ¥ã®æµã
チュートリアルã®æ²ç¤ºæ¿ãã«ã¹ã¿ãã¤ãºãã¦ã¿ããã¨æãã¾ãã
ä»åã¯ãå
¥åé
ç®ã®ããªãã¼ã·ã§ã³*1ããã¦ãããªãã¼ã·ã§ã³ã¨ã©ã¼ã ã£ããããã®æ¨ãç»é¢ã«è¡¨ç¤ºãã¦ã¿ããã¨ãã趣æ¨ã§ãã
è¨ã訳ãæåã«æ¸ãã¦ããã¨ãPHP ã rhaco ãã»ã¼åãã¦ãªã®ã§ãã¯ã½ã¿ãããªæ¸ãæ¹ããã¦ããã¨æãã¾ãã
ãããããæ¸ãæ¹ã®æ¹ãè¯ããï¼ãã¨ããã¢ããã¤ã¹ãããã°ãã²æ¬²ããã§ãã
ã¨ãããããã³ã¼ã
以ä¸ã®ãããªä¿®æ£ãå ãã¦ãã¾ã
- æ²ç¤ºæ¿ã®ã³ã¡ã³ãï¼commentï¼ã空ã ã£ããå ¥åã¨ã©ã¼ã«ãã
- ã¨ã©ã¼ã ã£ãå ´åã¯èµ¤æåã§ã¨ã©ã¼ãåºåãã
<?php include('rhaco3.php'); \org\rhaco\Conf::set('org.rhaco.store.db.Dao' ,'Board' ,'{"type":"org.rhaco.store.db.module.Sqlite","dbname":"board"}' ); /** * @var serial $id * @var string $name * @var text $comment@{"require":true} * @var timestamp $created_at @{"auto_now_add":true} */ class Board extends \org\rhaco\store\db\Dao{ protected $id; protected $name; protected $comment; protected $created_at; } $req = new \org\rhaco\Request(); $unko = null; try { if($req->is_post()){ $obj = new Board(); $obj->name($req->in_vars("name")); $obj->comment($req->in_vars("comment")); $obj->save(); } } catch(\org\rhaco\Exceptions $e){ $unko = $e->messages(); } $paginator = new \org\rhaco\Paginator(5,$req->in_vars("page",1)); $template = new \org\rhaco\Template(); $template ->set_object_module(new \org\rhaco\flow\module\HtmlFilter()) ->set_object_module(new \org\rhaco\flow\module\Paginator()); $template->vars("object_list" ,Board::find_all($paginator,\org\rhaco\store\db\Q::order('-id')) ); $template->vars("unko", $unko); $template->vars("paginator",$paginator); $template->output(__FILE__); ?> <rt:template> <html> <head> <meta charset='utf8' /> </head> <body> <rt:if param="{$unko}"> <p style="color:red;"> {$unko[0]} </p> </rt:if> <form method="post"> åå:<input type="text" name="name" rt:ref="true" /><br /> ã³ã¡ã³ã:<textarea name="comment"></textarea><br /> <input type="submit" value="æ稿" /> </form> <rt:loop param="object_list" var="obj"> <div> {$obj.name()} [{$obj.fm_created_at()}] </div> <pre> {$obj.comment()} </pre> <hr /> </rt:loop> <div class="paginator"> <rt:paginator /> </div> </body> </html> </rt:template>
ã³ã¡ã³ãã空ã§*2ãã¹ãããã¨ã以ä¸ã®ããã«ã¨ã©ã¼ã表示ãããã
解説
ããªãã¼ã·ã§ã³
ã¨ã©ã¼ãã§ãã¯ã¯ï¼ãã¶ãï¼rhaco3 ã® Object ã¯ã©ã¹ãæä¾ãã¦ããã¢ããã¼ã·ã§ã³æ©è½ãªãã ã¨æãããããã¦ã« rhaco2 ã§ã®ä½¿ããæ¹ããã¯ãã¦ãã¦è¿½å ããã®ã以ä¸ã®ã³ã¡ã³ãã
/** * @var serial $id * @var string $name * @var text $comment@{"require":true} * @var timestamp $created_at @{"auto_now_add":true} */ class Board extends \org\rhaco\store\db\Dao{ (ç¥) }
"require":true ã¨ãã¦ããã¨ããã®ãã©ã¡ã¼ã¿ã空ã®ç¶æ
㧠save() ãã㨠\org\rhaco\Exceptions ãçºçããã
ãã®ä¾å¤ãçºçããã¨ãã«ãã¨ã©ã¼ç¨ã®å¤æ°â¦â¦ä¸è¨ã®ã³ã¼ãã 㨠$unko ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãæ ¼ç´ãã¦ããã
ãã³ãã¬ã¼ãã¸ãã¤ã³ã
$template->vars("unko", $unko);
ã§ãã³ãã¬ã¼ãã« unko å¤æ°ããã¤ã³ããã¦ããï¼ã¨æãï¼ã
ãã³ãã¬ã¼ãã§åºå
<rt:if param="{$unko}"> <p style="color:red;"> {$unko[0]} </p> </rt:if>
Templateの拡張タグãè¦ãã¨ãè²ã
ãªãã³ãã¬ã¼ãã¿ã°ã使ããã®ããããã¨æããä»åã¯ãã®ä¸ãã if ãé¸ãã ã
value ã®å¤ãçç¥ããå ´åã¯ãå¤ããããã©ããã§ãããªã«ãã£ã¦ãããã®ã§ä»åã®å ´åã¯ä¾¿å©ã
æåãparam="unko" ã£ã¦æ¸ãã¦ã¦ãªãã©è©¦ãã¦ãæå¾
ããåãã«ãªããªãã¦ãã¡ãã§ã£ãã®ã ãã©ãããããã¿ãã "{$unko}" ã£ã¦æ¸ãã¿ãããRubyã®æååå
ã§å±éããã®ã¨ä¼¼ããããªã¤ã¡ã¼ã¸ã ãããã
ã¾ã¨ã
å®ã¯ãã³ãã¬ã¼ãã®ç¶æ¿ã¾ã§è¡ããªãã£ãã®ã¯å¿æ®ããªã®ã ãã©ãã¢ããã¼ã·ã§ã³ã«ã¤ãã¦ã¯å°ã触ããã¨ãåºæ¥ããã¨ããããã¢ãã«å¨ãã¯ãããæã£ãã¨ããã«æ¸ããããããããªãããªãã¨æãã¾ããã
ãã³ãã¬ã¼ãã«ã¤ãã¦ã¯ããå°ãã¡ããã¨ç解ããªãã¨ä½¿ãããªããªãããªæãã ããã©ãããã