简体   繁体   中英

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. 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. Here is what it looks like.

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

<?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.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.

在此处输入图片说明

What am I doing wrong?

-- Other Info --

composer.json file

{
    "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. Entering a record through the ORM showed me that was the correct way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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