如何正确构建有关实体的Doctrine请求。 大家好日子。我对Symfony没什么问题。
$repository = $this->getDoctrine()->getRepository('enterBundle\Entity\users');
$query=$repository->createQueryBuilder('user')
->select('user.id','user.username','user.password','user.token')
->where('user.username=:userName')->setParameter('userName',$login)
->where('user.password=:Pass')->setParameter('Pass',$password)
->getQuery
->getResult(Query::HYDRATE_ARRAY);
问题是"如何修复高级代码"?
注意:未定义的属性:Doctrine \ ORM \ QueryBuilder :: $ getQuery
堆栈追踪:
in src/enterBundle/Controller/DefaultController.php at line 131 -
->select('user.id','user.username','user.password','user.token')
->where('user.username=:userName')->setParameter('userName',$login)
->where('user.password=:Pass')->setParameter('Pass',$password)
->getQuery
->getResult(Query::HYDRATE_ARRAY);
实体:
<?php
namespace enterBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="enterBundle\Repository\usersRepository")
*/
class users
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=777, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=999)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="token", type="text")
*/
private $token;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return users
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* @param string $password
*
* @return users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set token
*
* @param string $token
*
* @return users
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Get token
*
* @return string
*/
public function getToken()
{
return $this->token;
}
}
问题是如何正确使用createQueryBuilder
?
附:我是symfony的新手
答案 0 :(得分:4)
注意:未定义的属性:Doctrine \ ORM \ QueryBuilder :: $ getQuery
在这里查看代码:
//...
->getQuery // <--- missing ()
->getResult(Query::HYDRATE_ARRAY);
问题是如何正确使用createQueryBuilder?
最佳实践建议在存储库方法中创建此逻辑。
首先,创建用户存储库类:
// src/AppBundle/Repository/UserRepository.php
class UserRepository extends EntityRepository
{
public function findUser($username, $password)
{
return $this->createQueryBuilder('user')
->select('user.id','user.username','user.password','user.token')
->where('user.username = :username')->setParameter('username', $username)
->andWhere('user.password = :pass')->setParameter('pass', $password)
->getQuery()
->getArrayResult();
}
}
请注意第二个
where
语句,必须考虑andWhere
或orWhere
,否则将替换第一个where
语句。
其次,将您的存储库与您的实体相关联:
/**
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User
{
//...
}