ZF2 Doctrine - 使用查询生成器如何指向存储库中的自定义方法
ZF2 Doctrine - using query builder how to point at custom methods in repository
我正在为这个问题苦苦挣扎,希望有人能提供帮助。使用 ZF2 和 Doctrine 构建 webapp 我正在尝试使用 Doctrine 的查询构建器构建查询,并在我的实体文件中使用自定义方法。对于简单的示例实体文件如下(为清楚起见缩短):
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
public function getFullName()
{
return $this->getFirstName() . ' ' . $this->getLastName();
}
因此名字和姓氏直接映射到dB列,getFullName是实体文件中的自定义方法。然后使用自定义存储库扩展实体以进行查询我想使用 getFullName 方法。我在存储库中有以下内容(扩展了实体文件):
$qb = $this->_em->createQueryBuilder();
$qb->select('employee')
->from('Application\Entity\Employee', 'employee')
->andWhere('employee.fullName = :foo')
->setParameter('foo', 'Joe Bloggs');
$query = $qb->getQuery();
我希望在 andWhere 语句中它会转换 employee.fullName 以找到 getFullName 方法,但它似乎没有。有人有什么想法吗?
谢谢
詹姆斯
简单的答案是:"You can not!"。 getFullName
方法与学说无关,因为它只存在于模型中。
模拟你想要的最简单的方法是将名称拆分成几个部分并使用它:
$fullName = 'Joe Bloggs';
list($firstName, $lastName) = explode(' ', $fullName);
$qb = $this->_em->createQueryBuilder();
$qb->select('employee')
->from('Application\Entity\Employee', 'employee')
->andWhere('employee.firstName = :first')
->andWhere('employee.lastName = :last')
->setParameter('first', $firstName)
->setParameter('last', $lastName);
$query = $qb->getQuery();
我正在为这个问题苦苦挣扎,希望有人能提供帮助。使用 ZF2 和 Doctrine 构建 webapp 我正在尝试使用 Doctrine 的查询构建器构建查询,并在我的实体文件中使用自定义方法。对于简单的示例实体文件如下(为清楚起见缩短):
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
public function getFullName()
{
return $this->getFirstName() . ' ' . $this->getLastName();
}
因此名字和姓氏直接映射到dB列,getFullName是实体文件中的自定义方法。然后使用自定义存储库扩展实体以进行查询我想使用 getFullName 方法。我在存储库中有以下内容(扩展了实体文件):
$qb = $this->_em->createQueryBuilder();
$qb->select('employee')
->from('Application\Entity\Employee', 'employee')
->andWhere('employee.fullName = :foo')
->setParameter('foo', 'Joe Bloggs');
$query = $qb->getQuery();
我希望在 andWhere 语句中它会转换 employee.fullName 以找到 getFullName 方法,但它似乎没有。有人有什么想法吗?
谢谢 詹姆斯
简单的答案是:"You can not!"。 getFullName
方法与学说无关,因为它只存在于模型中。
模拟你想要的最简单的方法是将名称拆分成几个部分并使用它:
$fullName = 'Joe Bloggs';
list($firstName, $lastName) = explode(' ', $fullName);
$qb = $this->_em->createQueryBuilder();
$qb->select('employee')
->from('Application\Entity\Employee', 'employee')
->andWhere('employee.firstName = :first')
->andWhere('employee.lastName = :last')
->setParameter('first', $firstName)
->setParameter('last', $lastName);
$query = $qb->getQuery();