我正在尝试使用array_udiff
public function findFreeUsers($weekId)
{
function compare_users(User $user1, User $user2)
{
return $user1->getId() <=> $user2->getId();
}
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, "compare_users");
return $freeUsers;
}
错误
警告:array_udiff((要求参数3是有效的回调、找不到函数"compare_users"或函数名无效
我在存储库的公共函数中声明compare_users((。symfony似乎找不到compare_users函数。同样的事情也发生了,当我在使用array_udiff后声明coparision函数,或者在repo 中的公共函数之间声明它时
如果使用带闭包的可调用函数、匿名函数或指向类的方法会更好。如果您多次执行该方法,它将尝试重新声明该函数,并且该函数已经存在。此外,该函数可以在其他地方声明,这将导致类似的问题。这就是为什么最好使用以下解决方案。
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, function (User $user1, User $user2) {
return $user1->getId() <=> $user2->getId();
});
return $freeUsers;
}
其他选项
带封口:
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$compareClosure = function (User $user1, User $user2) {
return $user1->getId() <=> $user2->getId();
};
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, $compareClosure);
return $freeUsers;
}
具有类可调用
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_udiff($allUsers, $busyUsers, [$this, 'compare_users']);
return $freeUsers;
}
public static function compare_users(User $user1, User $user2)
{
return $user1->getId() <=> $user2->getId();
}