DQL 比使用 Doctrine 2 的对象语法更好吗?
我正在尝试掌握教义的窍门,并且有机会查看一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 doctrine 2 文档 你不能使用 DQL INSERT:
不过,我同意您的观点,使用 DQL 插入可以帮助我们在某些情况下更轻松地完成一些任务,包括多对多关系。
According to doctrine 2 documentation you can't INSERT using DQL:
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.