Doctrine 2 从 findBy 查询中得到 0 个结果
Doctrine 2 Getting 0 Results From findBy Query
请注意下面的代码。这是函数内部的片段。
global $entityManager;
$username = $request->getParam('username');
$password = $request->getParam('password');
我仔细检查了上面的内容,结果正确。
$results = $entityManager->getRepository('Entity\User')->findBy(array('username' => $username, 'password' => $password));
这个 returns 0 结果应该是我在数据库中的 return 2。 我验证了我正在连接到正确的数据库检查我的配置,如果设置不正确,它会抛出错误。
我的 entityManager 是在我的 boostrap 过程中创建的。这是它的样子。
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 如下
基础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 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中数据的一些截图。
我做错了什么?
-- 其他信息--
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"]
}
}
文件结构
好的,我找到了答案。我手动将数据输入数据库,使用扩展时不能这样做。 colum discr 的字段必须说 extended 否则它将不起作用。通过 ORM 输入记录告诉我这是正确的方法。
请注意下面的代码。这是函数内部的片段。
global $entityManager;
$username = $request->getParam('username');
$password = $request->getParam('password');
我仔细检查了上面的内容,结果正确。
$results = $entityManager->getRepository('Entity\User')->findBy(array('username' => $username, 'password' => $password));
这个 returns 0 结果应该是我在数据库中的 return 2。 我验证了我正在连接到正确的数据库检查我的配置,如果设置不正确,它会抛出错误。
我的 entityManager 是在我的 boostrap 过程中创建的。这是它的样子。
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 如下
基础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 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中数据的一些截图。
我做错了什么?
-- 其他信息--
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"]
}
}
文件结构
好的,我找到了答案。我手动将数据输入数据库,使用扩展时不能这样做。 colum discr 的字段必须说 extended 否则它将不起作用。通过 ORM 输入记录告诉我这是正确的方法。