简体   繁体   English

主义2从findBy查询中获得0个结果

[英]Doctrine 2 Getting 0 Results From findBy Query

Please note the following code below. 请注意以下代码。 This is a snippet from inside a function. 这是函数内部的代码片段。

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

I double checked above and its getting this correctly. 我仔细检查了上面,并正确地得到了它。

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

This returns 0 results when it should return 2 that I have in the database. 当它应返回数据库中的2时,将返回0结果。 I verified I am connecting to the correct database by checking my config and if not set correctly it would throw errors. 我通过检查我的配置验证了我正在连接到正确的数据库,如果设置不正确,则会引发错误。

My entityManager is created during my boostrap process. 我的entityManager是在我的boostrap过程中创建的。 Here is what it looks like. 这是它的样子。

bootstrap.php 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;
}

?>

My user entity is below. 我的用户实体在下面。

User.php 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
{
}

The BaseUser it extends from is below 它从其扩展的BaseUser在下面

BaseUser.php 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');
    }
}

Here is some screenshots of the data in phpmyadmin. 这是phpmyadmin中数据的一些屏幕截图。

在此处输入图片说明

What am I doing wrong? 我究竟做错了什么?

-- Other Info -- -其他信息-

composer.json file 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"]
    }
}

File Structure 档案结构

在此处输入图片说明

Ok I found out the answer. 好吧,我找到了答案。 I was manually entering data into the database and you can't do that when using extentions. 我是手动将数据输入数据库,而使用扩展时则无法这样做。 The field for the colum discr has to say extended or it won't work. 必须指出“ colum discr”的字段已扩展 ,否则将无法正常工作。 Entering a record through the ORM showed me that was the correct way to do it. 通过ORM输入记录表明,这是正确的记录方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM