2017-02-17 134 views
0

请注意以下代码。这是函数内部的一个片段。Doctrine 2从findBy查询结果中获得0条结果

global $entityManager; 
$username = $request->getParam('username'); 
$password = $request->getParam('password'); 

我再次检查上面,并正确得到它。

$results = $entityManager->getRepository('Entity\User')->findBy(array('username' => $username, 'password' => $password)); 

这将返回0结果时,它应该返回2,我在数据库中。我验证了我通过检查我的配置连接到正确的数据库,如果没有正确设置,它会抛出错误。

我的实体管理器是在我的推进过程中创建的。这是它的样子。

bootstrap.php中

<?php 

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

require_once('config/config.php'); 

$paths = array(__DIR__."/../entities"); 

// The database connection configuration 
$dbParams = array(
    'driver' => 'pdo_mysql', 
    'host'  => $config['host'], 
    'user'  => $config['dbusername'], 
    'password' => $config['dbpassword'], 
    'dbname' => $config['dbname'], 
); 

$config = Setup::createAnnotationMetadataConfiguration($paths, $debug, null, null, false); 
$entityManager = EntityManager::create($dbParams, $config); 

function GetEntityManager(){ 
    global $entityManager; 
    return $entityManager; 
} 

?> 

我的用户实体如下。

user.php的

<?php 

/** 
* Auto generated by MySQL Workbench Schema Exporter. 
* Version 3.0.3 (doctrine2-annotation) on 2017-02-17 06:04:14. 
* Goto https://github.com/johmue/mysql-workbench-schema-exporter for more 
* information. 
*/ 

namespace Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Entity\BaseUser; 

/** 
* Entity\User 
* 
* @ORM\Entity() 
*/ 
class User extends BaseUser 
{ 
} 

的BaseUser其延伸从低于

BaseUser.php

<?php 

/** 
* Auto generated by MySQL Workbench Schema Exporter. 
* Version 3.0.3 (doctrine2-annotation) on 2017-02-17 06:04:14. 
* Goto https://github.com/johmue/mysql-workbench-schema-exporter for more 
* information. 
*/ 

namespace Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* Entity\User 
* 
* @ORM\Entity() 
* @ORM\Table(name="`user`", uniqueConstraints={@ORM\UniqueConstraint(name="username_UNIQUE", columns={"username"})}) 
* @ORM\InheritanceType("SINGLE_TABLE") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"base":"BaseUser", "extended":"User"}) 
*/ 
class BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer", options={"unsigned":true}) 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=45, nullable=true) 
    */ 
    protected $username; 

    /** 
    * @ORM\Column(name="`password`", type="string", length=45, nullable=true) 
    */ 
    protected $password; 

    /** 
    * @ORM\OneToMany(targetEntity="Authtoken", mappedBy="user") 
    * @ORM\JoinColumn(name="id", referencedColumnName="user_id", nullable=false) 
    */ 
    protected $authtokens; 

    /** 
    * @ORM\OneToMany(targetEntity="Phonenumber", mappedBy="user") 
    * @ORM\JoinColumn(name="id", referencedColumnName="user_id", nullable=false) 
    */ 
    protected $phonenumbers; 

    public function __construct() 
    { 
     $this->authtokens = new ArrayCollection(); 
     $this->phonenumbers = new ArrayCollection(); 
    } 

    /** 
    * Set the value of id. 
    * 
    * @param integer $id 
    * @return \Entity\User 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

    /** 
    * Get the value of id. 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set the value of username. 
    * 
    * @param string $username 
    * @return \Entity\User 
    */ 
    public function setUsername($username) 
    { 
     $this->username = $username; 

     return $this; 
    } 

    /** 
    * Get the value of username. 
    * 
    * @return string 
    */ 
    public function getUsername() 
    { 
     return $this->username; 
    } 

    /** 
    * Set the value of password. 
    * 
    * @param string $password 
    * @return \Entity\User 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    /** 
    * Get the value of password. 
    * 
    * @return string 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Add Authtoken entity to collection (one to many). 
    * 
    * @param \Entity\Authtoken $authtoken 
    * @return \Entity\User 
    */ 
    public function addAuthtoken(Authtoken $authtoken) 
    { 
     $this->authtokens[] = $authtoken; 

     return $this; 
    } 

    /** 
    * Remove Authtoken entity from collection (one to many). 
    * 
    * @param \Entity\Authtoken $authtoken 
    * @return \Entity\User 
    */ 
    public function removeAuthtoken(Authtoken $authtoken) 
    { 
     $this->authtokens->removeElement($authtoken); 

     return $this; 
    } 

    /** 
    * Get Authtoken entity collection (one to many). 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getAuthtokens() 
    { 
     return $this->authtokens; 
    } 

    /** 
    * Add Phonenumber entity to collection (one to many). 
    * 
    * @param \Entity\Phonenumber $phonenumber 
    * @return \Entity\User 
    */ 
    public function addPhonenumber(Phonenumber $phonenumber) 
    { 
     $this->phonenumbers[] = $phonenumber; 

     return $this; 
    } 

    /** 
    * Remove Phonenumber entity from collection (one to many). 
    * 
    * @param \Entity\Phonenumber $phonenumber 
    * @return \Entity\User 
    */ 
    public function removePhonenumber(Phonenumber $phonenumber) 
    { 
     $this->phonenumbers->removeElement($phonenumber); 

     return $this; 
    } 

    /** 
    * Get Phonenumber entity collection (one to many). 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getPhonenumbers() 
    { 
     return $this->phonenumbers; 
    } 

    public function __sleep() 
    { 
     return array('id', 'username', 'password'); 
    } 
} 

这里是在phpMyAdmin的数据的一些截图。

enter image description here

我在做什么错?

- 其他信息 -

composer.json文件

{ 
    "require": { 
     "doctrine/orm": "^2.5", 
     "slim/slim": "^3.0", 
     "slim/twig-view": "^2.1", 
     "components/jquery": "*", 
     "components/normalize.css": "*", 
     "robloach/component-installer": "*", 
     "paragonie/random_compat": "^2.0" 
    }, 
    "autoload": { 
     "psr-4": {"app\\":"app","Entity\\":"entities/"}, 
     "files": ["lib/utilities.php","lib/security.php"] 
    } 
} 

文件结构

enter image description here

+0

也许它与从基类扩展有关?这会造成问题吗? –

+0

好的做了一个小小的进展,一旦我摆脱了扩展功能,我就开始工作了。为什么会造成这个问题搞砸?当您尝试扩展课程时,它是行不通的。 –

回答

0

好,我发现了个答案。我手动将数据输入到数据库中,使用扩展时不能这样做。专业领域discr必须说扩展或它不会工作。通过ORM输入记录显示,这是正确的方式。