Blog\Model\Comment::add PHP Method

add() public method

Add command
public add ( array $data, integer $documentId ) : boolean
$data array Array of comments
$documentId integer Document id
return boolean
    public function add(array $data, $documentId)
    {
        $mandatoryKeys = array('message', 'username', 'email');
        $insertData = array();
        foreach ($mandatoryKeys as $key) {
            if (empty($data[$key])) {
                return false;
            } else {
                $insertData[$key] = $data[$key];
            }
        }
        $insertData['show_email'] = empty($data['show_email']) ? 0 : 1;
        $insertData['document_id'] = $documentId;
        $insertData['created_at'] = new Expression('NOW()');
        $this->insert($insertData);
        return true;
    }

Usage Example

Beispiel #1
0
 /**
  * Invoke form
  *
  * @return void
  */
 public function __invoke()
 {
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         $this->getForm()->setData($post);
         if ($this->getForm()->isValid()) {
             $commentTable = new Blog\Model\Comment();
             $document = $this->getServiceLocator()->get('Zend\\View\\Renderer\\PhpRenderer')->plugin('CurrentDocument');
             if ($commentTable->add($this->getForm()->getInputFilter()->getValues(), $document()->getId())) {
                 $this->flashMessenger()->addSuccessMessage('Message sent');
                 return $this->redirect()->toUrl($request->getRequestUri());
             }
         }
     }
     return $this->addPath(__DIR__ . '/../views')->render('plugin/comment-form.phtml', array('form' => $this->getForm(), 'errorMessage' => 'Error'));
 }
All Usage Examples Of Blog\Model\Comment::add