/** * Get the inverse relationship type of the given relationship type * * @param RelationshipType $relationship_type * @return RelationshipType */ public static function getInverseRelationship(RelationshipType $relationship_type) { switch ($relationship_type) { default: throw new InvalidEntityException("Unknown relationship type: " . $relationship_type->key()); case RelationshipType::ONETOONE(): return RelationshipType::ONETOONE(); case RelationshipType::ONETOMANY(): return RelationshipType::MANYTOONE(); case RelationshipType::MANYTOONE(): return RelationshipType::ONETOMANY(); case RelationshipType::MANYTOMANY(): return RelationshipType::MANYTOMANY(); } }
/** * Remove all references to this entity * * @param string $table_name * @param string $local_id */ private function deleteRefs($table_name, $local_id) { $ref_key = $this->getKeyScheme()->getEntityRefKey($table_name, $local_id); $refs = $this->getDriver()->getRefs($ref_key); foreach ($refs as $ref) { $relationship = $this->getMapper()->getEntityMetadata($ref->getSourceClass())->getRelationshipByName($ref->getRelationshipName()); $relationship_key = $this->getRelationshipKey($relationship, $ref->getEntityId()); switch ($relationship->getRelationshipType()) { default: throw new UnexpectedValueException("Unknown relationship type: " . $relationship->getRelationshipType()->value()); case RelationshipType::MANYTOMANY(): case RelationshipType::ONETOMANY(): $this->getDriver()->removeMultiValueIndex($relationship_key, $local_id); break; case RelationshipType::MANYTOONE(): case RelationshipType::ONETOONE(): $this->getDriver()->clearSingleValueIndex($relationship_key); break; } foreach ($relationship->getSortableBy() as $sortable) { $sort_key = $this->getSortIndexKey($relationship, $sortable->getName(), $ref->getEntityId()); $this->getDriver()->removeSortedIndex($sort_key, $local_id); } } $this->getDriver()->clearRefs($ref_key); }
/** * Get all relationships in the entity * * @return Relationship[] */ public function getRelationships() { $r = []; $properties = $this->reflection_obj->getProperties(); foreach ($properties as $property) { /** @var OneToOne $oto */ $oto = $this->annotation_reader->getPropertyAnnotation($property, self::OTO_ANNOTATION); if ($oto) { $r[] = $this->createRelationship($property->getName(), RelationshipType::ONETOONE(), $oto); } /** @var OneToMany $otm */ $otm = $this->annotation_reader->getPropertyAnnotation($property, self::OTM_ANNOTATION); if ($otm) { $r[] = $this->createRelationship($property->getName(), RelationshipType::ONETOMANY(), $otm); } /** @var ManyToOne $mto */ $mto = $this->annotation_reader->getPropertyAnnotation($property, self::MTO_ANNOTATION); if ($mto) { $r[] = $this->createRelationship($property->getName(), RelationshipType::MANYTOONE(), $mto); } /** @var ManyToMany $mtm */ $mtm = $this->annotation_reader->getPropertyAnnotation($property, self::MTM_ANNOTATION); if ($mtm) { $r[] = $this->createRelationship($property->getName(), RelationshipType::MANYTOMANY(), $mtm); } } return $r; }
/** * Same as above test, except we'll delete the category * * @dataProvider entityManagerDataProvider * @param EntityManager $em */ public function testDeleteIndicesAndRelationshipsAlt(EntityManager $em) { $article1 = new RefArticle(); $article1->setId(502)->setTitle('Ref Article 502'); $category1 = new RefCategory(); $category1->setId(533)->setName('Ref Category 533'); $article1->setCanonicalCategory($category1); $em->persist($category1)->persist($article1)->flush(); $this->assertTrue($this->exists($em, 'article', '502')); $mto = $this->getRelKey($em, 'article', 'category', '502', 'canonical_category', RelationshipType::MANYTOONE()); $this->assertEquals('533', $em->getDriver()->getSingleValueIndex($mto)); // Not inversed: $otm = $this->getRelKey($em, 'category', 'article', '533', 'articles', RelationshipType::ONETOMANY()); $this->assertNotContains('502', $em->getDriver()->getMultiValueIndex($otm)); // Ref exists: $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '533')); $ref = (string) new Ref(RefArticle::class, '502', 'canonical_category'); $this->assertCount(1, $refs); $this->assertEquals($ref, (string) $refs[0]); /** @var RefCategory $category */ $category = $em->retrieve(RefCategory::class, 533); $em->delete($category)->flush(); $this->assertFalse($this->exists($em, 'category', '533')); $this->assertNull($em->getDriver()->getSingleValueIndex($mto)); $this->assertNotContains('502', $em->getDriver()->getMultiValueIndex($otm)); // Ref no longer needed: $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '533')); $this->assertNotContains($ref, $refs); }