我刚开始学习Symfony框架,我注意到几乎每次我的代码中有一些有意义的错误(例如我错过了一些“使用”语句),我的服务器意外终止并且我不知道如何得到关于究竟是什么原因造成的任何信息。我大部分时间都能自己弄清楚,但现在我已经开始使用Doctrine,并且希望将一些对象保存到数据库中,但是当我运行代码时,服务器终止。我发现“flush()”方法导致了这个问题,但是现在我陷入了困境,因为我无法获得有关错误的更多信息。所以我实际上有两个问题 - 我怎样才能获得更多关于这样的错误的信息以及如何解决这个问题?PHP服务器意外终止(Symfony)
的代码如下所示:
控制器
/* UserController.php */
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\User;
use Symfony\Component\HttpFoundation\Response;
class UserController extends Controller
{
/**
* @Route("/user/test")
*/
public function testAction(Request $request)
{
$user = new User();
$user->setLogin("Test");
$user->setEmail("[email protected]");
$user->setPassword(hash("sha256", "test"));
$user->setJoined(date("Y-m-d H:i:s"));
$user->setActivationCode(NULL);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush(); /* The line that causes server to terminate! */
return new Response("Hello");
}
}
与实体
/* User.php (Entity) */
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="`user`")
*/
class User
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=128)
*/
private $email;
/**
* @ORM\Column(type="string", length=64)
*/
private $login;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="datetime")
*/
private $joined;
/**
* @ORM\Column(type="string", length=64, nullable=TRUE)
*/
private $activation_code;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set login
*
* @param string $login
*
* @return User
*/
public function setLogin($login)
{
$this->login = $login;
return $this;
}
/**
* Get login
*
* @return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set joined
*
* @param \DateTime $joined
*
* @return User
*/
public function setJoined($joined)
{
$this->joined = $joined;
return $this;
}
/**
* Get joined
*
* @return \DateTime
*/
public function getJoined()
{
return $this->joined;
}
/**
* Set activationCode
*
* @param string $activationCode
*
* @return User
*/
public function setActivationCode($activationCode)
{
$this->activation_code = $activationCode;
return $this;
}
/**
* Get activationCode
*
* @return string
*/
public function getActivationCode()
{
return $this->activation_code;
}
}
检查Symfony的日志。如果它没有打开,请打开PHP日志记录,然后在重复出错后检查该日志。如果您使用的是PHP服务器,则可以在Apache中进行设置,然后您也可以在那里进行日志记录。 – Shazbot
萨克你,真的有帮助! –