2016-06-06 259 views
0

我刚开始学习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; 
    } 
} 
+1

检查Symfony的日志。如果它没有打开,请打开PHP日志记录,然后在重复出错后检查该日志。如果您使用的是PHP服务器,则可以在Apache中进行设置,然后您也可以在那里进行日志记录。 – Shazbot

+0

萨克你,真的有帮助! –

回答

0

使用web/app_dev.php为您的端点。 因此,你的网址将会像localhost:8000/app_dev.php/<your-routing-alias>

+0

谢谢你的答案,但在这种情况下,即使当我使用app_dev.php服务器仍然终止,我仍然不能得到任何错误消息。 –

0

你是否使用网络浏览器访问远程主机上的Symfony实例 - 我的意思是一台不是'localhost'的机器?如果是这样,你需要修改的地方有一个像这样一条线的“网/ app_dev.php”文件:

|| !(in_array(@$_SERVER['REMOTE_ADDR'], ['192.168.40.208', '192.168.255.74', 
      '127.0.0.1', 'fe80::1', '::1']) || php_sapi_name() === 'cli-server') 

输入您尝试访问您的网络远程主机的IP地址网址来自。使用DEV URL:“http://ip_address/app_dev.php/”将是解决问题的最佳方法。

当您使用此链接时,您会看到有用的帮助信息 - 我强烈建议您使用此链接。

我也注意到这一行:

@ORM\Table(name="`user`") 

我不知道,如果你需要的反引号。试试这个:

@ORM\Table(name="user") 
+0

谢谢,但我使用本地主机,并使用开发环境没有帮助。另外我认为这是默认情况下的开发环境(?)。反引号是必需的,因为“用户”是MySql保留字。不过,我设法通过检查php日志文件来解决我的问题。事实证明,我不得不创建日期时间对象,而不是将日期作为字符串提供。 –

+0

嗨 - 这是不正确的。我有相同的注释'@ORM \ Table(name =“user”)',它工作正常。 –

+1

也许,但在Doctrine文档中它说你应该反驳MySql的保留字,所以我只是不想冒这个风险。它也可以反引号工作。 –

0

我最近有这个问题,经过大量的调查发现日期导致我的问题。我可以看到这是一个老问题,但答案可能有助于某人。

它看起来像学说插入一个日期/ datetimetz场时,需要一个完整的日期时间对象:

尝试更换

$user->setJoined(date("Y-m-d H:i:s")); 

随着

$user->setJoined(new \DateTime("now"));