Skip to content

Commit f620719

Browse files
committed
🚧 taxonomy, classification and content types are now inserted correctly
1 parent d1f62f3 commit f620719

4 files changed

Lines changed: 176 additions & 18 deletions

File tree

src/Entities/Classification.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TapestryCloud\Database\Entities;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
6+
57
/**
68
* @Entity
79
* @Table(name="classifications")
@@ -14,18 +16,39 @@ class Classification
1416
/** @Column(type="string") */
1517
private $name;
1618

17-
/** @ManyToOne(targetEntity="Taxonomy") */
19+
/**
20+
* @var \Doctrine\Common\Collections\Collection|Taxonomy[]
21+
*
22+
* @ManyToMany(targetEntity="Taxonomy", inversedBy="classification")
23+
* @JoinTable(
24+
* name="taxonomy_classifications",
25+
* joinColumns={
26+
* @JoinColumn(name="classification_id", referencedColumnName="id")
27+
* },
28+
* inverseJoinColumns={
29+
* @JoinColumn(name="taxonomy_id", referencedColumnName="id")
30+
* }
31+
* )
32+
*/
1833
private $taxonomy;
1934

2035
/**
36+
* @var \Doctrine\Common\Collections\Collection|File[]
37+
*
2138
* @ManyToMany(targetEntity="Classification")
22-
* @JoinTable(name="files_classifications",
23-
* joinColumns={@JoinColumn(name="file_id", referencedColumnName="id")},
24-
* inverseJoinColumns={@JoinColumn(name="classification_id", referencedColumnName="id")}
39+
* @JoinTable(name="file_classifications",
40+
* joinColumns={@JoinColumn(name="classification_id", referencedColumnName="id")},
41+
* inverseJoinColumns={@JoinColumn(name="file_id", referencedColumnName="id")}
2542
* )
2643
*/
2744
private $files;
2845

46+
public function __construct()
47+
{
48+
$this->taxonomy = new ArrayCollection();
49+
$this->files = new ArrayCollection();
50+
}
51+
2952
/**
3053
* @return string
3154
*/
@@ -38,4 +61,30 @@ public function setName($name)
3861
{
3962
$this->name = $name;
4063
}
64+
65+
/**
66+
* @param Taxonomy $taxonomy
67+
*/
68+
public function addTaxonomy(Taxonomy $taxonomy)
69+
{
70+
if ($this->taxonomy->contains($taxonomy)) {
71+
return;
72+
}
73+
74+
$this->taxonomy->add($taxonomy);
75+
$taxonomy->addClassification($this);
76+
}
77+
78+
/**
79+
* @param Taxonomy $taxonomy
80+
*/
81+
public function removeTaxonomy(Taxonomy $taxonomy)
82+
{
83+
if (!$this->taxonomy->contains($taxonomy)) {
84+
return;
85+
}
86+
87+
$this->taxonomy->removeElement($taxonomy);
88+
}
89+
4190
}

src/Entities/ContentType.php

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TapestryCloud\Database\Entities;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
57
use Tapestry\Entities\ContentType as TapestryContentType;
68

79
/**
@@ -10,30 +12,56 @@
1012
*/
1113
class ContentType
1214
{
13-
/** @Id @Column(type="integer") @GeneratedValue */
15+
/**
16+
* @var int
17+
* @Id @Column(type="integer") @GeneratedValue */
1418
private $id;
1519

16-
/** @ManyToOne(targetEntity="Environment") */
20+
/**
21+
* @var Environment
22+
* @ManyToOne(targetEntity="Environment") */
1723
private $environment;
1824

19-
/** @Column(type="string") */
25+
/**
26+
* @var string
27+
* @Column(type="string") */
2028
private $name;
2129

22-
/** @Column(type="string") */
30+
/**
31+
* @var string
32+
* @Column(type="string") */
2333
private $path;
2434

25-
/** @Column(type="string") */
35+
/**
36+
* @var string
37+
* @Column(type="string") */
2638
private $template;
2739

28-
/** @Column(type="string") */
40+
/**
41+
* @var string
42+
* @Column(type="string") */
2943
private $permalink;
3044

31-
/** @Column(type="boolean") */
45+
/**
46+
* @var bool
47+
* @Column(type="boolean") */
3248
private $enabled;
3349

34-
/** @OnetoMany(targetEntity="Taxonomy", mappedBy="content_type_id") */
50+
/**
51+
* @var Collection|Taxonomy[]
52+
*
53+
* @OneToMany(targetEntity="Taxonomy", mappedBy="contentType")
54+
*/
3555
private $taxonomy;
3656

57+
/**
58+
* ContentType constructor.
59+
*/
60+
public function __construct()
61+
{
62+
$this->taxonomy = new ArrayCollection();
63+
}
64+
3765
/**
3866
* ContentType Hydration
3967
*
@@ -91,7 +119,7 @@ public function getEnabled()
91119
}
92120

93121
/**
94-
* @return Environment[]
122+
* @return Environment
95123
*/
96124
public function getEnvironment()
97125
{
@@ -104,15 +132,35 @@ public function setEnvironment(Environment $environment)
104132
}
105133

106134
/**
107-
* @return Taxonomy[]
135+
* @return Collection|Taxonomy[]
108136
*/
109137
public function getTaxonomy()
110138
{
111139
return $this->taxonomy;
112140
}
113141

142+
/**
143+
* @param Taxonomy $taxonomy
144+
*/
114145
public function addTaxonomy(Taxonomy $taxonomy)
115146
{
116-
$this->taxonomy[] = $taxonomy;
147+
if ($this->taxonomy->contains($taxonomy)) {
148+
return;
149+
}
150+
151+
$this->taxonomy->add($taxonomy);
152+
$taxonomy->setContentType($this);
153+
}
154+
155+
/**
156+
* @param Taxonomy $taxonomy
157+
*/
158+
public function removeTaxonomy(Taxonomy $taxonomy)
159+
{
160+
if (!$this->taxonomy->contains($taxonomy)) {
161+
return;
162+
}
163+
164+
$this->taxonomy->removeElement($taxonomy);
117165
}
118166
}

src/Entities/Taxonomy.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TapestryCloud\Database\Entities;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Doctrine\Common\Collections\Collection;
57
use Tapestry\Entities\Taxonomy as TapestryTaxonomy;
68

79
/**
@@ -10,15 +12,41 @@
1012
*/
1113
class Taxonomy
1214
{
13-
/** @Id @Column(type="integer") @GeneratedValue */
15+
/**
16+
* @var int
17+
*
18+
* @Id @Column(type="integer") @GeneratedValue */
1419
private $id;
1520

16-
/** @Column(type="string") */
21+
/**
22+
* @var string
23+
*
24+
* @Column(type="string") */
1725
private $name;
1826

19-
/** @OnetoMany(targetEntity="Classification", mappedBy="taxonomy_id") */
27+
28+
/**
29+
* @var ContentType
30+
*
31+
* @ManyToOne(targetEntity="ContentType", inversedBy="taxonomy")
32+
*/
33+
private $contentType;
34+
35+
/**
36+
* @var Collection|Classification[]
37+
*
38+
* @ManyToMany(targetEntity="Classification", mappedBy="taxonomy")
39+
*/
2040
private $classifications;
2141

42+
/**
43+
* Taxonomy constructor.
44+
*/
45+
public function __construct()
46+
{
47+
$this->classifications = new ArrayCollection();
48+
}
49+
2250
/**
2351
* Taxonomy Hydration.
2452
* @param TapestryTaxonomy $taxonomy
@@ -40,4 +68,23 @@ public function setName($name)
4068
{
4169
$this->name = $name;
4270
}
71+
72+
public function getContentType()
73+
{
74+
return $this->contentType;
75+
}
76+
77+
public function setContentType(ContentType $contentType) {
78+
$this->contentType = $contentType;
79+
}
80+
81+
public function getClassifications()
82+
{
83+
return $this->classifications;
84+
}
85+
86+
public function addClassification(Classification $classification)
87+
{
88+
89+
}
4390
}

src/Synchronizes/ContentTypes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ORM\EntityManagerInterface;
66
use Tapestry\Entities\Taxonomy as TapestryTaxonomy;
77
use Tapestry\Modules\ContentTypes\ContentTypeFactory;
8+
use TapestryCloud\Database\Entities\Classification;
89
use TapestryCloud\Database\Entities\ContentType;
910
use TapestryCloud\Database\Entities\Environment;
1011
use TapestryCloud\Database\Entities\Taxonomy;
@@ -62,6 +63,19 @@ private function syncTaxonomyToContentType(ContentType $contentType, array $taxo
6263
$contentType->addTaxonomy($record);
6364
$this->em->persist($contentType);
6465
$this->em->flush();
66+
67+
foreach ($taxonomy->getFileList() as $classification => $files) {
68+
69+
if (! $classificationRecord = $this->em->getRepository(Classification::class)->findOneBy(['name' => $classification])){
70+
$classificationRecord = new Classification();
71+
$classificationRecord->setName($classification);
72+
$this->em->persist($classificationRecord);
73+
}
74+
75+
$classificationRecord->addTaxonomy($record);
76+
$this->em->flush();
77+
}
78+
6579
}
6680
}
6781
}

0 commit comments

Comments
 (0)