2015-05-04 50 views
1

我有以下模型,或者当你把它们称为实体时,我也有一个控制器,一切工作在这个动作中,但是当我检查数据库时没有用户。所以我很好奇,因为我失踪了。因此,让我们从一开始就了解我有什么:学说不保存我的实体?

bootstrap.php包含以下代码等等。

... 
/** ---------------------------------------------------------------- **/ 
// Lets Setup Doctrine. 
/** ---------------------------------------------------------------- **/ 
require_once 'vendor/autoload.php'; 

$loader = require 'vendor/autoload.php'; 
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass')); 

use Doctrine\ORM\Tools\Setup; 
use Doctrine\ORM\EntityManager; 

/** 
* Set up Doctrine. 
*/ 
class DoctrineSetup { 

    /** 
    * @var array $paths - where the entities live. 
    */ 
    protected $paths = array(APP_MODELS); 

    /** 
    * @var bool $isDevMode - Are we considered "in development." 
    */ 
    protected $isDevMode = false; 

    /** 
    * @var array $dbParams - The database paramters. 
    */ 
    protected $dbParams = null; 

    /** 
    * Constructor to set some core values. 
    */ 
    public function __construct(){ 
     if (!file_exists('db_config.ini')) { 
      throw new \Exception(
       'Missing db_config.ini. You can create this from the db_config_sample.ini' 
      ); 
     } 

     $this->dbParams = array(
      'driver' => 'pdo_mysql', 
      'user' => parse_ini_file('db_config.ini')['DB_USER'], 
      'password' => parse_ini_file('db_config.ini')['DB_PASSWORD'], 
      'dbname' => parse_ini_file('db_config.ini')['DB_NAME'] 
     ); 
    } 

    /** 
    * Get the entity manager for use through out the app. 
    * 
    * @return EntityManager 
    */ 
    public function getEntityManager() { 
     $config = Setup::createAnnotationMetadataConfiguration($this->paths, $this->isDevMode, null, null, false); 
     return EntityManager::create($this->dbParams, $config); 
    } 
} 

/** 
* Function that can be called through out the app. 
* 
* @return EntityManager 
*/ 
function getEntityManager() { 
    $ds = new DoctrineSetup(); 
    return $ds->getEntityManager(); 
} 

/** 
* Function that returns the conection to the database. 
*/ 
function getConnection() { 
    $ds = new DoctrineSetup(); 
    return $ds->getEntityManager()->getConnection(); 
} 
... 

所以现在我们有教义建立它的时间来创建一个模型(实体)并设置哪些字段,不能为空等等等等。

注意在这一点上,你应该知道我没有使用Symfony以及它的组件在Doctrine之上。我正在使用Slim Framework。因此,如果有任何建议是使用symfony中的x或y,请确保它是一个组件。

Models/User.php

<?php 

namespace ImageUploader\Models; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 

/** 
* @ORM\Entity 
* @ORM\Table(name="users", uniqueConstraints={ 
* @ORM\UniqueConstraint(name="user", columns={"userName", "email"})} 
*) 
*/ 
class User { 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=32, nullable=false) 
    * @Assert\NotBlank() 
    */ 
    protected $firstName; 

    /** 
    * @ORM\Column(type="string", length=32, nullable=false) 
    * @Assert\NotBlank() 
    */ 
    protected $lastName; 

    /** 
    * @ORM\Column(type="string", length=100, unique=true, nullable=false) 
    * @Assert\NotBlank(
    * message = "Username cannot be blank" 
    *) 
    */ 
    protected $userName; 

    /** 
    * @ORM\Column(type="string", length=100, unique=true, nullable=false) 
    * @Assert\NotBlank(
    * message = "Email field cannot be blank." 
    *) 
    * @Assert\Email(
    * message = "The email you entered is invalid.", 
    * checkMX = true 
    *) 
    */ 
    protected $email; 

    /** 
    * @ORM\Column(type="string", length=500, nullable=false) 
    * @Assert\NotBlank(
    * message = "The password field cannot be empty." 
    *) 
    */ 
    protected $password; 

    /** 
    * @ORM\Column(type="datetime", nullable=true) 
    */ 
    protected $created_at; 

    /** 
    * @ORM\Column(type="datetime", nullable=true) 
    */ 
    protected $updated_at; 

    /** 
    * Get the value of Created At 
    * 
    * @return mixed 
    */ 
    public function getCreatedAt() 
    { 
     return $this->created_at; 
    } 

    /** 
    * Set the value of Created At 
    * 
    * @param mixed created_at 
    * 
    * @return self 
    */ 
    public function setCreatedAt(\DateTime $created_at = null) 
    { 
     $this->created_at = $created_at; 

     return $this; 
    } 

    /** 
    * Get the value of Updated At 
    * 
    * @return mixed 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updated_at; 
    } 

    /** 
    * Set the value of Updated At 
    * 
    * @param mixed updated_at 
    * 
    * @return self 
    */ 
    public function setUpdatedAt(\DateTime $updated_at = null) 
    { 
     $this->updated_at = $updated_at; 

     return $this; 
    } 

    /** 
    * Get the value of First Name 
    * 
    * @return mixed 
    */ 
    public function getFirstName() 
    { 
     return $this->firstName; 
    } 

    /** 
    * Set the value of First Name 
    * 
    * @param mixed firstName 
    * 
    * @return self 
    */ 
    public function setFirstName($firstName) 
    { 
     $this->firstName = $firstName; 

     return $this; 
    } 

    /** 
    * Get the value of Last Name 
    * 
    * @return mixed 
    */ 
    public function getLastName() 
    { 
     return $this->lastName; 
    } 

    /** 
    * Set the value of Last Name 
    * 
    * @param mixed lastName 
    * 
    * @return self 
    */ 
    public function setLastName($lastName) 
    { 
     $this->lastName = $lastName; 

     return $this; 
    } 

    /** 
    * Get the value of User Name 
    * 
    * @return mixed 
    */ 
    public function getUserName() 
    { 
     return $this->userName; 
    } 

    /** 
    * Set the value of User Name 
    * 
    * @param mixed userName 
    * 
    * @return self 
    */ 
    public function setUserName($userName) 
    { 
     $this->userName = $userName; 

     return $this; 
    } 

    /** 
    * Get the value of Email 
    * 
    * @return mixed 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * Set the value of Email 
    * 
    * @param mixed email 
    * 
    * @return self 
    */ 
    public function setEmail($email) 
    { 
     $this->email = $email; 

     return $this; 
    } 

    /** 
    * Set ths password. 
    * 
    * @param string password 
    * 
    * @return self 
    */ 
    public function setPassword($password) { 
     $this->password = password_hash($password, PASSWORD_DEFAULT); 
     return $this; 
    } 

    /** 
    * Check the users password against that which is enterd. 
    * 
    * @param string password 
    * 
    * @return bool 
    */ 
    public function checkPassword($password) { 
     if (password_hash($password, PASSWORD_DEFAULT) === $this->getPassword()) { 
      return true; 
     } 

     return false; 
    } 

    /** 
    * Return the password value. 
    * 
    * @return hash 
    */ 
    private function getPassword(){ 
     return $this->password; 
    } 

    /** 
    * @ORM\PrePersist 
    */ 
    public function setCreatedAtTimeStamp() { 
     if (is_null($this->getCreatedAt())) { 
      $this->setCreatedAt(new \DateTime()); 
     } 
    } 

    /** 
    * @ORM\PreUpdate 
    */ 
    public function setUpdatedAtTimeStamp() { 
     if (is_null($this->getUpdatedAt())) { 
      $this->setUpdatedAt(new \DateTime()); 
     } 
    } 
} 

上述模型是正确的,因为据我所知,我的意思是,当我运行"vendor/bin/doctrine migrations:migrate"数据库表是创建

现在,这里用到了什么?它的一个作用下称为SignupController控制器使用称为createAction($params)

**createAction($params)**

public static function createAction($params){ 
    $postParams = $params->request()->post(); 
    $flash = new Flash(); 

    if ($postParams['password'] !== $postParams['repassword']) { 
    $flash->createFlash('error', 'Your passwords do not match.'); 
    self::$createEncryptedPostParams($postParams); 
    $params->redirect('/signup/error'); 
    } 

    $user = new User(); 

    $user->setFirstName($postParams['firstname']) 
     ->setLastName($postParams['lastname']) 
     ->setUserName($postParams['username']) 
     ->setEmail($postParams['email']) 
     ->setPassword($postParams['password']) 
     ->setCreatedAtTimeStamp(); 

    $validator = Validator::createValidatorBuilder(); 
    $validator->enableAnnotationMapping(); 

    $errors = $validator->getValidator()->validate($user); 

    if (count($errors) > 0) { 
     foreach($errors as $error) { 
     $flash->createFlash(
      $error->getPropertyPath() . 'error', 
      $error->getMessage() 
     ); 
    } 

    self::createEncryptedPostParams($postParams); 
    $params->redirect('/signup/error'); 
    } 

    $anyEncryptedErors = self::getEncryptedPostParams(); 
    if ($anyEncryptedErors !== null) { 
    $anyEncryptedErors->destroy('error'); 
    } 

    getEntityManager()->flush(); 
    getEntityManager()->persist($user); 

    $flash->createFlash('success', ' You have signed up successfully! Please sign in!'); 
    $params->redirect('/signin'); 
} 

现在你应该在正确的我展现成功的闪光灯和重定向你输入的一切。 这个作品它重定向,它显示一个flash消息。但它的:

getEntityManager()->flush(); 
    getEntityManager()->persist($user); 

我不认为是工作。为什么?因为在有问题的数据库上执行select * from users时没有记录。

为什么?

+0

实体框架是一个.NET特定的框架,而不是一个通用术语。 –

回答

3

同花顺的说法应该是执行后仍然存在。所以代码应该是:

getEntityManager()->persist($user); 
getEntityManager()->flush(); 
+0

我所做的更改如你所说,依然没有记录在数据库中 – TheWebs

+0

我不知道为什么不工作,但要确保验证没有失败,然后再尝试这个办法: $ EM = getEntityManager(); $ em-> persist($ user); $ em-> flush(); –