Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bugs on inheritance system and add free primary key capability #98

Open
wants to merge 4 commits 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
49 changes: 36 additions & 13 deletions lib/HireVoice/Neo4j/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,24 @@ function findAny($id)
*/
public function load($node)
{
if (! isset($this->loadedNodes[$node->getId()])) {
if ($node instanceof Proxy\Entity) {
$pk = $this->getPrimaryKeyValue($node);
}else{
$pk = $node->getId();
}

if (! isset($this->loadedNodes[$pk])) {
$em = $this;

$entity = $this->proxyFactory->fromNode($node, $this->metaRepository, function ($node) use ($em) {
return $em->load($node);
});

$this->loadedNodes[$node->getId()] = $entity;
$this->loadedNodes[$pk] = $entity;
$this->nodes[$this->getHash($entity)] = $node;
}

return $this->loadedNodes[$node->getId()];
return $this->loadedNodes[$pk];
}

/**
Expand All @@ -287,9 +293,9 @@ public function load($node)
public function reload($entity)
{
if ($entity instanceof Proxy\Entity) {
return $this->load($this->findAny($entity->getId()));
return $this->load($this->findAny($this->getPrimaryKeyValue($entity)));
} else {
return $this->find(get_class($entity), $entity->getId());
return $this->find(get_class($entity), $this->getPrimaryKeyValue($entity));
}
}

Expand Down Expand Up @@ -536,7 +542,7 @@ private function traverseRelations($entity, $addCallback, $removeCallback = null
if ($property->isTraversed()) {
if ($entry = $property->getValue($entity)) {
if ($removeCallback) {
$this->removePreviousRelations($entity, $property->getName(), $entry);
$this->removePreviousRelations($entity, $property->getName(), $property->getDirection(), $entry);
}
$addCallback($entry, $property->getName(), $property->getDirection());
}
Expand Down Expand Up @@ -616,7 +622,7 @@ private function writeRelations()
{
$this->begin();
foreach ($this->entities as $entity) {
$this->writeRelationsFor($entity);
$this->writeRelationsFor($entity);
}
$this->commit();
}
Expand Down Expand Up @@ -653,14 +659,20 @@ private function writeRelationsFor($entity)
$this->traverseRelations($entity, $addCallback, $removeCallback);
}

private function removePreviousRelations($from, $relation, $exception)
private function removePreviousRelations($from, $relation, $direction, $exception)
{
$node = $this->getLoadedNode($from);

foreach ($this->getRelationsFrom($node, $relation) as $r) {
if (basename($r['end']) != $exception->getId()) {
if(strtolower($direction) == 'to'){
$rstart = basename($r['start']);
}else{
$rstart = basename($r['end']);
}

if ($rstart != $this->getPrimaryKeyValue($exception)){
$this->deleteRelationship($r);
}
}
}
}

Expand Down Expand Up @@ -761,7 +773,7 @@ private function index($entity)

$class = $meta->getName();
$mainIndex = $this->createIndex($class);
$mainIndex->add($node, 'id', $entity->getId());
$mainIndex->add($node, 'id', $this->getPrimaryKeyValue($entity));
}

private function writeIndexes()
Expand Down Expand Up @@ -796,7 +808,7 @@ private function removeIndexes(){
}
$class = $meta->getName();
$mainIndex = $this->createIndex($class);
$mainIndex->remove($node, 'id', $entity->getId());
$mainIndex->remove($node, 'id', $this->getPrimaryKeyValue($entity));
}
}
/**
Expand Down Expand Up @@ -844,4 +856,15 @@ public function getPathFinder()
{
return clone $this->pathFinder;
}

/**
* @return \HireVoice\Neo4j\Meta\Property
*/
public function getPrimaryKeyValue($entity)
{
$meta = $this->getMeta($entity);
$pk = $meta->getPrimaryKey();
$pkGetter = 'get'.ucfirst($pk->getName());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reflected property should allow to read the value directly from the property rather than relying on the naming convention. There used to be a lot of this, but it has since been removed.

I fail to understand the purpose of the change. Is it not possible to use @auto on an arbitrary property?

return $entity->$pkGetter();
}
}
5 changes: 4 additions & 1 deletion lib/HireVoice/Neo4j/Proxy/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ private function __loadProperty(\$name, \$propertyName)
\$root = \$relation['end'];
}

if (basename(\$root) == \$this->getId()) {
\$pk = \$this->neo4j_meta->getPrimaryKey();
\$pkGetter = 'get'.ucfirst(\$pk->getName());

if (basename(\$root) == \$this->\$pkGetter()) {
\$node = \$this->neo4j_node->getClient()->getNode(basename(\$nodeUrl));
\$loader = \$this->neo4j_loadCallback;
\$collection->add(\$loader(\$node));
Expand Down
83 changes: 83 additions & 0 deletions lib/HireVoice/Neo4j/Tests/Entity/Olive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright (C) 2012 Louis-Philippe Huberdeau
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

namespace HireVoice\Neo4j\Tests\Entity;
use HireVoice\Neo4j\Annotation as OGM;

/**
* @OGM\Entity(labels="Olive")
*/
class Olive
{
/**
* @OGM\Auto
*/
protected $veryStrangePrimaryKey;

/**
* @OGM\Property
* @OGM\Index
*/
protected $name;


/**
* @param mixed $veryStrangePrimaryKey
*
* @return Olive
*/
public function setVeryStrangePrimaryKey($veryStrangePrimaryKey)
{
$this->veryStrangePrimaryKey = $veryStrangePrimaryKey;
return $this;
}

/**
* @return mixed
*/
public function getVeryStrangePrimaryKey()
{
return $this->veryStrangePrimaryKey;
}

/**
* @param mixed $name
*
* @return Olive
*/
public function setName($name)
{
$this->name = $name;
return $this;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

}

145 changes: 145 additions & 0 deletions lib/HireVoice/Neo4j/Tests/Entity/Pasta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php
/**
* Copyright (C) 2012 Louis-Philippe Huberdeau
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

namespace HireVoice\Neo4j\Tests\Entity;
use HireVoice\Neo4j\Annotation as OGM;

/**
* @OGM\Entity(labels="Pasta")
*/
class Pasta
{
/**
* @OGM\Auto
*/
protected $strangePrimaryKey;

/**
* @OGM\Property
* @OGM\Index
*/
protected $name;

/**
* @OGM\ManyToMany(relation="ingredient")
*/
protected $olives;

/**
* @OGM\ManyToOne(relation="base_ingredient")
*/
protected $tomato;

/**
* Constructor that initializes references
*
*/
public function __construct(){
$this->olives = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @param mixed $strangePrimaryKey
*
* @return Pasta
*/
public function setStrangePrimaryKey($strangePrimaryKey)
{
$this->strangePrimaryKey = $strangePrimaryKey;
return $this;
}

/**
* @return mixed
*/
public function getStrangePrimaryKey()
{
return $this->strangePrimaryKey;
}

/**
* @param mixed $name
*
* @return Pasta
*/
public function setName($name)
{
$this->name = $name;
return $this;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @param mixed $olives
*
* @return Pasta
*/
public function setOlives($olives)
{
$this->olives = $olives;
return $this;
}

/**
* @return mixed
*/
public function getOlives()
{
return $this->olives;
}

/**
*
* @param
*/
public function addOlive($olive)
{
$this->olives[] = $olive;
}
/**
* @param mixed $tomato
*
* @return Pasta
*/
public function setTomato($tomato)
{
$this->tomato = $tomato;
return $this;
}

/**
* @return mixed
*/
public function getTomato()
{
return $this->tomato;
}

}

Loading