1. Таблици базы данных (MySQL):
CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) collate utf8_unicode_ci NOT NULL, `email` varchar(255) collate utf8_unicode_ci NOT NULL, `text` text collate utf8_unicode_ci NOT NULL, `post_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `posts` ( `id` mediumint(9) NOT NULL AUTO_INCREMENT, `title` varchar(255) collate utf8_unicode_ci NOT NULL, `text` text collate utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
2. Копируем файл system/config/database.php и вставляем в application/config
Изменяем данные подключения к базе данных.
3. Модель (models/comment.php):
class Comment_Model extends ORM{
protected $belongs_to=array('post');
}
5. Контроллер (controllers/blog.php):
class Blog_Controller extends Controller {
public function add_post()
{
$form=new Post_Form;
if($form->validate())
{
$post=new Post_Model;
$post->title= (string) $form['title'];
$post->text= (string) $form['text'];
$post->save();//Save post
url::redirect('blog');
}
else
{
$view=new View('add_post');
$view->form=$form->render();
$view->render(true);
}
}
public function index()
{
$view=new View('blog');
$view->posts=ORM::factory('post')->orderby('id','desc')->find_all();
$view->render(true);
}
public function view_post($id)
{
$post=ORM::factory('post',(int) $id);
$view=new View('view_post');
$view->post=$post;
$view->comments=$post->comments;
$view->render(true);
}
public function add_comment($id)
{
$post=ORM::factory('post',(int) $id);
$form=new Formation;
$form->add_element('input','name')->add_rule('Rule_Required');
$form->add_element('email','email')->add_rule('Rule_Required');
$form->add_element('textarea','text')->add_rule('Rule_Required');
$form->add_element('submit','Submit');
if($form->validate())
{
$comment=new Comment_Model;
$comment->name= (string) $form['name'];
$comment->email= (string) $form['email'];
$comment->text= (string) $form['text'];
$comment->post_id = (int) $id;
$comment->save($comment);
url::redirect('blog/view_post/'.(int) $id);
}
else
{
$view=new View('add_comment');
$view->form=$form->render();
$view->render(true);
}
}
}6. Качаем модель Formation и подключаем её файле application/config/config.php:
$config['modules'] = array
(
MODPATH.'formation', // Formation
7. Создаем файл Post_Form.php в папке application/libraries:
class Post_Form_Core extends Formation{
public function __construct()
{
parent::__construct();
$this->legend='Add post';
$this->add_element('input','title')->add_rule('required')->add_pre_filter('ucfirst');
$this->add_element('textarea','text')->add_rule('required');
$this->add_element('submit','Submit');
}
}
8. Создаем файл add_post.php в папке application/views:
9. Создаем файл blog.php в папке application/views:Add post
echo ''.html::anchor('blog/add_post/', 'Add post').'
';
foreach($posts as $post)
{
echo ''.html::anchor('blog/view_post/'.$post->id, $post->title).'
';
echo ''.$post->text.'
';
}
10. Создаем файл view_post.php в папке application/views:
echo html::anchor('blog','Post list') .'
';
echo ''. $post->title.'
';
echo ''.$post->text.'
';
echo ''.html::anchor('blog/add_comment/'.$post->id, 'Add comment').'
';
echo '
';
foreach($comments as $comment)
{
echo ''.$comment->name.'
';
echo ''.$comment->text.'
';
}
11. Создаем файл view_post.php в папке application/views:
Add comment
Комментариев нет:
Отправить комментарий