DQL 比使用 Doctrine 2 的对象语法更好吗?

发布于 2024-12-03 00:13:28 字数 2875 浏览 0 评论 0原文

我正在尝试掌握教义的窍门,并且有机会查看一些 DQL 内容,并且能够执行一些简单的插入,例如:

 function insert_user($username,$email,$password) 
        {
$user = new User();
$user->setUsername($username);
$user->setEmail($email);
$user->setPassword(md5($password));

    try {
            //save to database
            $this->em->persist($user);
            $this->em->flush();
        }
        catch(Exception $err){

            die($err->getMessage());

            return false;
        }
        return true;
        }

但是,我仍然在如何执行用户名/密码身份验证等例程方面面临挑战。例如我有:

    function validate_user($username,$password) 
            {
$query = $this->em->createQuery('SELECT u from User u WHERE u.username = :name AND u.username = :name2');
$query->setParameters(array(
    'name' => $username,
    'name2' => $password,
));
$users = $query->getResult(); // array of user objects
            }

这是我的实体类:

<?php



use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @Table(name="user")
 * @Entity
 */
class User
{
    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $username
     *
     * @Column(name="username", type="string", length=300, nullable=false)
     */
    private $username;

    /**
     * @var string $email
     *
     * @Column(name="email", type="string", length=300, nullable=false)
     */
    private $email;

    /**
     * @var string $password
     *
     * @Column(name="password", type="string", length=300, nullable=false)
     */
    private $password;


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

    /**
     * Set username
     *
     * @param string $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

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

    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set password
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

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

我想我可以在 DQL 中弄清楚它,但不太确定如何使用对象语法来做到这一点。

我希望我的问题有意义,因为我只是想熟悉一下。

再次感谢

I am trying to get a hang of doctrine and have had a chance to look at some DQL stuff and have been able to do some simple inserts like:

 function insert_user($username,$email,$password) 
        {
$user = new User();
$user->setUsername($username);
$user->setEmail($email);
$user->setPassword(md5($password));

    try {
            //save to database
            $this->em->persist($user);
            $this->em->flush();
        }
        catch(Exception $err){

            die($err->getMessage());

            return false;
        }
        return true;
        }

However I am still challenged in terms of how to do a routine like username/password authentication. For instance I have:

    function validate_user($username,$password) 
            {
$query = $this->em->createQuery('SELECT u from User u WHERE u.username = :name AND u.username = :name2');
$query->setParameters(array(
    'name' => $username,
    'name2' => $password,
));
$users = $query->getResult(); // array of user objects
            }

Here is my entity class:

<?php



use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @Table(name="user")
 * @Entity
 */
class User
{
    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $username
     *
     * @Column(name="username", type="string", length=300, nullable=false)
     */
    private $username;

    /**
     * @var string $email
     *
     * @Column(name="email", type="string", length=300, nullable=false)
     */
    private $email;

    /**
     * @var string $password
     *
     * @Column(name="password", type="string", length=300, nullable=false)
     */
    private $password;


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

    /**
     * Set username
     *
     * @param string $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

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

    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set password
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

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

I guess I can figure it out in DQL however not too sure how to do it using object syntax.

I hope my question makes sense as I am just trying to get familiar.

Thanks again

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-12-10 00:13:28

根据 doctrine 2 文档 你不能使用 DQL INSERT:

DQL 作为查询语言,具有 SELECT、UPDATE 和 DELETE 结构,
映射到相应的 SQL 语句类型。 INSERT 语句是
DQL 中不允许,因为实体及其关系必须是
通过引入持久化上下文
EntityManager#persist() 确保对象模型的一致性。


不过,我同意您的观点,使用 DQL 插入可以帮助我们在某些情况下更轻松地完成一些任务,包括多对多关系。

According to doctrine 2 documentation you can't INSERT using DQL:

DQL as a query language has SELECT, UPDATE and DELETE constructs that
map to their corresponding SQL statement types. INSERT statements are
not allowed in DQL
, because entities and their relations have to be
introduced into the persistence context through
EntityManager#persist() to ensure consistency of your object model.

However i agree with you that inserting using DQL could help us do some tasks including many-to many relationships a lot easier in some cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文