вторник, 24 ноября 2009 г.

Тестовый пост

public function addAction() {
$form = new Blog_Form_AddPost();
$form->setAction($this->_helper->url($this->getRequest()->getActionName()));
$form->setAttrib('id', 'form-addpost');

if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$blogPost = $this->getTable('BlogPost')->createRow();
$blogPostText = $this->getTable('BlogPostText')->createRow();

$text = $form->getValue('text');

$blogPost->title = $form->getValue('title');
$blogPost->userId = $this->getHelper('Auth')->getUser()->id;
$blogPost->createdAt = date(DATE_ISO8601);
$blogPost->publishedAt = date(DATE_ISO8601);

$position = strpos($text, '
');
if ($position > 0) {
$blogPost->description = strop_tags(substr($text, 0, $position));
}
$blogPostText->text = $form->getValue('text');


try{
$this->getTable('BlogPost')->getAdapter()->beginTransaction();
$blogPost->save();
$blogPostText->postId = $blogPost->id;
$blogPostText->save();
$this->getTable('BlogPost')->getAdapter()->commit();
} catch(Zend_Db_Exception $e){
$this->getTable('BlogPost')->getAdapter()->rollBack();
// save failed; print the reasons why
$form->addErrorMessage($e->getMessage());
}
}
}

$this->view->form = $form;
$this->view->messages = $form->getErrorMessages();
}