-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathParentEntity.php
More file actions
64 lines (53 loc) · 1.58 KB
/
ParentEntity.php
File metadata and controls
64 lines (53 loc) · 1.58 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\OneToManyWithLifecycleCallback;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Zenstruck\Foundry\Tests\Fixture\Model\Base;
#[ORM\Entity]
#[ORM\Table('one_to_many_lifecycle_parent')]
#[ORM\HasLifecycleCallbacks]
class ParentEntity extends Base
{
/** @var Collection<int, ChildEntity> */
#[ORM\OneToMany(targetEntity: ChildEntity::class, mappedBy: 'parent', cascade: ['persist', 'remove'])]
private Collection $children;
#[ORM\Column()]
public int $childrenCount = 0;
public function __construct()
{
$this->children = new ArrayCollection();
}
#[ORM\PrePersist]
public function computeChildrenCount(): void
{
$this->childrenCount = $this->children->count();
}
public function addChild(ChildEntity $child): void
{
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->parent = $this;
}
}
public function removeChild(ChildEntity $child): void
{
$this->children->removeElement($child);
}
/**
* @return Collection<int, ChildEntity>
*/
public function getChildren(): Collection
{
return $this->children;
}
}