我正在尝试使用 Symfony 2 向文章添加评论,但在执行 POST 时出现白页。
这是控制器:
public function articleAction($id)
{
$repository = $this->getDoctrine()
->getManager()
->getRepository('PortfolioPortfolioBundle:Article');
$article = $repository->find($id);
if($article === null)
{
throw $this->createNotFoundException('Article[id='.$id.'] inexistant.');
}
$comment = new Comment();
$form = $this->createFormBuilder($comment)
->add('autor', 'text')
->add('content', 'textarea')
->getForm();
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$article->addComment($comment);
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->persist($comment);
$em->flush();
}
}
return $this->render('PortfolioPortfolioBundle:Portfolio:article.html.twig', array(
'article' => $article, "active" => "article", 'form' => $form->createView()
));
}
这是文章模型:
public function addComment(\Portfolio\PortfolioBundle\Entity\Comment $comment)
{
$this->comments[] = $comment;
$comments->setArticle($this);
return $this;
}
public function removeComment(\Portfolio\PortfolioBundle\Entity\Comment $comment)
{
$this->comments->removeElement($comment);
}
public function getcomments()
{
return $this->comments;
}
这是评论模型:
<?php
namespace Portfolio\PortfolioBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Comment
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Portfolio\PortfolioBundle\Entity\CommentRepository")
*/
class Comment
{
/**
* @ORM\ManyToOne(targetEntity="Portfolio\PortfolioBundle\Entity\Article", inversedBy="comments")
* @ORM\JoinColumn(nullable=false)
*/
private $article;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="autor", type="string", length=255)
*/
private $autor;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
public function __construct()
{
$this->date = new \Datetime('now');
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set autor
*
* @param string $autor
* @return Comment
*/
public function setAutor($autor)
{
$this->autor = $autor;
return $this;
}
/**
* Get autor
*
* @return string
*/
public function getAutor()
{
return $this->autor;
}
/**
* Set date
*
* @param \DateTime $date
* @return Comment
*/
public function setDate($date)
{
$this->date = new \DateTime("now");
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set content
*
* @param string $content
* @return Comment
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set article
*
* @param \Portfolio\PortfolioBundle\Entity\Article $article
* @return Comment
*/
public function setArticle(\Portfolio\PortfolioBundle\Entity\Article $article)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* @return \Portfolio\PortfolioBundle\Entity\Article
*/
public function getArticle()
{
return $this->article;
}
}
这是视图:
<form class="comment-text" method="post" {{ form_enctype(form) }}>
<div class="controls-group col-md-10">
<div class="controls">
<!--<textarea id="content" name="content" class="form-control" placeholder="Add a comment" rows="5"></textarea><br/>-->
{{ form_widget(form) }}
<button id="contact-submit" type="submit" class="btn btn-primary">Send</button>
</div>
</div>
</form>
日志:
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-11-14 10:25:36] event.DEBUG:通知事件“kernel.request”到监听器“Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest”。[] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-11-14 10:25:36] request.INFO: Matched route "Article" (parameters: "_controller": "Portfolio\PortfolioBundle\Controller\PortfolioController::articleAction", "id": "2", "_route": "Article") [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger". [] [
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] []
[2013-11-14 10:25:36] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] []
[2013-11-14 10:25:36] doctrine.DEBUG: SELECT t0.id AS id1, t0.title AS title2, t0.description AS description3, t0.date AS date4, t0.autor AS autor5, t0.content AS content6 FROM Article t0 WHERE t0.id = ? ["2"] []
阿帕奇错误日志:
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP Fatal error: Call to a member function setArticle() on a non-object in C:\\wamp\\www\\Symfony\\src\\Portfolio\\PortfolioBundle\\Entity\\Article.php on line 206, referer: /Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP Stack trace:, referer: /Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 1. {main}() C:\\wamp\\www\\Symfony\\web\\app_dev.php:0, referer: /Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 2. Symfony\\Component\\HttpKernel\\Kernel->handle() C:\\wamp\\www\\Symfony\\web\\app_dev.php:28, referer: Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 3. Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel->handle() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2249, referer: /Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 4. Symfony\\Component\\HttpKernel\\HttpKernel->handle() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2947, referer: /Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 5. Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2818, referer: Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 6. call_user_func_array() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2844, referer: Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 7. Portfolio\\PortfolioBundle\\Controller\\PortfolioController->articleAction() C:\\wamp\\www\\Symfony\\app\\bootstrap.php.cache:2844, referer: Symfony/web/app_dev.php/article/2
[Thu Nov 14 11:47:59 2013] [error] [client ] PHP 8. Portfolio\\PortfolioBundle\\Entity\\Article->addComment() C:\\wamp\\www\\Symfony\\src\\Portfolio\\PortfolioBundle\\Controller\\PortfolioController.php:173, referer: Symfony/web/app_dev.php/article/2
你看有什么错误吗?
非常感谢 !