/** * 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(); } }
public function testOtoRelationship() { $mapper = new AnnotationMapper(); $user = new User(); $user_meta = $mapper->getEntityMetadata($user); $relationships = $user_meta->getRelationships(); $this->assertCount(1, $relationships); $address_relationship = $user_meta->getRelationshipByName('address'); $this->assertEquals(User::class, $address_relationship->getSource()); $this->assertEquals(Address::class, $address_relationship->getTarget()); $this->assertEquals('user', $address_relationship->getInversedBy()); $this->assertEquals(RelationshipType::ONETOONE(), $address_relationship->getRelationshipType()); }
/** * Get an array containing an array of foreign entities to remove the local entity from, and an array of foreign * entities to add the local entity to and an array of entities which remain the same in the relationship * * @param string $key Local relationship key * @param Relationship $relationship Relationship in question * @param object|object[] $new_value New local value containing foreign entities * @return array */ private function getRelationshipDeltas($key, Relationship $relationship, $new_value) { $this->getDriver()->debugLog('Getting inverse relationship deltas: ' . $key); if (RelationshipType::isMultiIndex($relationship->getRelationshipType())) { return $this->getRelationshipDeltasMulti($key, $new_value); } else { return $this->getRelationshipDeltasSingle($key, $new_value); } }
/** * Hydrate a relationship * * @param Relationship $relative * @return $this */ public function hydrateRelative(Relationship $relative) { $this->entity_manager->getDriver()->debugLog("Hydrating relative for " . $this->metadata->getTableName() . "[" . $this->getReader()->getId() . "]::" . $relative->getName()); $setter = $relative->getSetter(); $key = $this->entity_manager->getKeyScheme()->getRelationshipKey($relative, $this->entity_manager->getMapper()->getEntityMetadata($relative->getSource())->getTableName(), $this->entity_manager->getMapper()->getEntityMetadata($relative->getTarget())->getTableName(), $this->getReader()->getId()); if (RelationshipType::isMultiIndex($relative->getRelationshipType())) { $items = []; $ids = $this->entity_manager->getDriver()->getMultiValueIndex($key); foreach ($ids as $id) { $items[] = $this->entity_manager->retrieveEntityOrNew($relative->getTarget(), $id); } $this->proxy->{$setter}($items); } else { $id = $this->entity_manager->getDriver()->getSingleValueIndex($key); if ($id) { $this->proxy->{$setter}($this->entity_manager->retrieve($relative->getTarget(), $id)); } } return $this; }
/** * 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); }
/** * Create a relationship from schema * * @param string $property * @param array $column_schema * @return Relationship */ private function createRelationship($property, array $column_schema) { $assoc = $this->getNode($column_schema, Schema::REL_ASSOCIATION, true); $relationship = new Relationship($property, RelationshipType::memberByValue($assoc)); $relationship->setTarget($this->getNode($column_schema, Schema::REL_TARGET, true))->setInversedBy($this->getNode($column_schema, Schema::REL_INVERSED_BY, false))->setGetter($this->getNode($column_schema, Schema::GETTER, false))->setSetter($this->getNode($column_schema, Schema::SETTER, false))->setSortableBy($this->createSortables($column_schema)); return $relationship; }