Doctrine 2从findBy Query获取0结果

时间:2017-02-17 06:39:04

标签: php doctrine-orm orm doctrine slim-3

请注意以下代码。这是函数内部的一个片段。

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

我在上面仔细检查了它是否正确。

<?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;
}

?>

当它返回我在数据库中的2时返回0结果。我验证我通过检查我的配置连接到正确的数据库,如果设置不正确则会抛出错误。< / p>

我的entityManager是在我的boostrap过程中创建的。这是它的样子。

bootstrap.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
{
}

我的用户实体位于下方。

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

它扩展的BaseUser低于

BaseUser.php

{
    "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"]
    }
}

以下是phpmyadmin中数据的一些截图。

enter image description here

我做错了什么?

- 其他信息 -

composer.json文件

site.com/test1/form1

文件结构

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。我手动将数据输入数据库,在使用扩展时无法执行此操作。列区分的字段必须说扩展,否则它将无法正常工作。通过ORM输入记录向我展示了这是正确的做法。

相关问题