gpt4 book ai didi

php - 如何通过属性'()查找所有?

转载 作者:行者123 更新时间:2023-11-28 23:57:18 26 4
gpt4 key购买 nike

我有一个实体

class Developer extends CustomUser
{

/**
* @var string
*
* @ORM\Column(name="unsubscribe_token", type="string", length=60, nullable=true)
*/
private $unsubscribeToken;


// ...
}

和实体 CustomUser

class CustomUser
{

/**
* @var string
*
* @ORM\Column(name="role", type="string", length=25, nullable=false)
* Assert\NotBlank
*/
protected $roles = 'ROLE_AGENT';


// ...
}

还有一个 Action

public function indexAction()
{
$role = 'ROLE_FREELANCER';

$em = $this->getDoctrine()->getManager();

$all_developer = $this->getDoctrine()->getRepository('ArtelProfileBundle:Developer')->findAll();

$dev_freelancer = $em->getRepository('ArtelProfileBundle:Developer')
->findBy(array('roles' => $role, 'level' => 'Middle', 'unsubscribeToken' => ??));
return $this->render('ArtelProfileBundle:Dachboard:index.html.twig', array(
'developer' => $all_developer,
'developer_freelancer' => $dev_freelancer,
));
}

public function getDevelopersWithRoleAndNullToken($role)
{
$qb = $this->getEntityManager()->createQueryBuilder('d');
$qb
->select('d')
->from('ArtelProfileBundle:Developer', 'd')
->where('d.role = :role')
->andWhere('d.token IS NOT NULL')
->setParameter('role', $role)
->getQuery();
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}

[语义错误] line 0, col 53 near 'role = :role': Error: Class Artel\ProfileBundle\Entity\Developer has no field or association named role

在我的数据库中有很多开发人员,我想找到所有具有 freelancer 和 unsubscribeToken null 示例角色的开发人员。我该怎么做?

最佳答案

FindBy 会找到所有的,FindOneBy 只会找到一个

所以,

考虑到您的字段称为角色,您必须这样做

$dev_freelancer = $em->getRepository('ArtelProfileBundle:Developer')->findBy(array('role' => $role));

这里有文档

http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-simple-conditions

如果你想找到 rol 是 freelance 并且 token 不为 null 的开发者你应该通过 QueryBuilder 来做,没有办法 findBy not null value

像这样

    public function getDevelopersWithRoleAndNullToken($role)
{
$qb = $this->getEntityManager()->createQueryBuilder('d');

$qb
->select('d')
->from('ArtelProfileBundle\Entity\Developer', 'd')
->where('d.roles = :role')
->andWhere('d.unsubscribeToken IS NOT NULL')
->setParameter('role', $role)
->getQuery();

return $qb->getResult();
}

然后从 Controller ...

$developersRolAndNullToken = $em->getRepository('ArtelProfileBundle:Developer')->getDevelopersWithRoleAndNullToken($role);

关于php - 如何通过属性'()查找所有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com