This repository has been archived by the owner on Nov 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
EquipmentRepository.php
91 lines (81 loc) · 2.45 KB
/
EquipmentRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Runalyze\Bundle\CoreBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Runalyze\Bundle\CoreBundle\Model\Equipment\EquipmentStatistics;
class EquipmentRepository extends EntityRepository
{
/**
* @param int $equipmentTypeId
* @param Account $account
* @return Equipment[]
*/
public function findByTypeId($equipmentTypeId, Account $account)
{
return $this->findBy([
'type' => $equipmentTypeId,
'account' => $account->getId()
]);
}
/**
* @param string|string[] $equipmentName
* @param Account $account
* @return Equipment[]
*/
public function findByName($equipmentName, Account $account)
{
return $this->findBy([
'account' => $account->getId(),
'name' => $equipmentName
]);
}
/**
* @param int $typeId
* @param Account $account
* @return EquipmentStatistics
*/
public function getStatisticsForType($typeId, Account $account)
{
return new EquipmentStatistics($this->createQueryBuilder('eq')
->select('eq')
->addSelect('SUM(1) as num')
->addSelect('MIN(t.s/t.distance) as max_pace')
->addSelect('MAX(t.distance) as max_distance')
->join('eq.activity', 't')
->where('eq.type = :typeid')
->andWhere('eq.account = :account')
->setParameter(':typeid', $typeId)
->setParameter(':account', $account->getId())
->addOrderBy('eq.distance', 'DESC')
->groupBy('eq.id')
->getQuery()
->getResult()
);
}
/**
* @param Equipment[] $equipment
* @param int|float $additionalDuration [s]
* @param float $additionalDistance [km]
* @param bool $flush
*/
public function updateEquipment(array $equipment, $additionalDuration, $additionalDistance = 0.0, $flush = true)
{
foreach ($equipment as $object) {
$object->addTime($additionalDuration);
$object->addDistance($additionalDistance);
$this->_em->persist($object);
}
if ($flush) {
$this->_em->flush($equipment);
}
}
public function save(Equipment $equipment)
{
$this->_em->persist($equipment);
$this->_em->flush();
}
public function remove(Equipment $equipment)
{
$this->_em->remove($equipment);
$this->_em->flush();
}
}