Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Implemented multiple labels ability. #186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion graphaware-mapping.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<xs:element type="ogm:propertyType" name="property" maxOccurs="unbounded" minOccurs="0"/>
<xs:element type="ogm:relationshipType" name="relationship" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute type="xs:string" name="label" use="required"/>
<xs:attribute type="ogm:labels-list-type" name="labels" use="required"/>
<xs:attribute type="xs:string" name="entity" use="required"/>
<xs:attribute type="xs:string" name="repository-class" use="optional"/>
</xs:complexType>
Expand All @@ -82,6 +82,9 @@
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="labels-list-type">
<xs:list itemType="xs:string" />
</xs:simpleType>
<xs:complexType name="order-byType">
<xs:simpleContent>
<xs:extension base="xs:string">
Expand Down
4 changes: 2 additions & 2 deletions src/Annotations/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
final class Node implements Entity
{
/**
* @var string
* @var string[]
*/
public $label;
public $labels;

/**
* @var string
Expand Down
6 changes: 3 additions & 3 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public function isRelationshipEntity()
}

/**
* @return string
* @return string[]
*/
public function getLabel()
public function getLabels()
{
if (!$this->isNodeEntity()) {
throw new MappingException(sprintf('This class metadata is not for a node entity'));
}

return $this->entityAnnotation->label;
return $this->entityAnnotation->labels;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function create($nodeEntityClass)
$annotation = $this->reader->getClassAnnotation($reflectionClass, Node::class);

if (null !== $annotation) {
return new NodeAnnotationMetadata($annotation->label, $annotation->repository);
return new NodeAnnotationMetadata($annotation->labels, $annotation->repository);
}

throw new MappingException(sprintf('The class "%s" is missing the "%s" annotation', $nodeEntityClass, Node::class));
Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/Factory/Xml/NodeEntityMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public function buildNodeEntityMetadata(\SimpleXMLElement $node, $className)
*/
private function buildNodeMetadata(\SimpleXMLElement $node, $className)
{
if (!isset($node['label'])) {
if (!isset($node['labels'])) {
throw new MappingException(
sprintf('Class "%s" OGM XML node configuration is missing "label" attribute', $className)
);
}

return new NodeAnnotationMetadata(
(string) $node['label'],
\explode(' ',(string) $node['labels']),
isset($node['repository-class']) ? (string) $node['repository-class'] : null
);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Metadata/NodeAnnotationMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@
final class NodeAnnotationMetadata
{
/**
* @var string
* @var string[]
*/
private $label;
private $labels;

/**
* @var string
*/
private $customRepository;

/**
* @param string $label
* @param string[] $labels
* @param string|null $repository
*/
public function __construct($label, $repository)
public function __construct($labels, $repository)
{
$this->label = $label;
$this->labels = $labels;
$this->customRepository = $repository;
}

/**
* @return string
* @return string[]
*/
public function getLabel()
public function getLabels()
{
return $this->label;
return $this->labels;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Metadata/NodeEntityMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function __construct(
}

/**
* @return string
* @return string[]
*/
public function getLabel()
public function getLabels()
{
return $this->nodeAnnotationMetadata->getLabel();
return $this->nodeAnnotationMetadata->getLabels();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Persister/EntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getCreateQuery($object)
}
}

$query = sprintf('CREATE (n:%s) SET n += {properties}', $this->classMetadata->getLabel());
$query = sprintf('CREATE (n:%s) SET n += {properties}', \implode(':', $this->classMetadata->getLabels()));
if (!empty($extraLabels)) {
foreach ($extraLabels as $label) {
$query .= ' SET n:'.$label;
Expand Down Expand Up @@ -141,8 +141,8 @@ public function getUpdateQuery($object)
*/
public function refresh($id, $entity)
{
$label = $this->classMetadata->getLabel();
$query = sprintf('MATCH (n:%s) WHERE id(n) = {%s} RETURN n', $label, 'id');
$label = $this->classMetadata->getLabels();
$query = sprintf('MATCH (n:%s) WHERE id(n) = {%s} RETURN n', \implode(':', $label), 'id');
$result = $this->entityManager->getDatabaseDriver()->run($query, ['id' => $id]);

if ($result->size() > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Persister/FlushOperationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function processNodesCreationJob(array $nodesScheduledForCreate)
$byLabelsMap = [];
foreach ($nodesScheduledForCreate as $node) {
$metadata = $this->em->getClassMetadataFor(get_class($node));
$byLabelsMap[$metadata->getLabel()][] = $node;
$byLabelsMap[\implode('`:`', $metadata->getLabels())][] = $node;
}

return $this->createLabeledNodesCreationStack($byLabelsMap);
Expand All @@ -49,7 +49,7 @@ private function createLabeledNodesCreationStack(array $byLabelsMap)
$metadata = $this->em->getClassMetadataFor(get_class($entity));
$oid = spl_object_hash($entity);
$labeledProperties = $metadata->getLabeledPropertiesToBeSet($entity);
$lblKey = sprintf('%s_%s', $metadata->getLabel(), implode('_', array_map(function (LabeledPropertyMetadata $labeledPropertyMetadata) {
$lblKey = sprintf('%s_%s', \implode(':', $metadata->getLabels()), implode('_', array_map(function (LabeledPropertyMetadata $labeledPropertyMetadata) {
return $labeledPropertyMetadata->getLabelName();
}, $labeledProperties)));

Expand Down
24 changes: 12 additions & 12 deletions src/Persisters/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public function getCountForRelationship($alias, $sourceEntity)
public function getMatchCypher(array $criteria = [], $orderBy = null, $limit = null, $offset = null)
{
$identifier = $this->_classMetadata->getEntityAlias();
$classLabel = $this->_classMetadata->getLabel();
$cypher = 'MATCH ('.$identifier.':'.$classLabel.') ';
$classLabels = $this->_classMetadata->getLabels();
$cypher = 'MATCH ('.$identifier.':'.\implode(':', $classLabels).') ';

$filter_cursor = 0;
$params = [];
Expand Down Expand Up @@ -188,7 +188,7 @@ private function getSimpleRelationshipStatement($alias, $sourceEntity)
$relationshipMeta = $this->_classMetadata->getRelationship($alias);
$relAlias = $relationshipMeta->getAlias();
$targetMetadata = $this->_em->getClassMetadataFor($relationshipMeta->getTargetEntity());
$targetClassLabel = $targetMetadata->getLabel();
$targetClassLabels = $targetMetadata->getLabels();
$targetAlias = $targetMetadata->getEntityAlias();
$sourceEntityId = $this->_classMetadata->getIdValue($sourceEntity);
$relationshipType = $relationshipMeta->getType();
Expand All @@ -199,7 +199,7 @@ private function getSimpleRelationshipStatement($alias, $sourceEntity)
$relPattern = sprintf('%s-[%s:`%s`]-%s', $isIncoming, $relAlias, $relationshipType, $isOutgoing);

$cypher = 'MATCH (n) WHERE id(n) = {id} ';
$cypher .= 'MATCH (n)'.$relPattern.'('.$targetAlias.($targetClassLabel != null ? ':' . $targetClassLabel : '').') ';
$cypher .= 'MATCH (n)'.$relPattern.'('.$targetAlias.($targetClassLabels != null ? ':' . \implode(':', $targetClassLabels) : '').') ';
$cypher .= 'RETURN '.$targetAlias;

$params = ['id' => (int) $sourceEntityId];
Expand Down Expand Up @@ -237,7 +237,7 @@ private function getSimpleRelationshipCollectionStatement($alias, $sourceEntity)
$relationshipMeta = $this->_classMetadata->getRelationship($alias);
$relAlias = $relationshipMeta->getAlias();
$targetMetadata = $this->_em->getClassMetadataFor($relationshipMeta->getTargetEntity());
$targetClassLabel = $targetMetadata->getLabel();
$targetClassLabels = $targetMetadata->getLabels();
$targetAlias = $targetMetadata->getEntityAlias();
$sourceEntityId = $this->_classMetadata->getIdValue($sourceEntity);
$relationshipType = $relationshipMeta->getType();
Expand All @@ -248,7 +248,7 @@ private function getSimpleRelationshipCollectionStatement($alias, $sourceEntity)
$relPattern = sprintf('%s-[%s:`%s`]-%s', $isIncoming, $relAlias, $relationshipType, $isOutgoing);

$cypher = 'MATCH (n) WHERE id(n) = {id} ';
$cypher .= 'MATCH (n)'.$relPattern.'('.$targetAlias.($targetClassLabel != null ? ':' . $targetClassLabel : '').') ';
$cypher .= 'MATCH (n)'.$relPattern.'('.$targetAlias.($targetClassLabels != null ? ':' . \implode(':', $targetClassLabels) : '').') ';
$cypher .= 'RETURN '.$targetAlias.' AS '.$targetAlias.' ';

if ($relationshipMeta->hasOrderBy()) {
Expand All @@ -263,8 +263,8 @@ private function getSimpleRelationshipCollectionStatement($alias, $sourceEntity)
private function getMatchOneByIdCypher($id)
{
$identifier = $this->_classMetadata->getEntityAlias();
$label = $this->_classMetadata->getLabel();
$cypher = 'MATCH ('.$identifier.':`'.$label.'`) WHERE id('.$identifier.') = {id} RETURN '.$identifier;
$labels = $this->_classMetadata->getLabels();
$cypher = 'MATCH ('.$identifier.':`'.\implode('`:`', $labels).'`) WHERE id('.$identifier.') = {id} RETURN '.$identifier;
$params = ['id' => (int) $id];

return Statement::create($cypher, $params);
Expand All @@ -274,11 +274,11 @@ private function getDegreeStatement($alias, $sourceEntity)
{
$relationshipMeta = $this->_classMetadata->getRelationship($alias);
$relAlias = $relationshipMeta->getAlias();
$targetClassLabel = '';
$targetClassLabels = '';
if ($relationshipMeta->isRelationshipEntity() === false && $relationshipMeta->isTargetEntity() === true) {
$targetMetadata = $this->_em->getClassMetadataFor($relationshipMeta->getTargetEntity());
if ($targetMetadata->getLabel() != null) {
$targetClassLabel = ':'.$targetMetadata->getLabel();
if ($targetMetadata->getLabels() != null) {
$targetClassLabels = ':'. \implode(':', $targetMetadata->getLabels());
}
}
$sourceEntityId = $this->_classMetadata->getIdValue($sourceEntity);
Expand All @@ -290,7 +290,7 @@ private function getDegreeStatement($alias, $sourceEntity)
$relPattern = sprintf('%s-[:`%s`]-%s', $isIncoming, $relationshipType, $isOutgoing);

$cypher = 'MATCH (n) WHERE id(n) = {id} ';
$cypher .= 'RETURN size((n)'.$relPattern.'('.$targetClassLabel.')) ';
$cypher .= 'RETURN size((n)'.$relPattern.'('.$targetClassLabels.')) ';
$cypher .= 'AS '.$alias;

return Statement::create($cypher, ['id' => $sourceEntityId]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Community/Issue103/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use GraphAware\Neo4j\OGM\Annotations as OGM;

/**
* @OGM\Node(label="Context")
* @OGM\Node(labels={"Context"})
*/
class Context
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Community/Issue103/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use GraphAware\Neo4j\OGM\Annotations as OGM;

/**
* @OGM\Node(label="Entity")
* @OGM\Node(labels={"Entity"})
*/
class Entity
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Community/Issue21/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Class TestUser.
*
* @OGM\Node(label="TestUser")
* @OGM\Node(labels={"TestUser"})
*/
class TestUser implements \JsonSerializable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testTimestampOnRelationshipEntityIsConverted()
* Class TimestampConverterEntity
* @package GraphAware\Neo4j\OGM\Tests\Integration\Convert
*
* @OGM\Node(label="Entity")
* @OGM\Node(labels={"Entity"})
*/
class TimestampConverterEntity
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/Base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Class User.
*
* @OGM\Node(label="User")
* @OGM\Node(labels={"User"})
*/
class User
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/BooleanLabel/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Class BlogPost.
*
* @OGM\Node(label="BlogPost")
* @OGM\Node(labels={"BlogPost"})
*/
class BlogPost
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Class Car.
*
* @OGM\Node(label="Car")
* @OGM\Node(labels={"Car"})
*/
class Car
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Class ModelNumber.
*
* @OGM\Node(label="ModelNumber")
* @OGM\Node(labels={"ModelNumber"})
*/
class ModelNumber
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Class Person.
*
* @OGM\Node(label="Person")
* @OGM\Node(labels={"Person"})
*/
class Person
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/ManyToManyRelationship/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Class Group.
*
* @OGM\Node(label="Group")
* @OGM\Node(labels={"Group"})
*/
class Group
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/ManyToManyRelationship/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Class User.
*
* @OGM\Node(label="User")
* @OGM\Node(labels={"User"})
*/
class User
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/ManyToOne/Bag.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Class Bag
* @package GraphAware\Neo4j\OGM\Tests\Integration\Models\ManyToOne
*
* @OGM\Node(label="Bag")
* @OGM\Node(labels={"Bag"})
*/
class Bag
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/ManyToOne/Woman.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Class Woman
* @package GraphAware\Neo4j\OGM\Tests\Integration\Models\ManyToOne
*
* @OGM\Node(label="Woman")
* @OGM\Node(labels={"Woman"})
*/
class Woman
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/MoviesDemo/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Class Movie.
*
* @OGM\Node(label="Movie", repository="GraphAware\Neo4j\OGM\Tests\Integration\Repository\MoviesCustomRepository")
* @OGM\Node(labels={"Movie"}, repository="GraphAware\Neo4j\OGM\Tests\Integration\Repository\MoviesCustomRepository")
*/
class Movie
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Models/MoviesDemo/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Class Person.
*
* @OGM\Node(label="Person")
* @OGM\Node(labels={"Person"})
*/
class Person
{
Expand Down
Loading