Skip to content

Commit

Permalink
Add support for Ancestors.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwhelan committed Sep 14, 2015
1 parent a25d73a commit a02c634
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 8 deletions.
54 changes: 54 additions & 0 deletions src/Ancestors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Array wrapper to both allow for property-based access to Ancestors for
* writing during entity creation as well as enforcing read-only status
* for objects that have already been created that have ancestors.
*/
namespace Datachore;
use \google\appengine\datastore\v4\Key as GoogleKey;
use \google\appengine\datastore\v4\PathElement as GooglePathElement;

class Ancestors extends \ArrayObject
{
protected $read_only = false;


public function offsetSet($index, $newval)
{
if ($this->read_only)
{
throw new \InvalidArgumentException("Cannot modify ancestors once an entity is already created");
}

if ($index && (!is_numeric($index) || ($index != (int)$index)))
{
throw new \InvalidArgumentException("only numeric indexes are allowed: {$index}");
}

if ($newval instanceof Model)
{
$newval = $newval->key->getPathElement(
$newval->key->getPathElementSize()-1
);
}
else if (!$newval instanceof GooglePathElement)
{
throw new \InvalidArgumentException("Illegal value for ancestor");
}

parent::offsetSet($index, $newval);
}

public function __construct(GoogleKey $key = null)
{
if ($key)
{
for ($i = 0; $i < $key->getPathElementSize() - 1; $i++)
{
$this->offsetSet($i, $key->getPathElement($i));
}
$this->read_only = true;
}
}
}
11 changes: 9 additions & 2 deletions src/Datachore.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function save($trans = null)
//$this->_GoogleKeyValue($mutation->mutableKey());

$entity = $trans->mutation->addInsertAutoId();
$this->_GoogleKeyValue($entity->mutableKey());
$this->_GoogleKeyValue($entity->mutableKey(), null, $this->ancestors);
$trans->insertauto[] = $this;
}

Expand Down Expand Up @@ -274,9 +274,16 @@ public function delete($trans = null)
];


final protected function _GoogleKeyValue(\google\appengine\datastore\v4\Key $key, $id = null)
final protected function _GoogleKeyValue(\google\appengine\datastore\v4\Key $key, $id = null, $ancestors)
{
$partitionId = $key->mutablePartitionId();


foreach ($ancestors as $ancestor)
{
$path = $key->addPathElement();
$path->mergeFrom($ancestor);
}
$path = $key->addPathElement();

$partitionId->setDatasetId($this->datasetId());
Expand Down
39 changes: 33 additions & 6 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
* property must be modeled using one of the Datachore\Type classes.
*/
namespace Datachore;
use \google\appengine\datastore\v4\Key as GoogleKey;

class Model extends Datachore
{
/** private super key **/
protected $__key = null;

/** ancestors */
protected $ancestors = null;

/** Property definitions **/
protected $properties = [];

Expand All @@ -25,19 +29,27 @@ public function __get($key)
{
if ($key == 'id')
{
if ($this->__key)
{
return $this->__key->getPathElement(0)->getId();
}
else
if (!$this->__key)
{
return null;
}
return $this->__key
->getPathElement($this->__key->getPathElementSize()-1)
->getId();
}
else if ($key == 'key')
{
return $this->__key;
}
else if ($key == 'ancestors')
{
if ($this->ancestors == null)
{
$this->ancestors = new Ancestors($this->__key);
}

return $this->ancestors;
}
else if (isset($this->properties[$key]))
{
return $this->properties[$key]->get();
Expand All @@ -59,6 +71,20 @@ public function __set($key, $val)
throw new \InvalidArgumentException("Invalid Type for Key");
}
}
else if ($key == 'ancestors')
{
if (!is_array($val))
{
$val = [$val];
}

$this->ancestors = new Ancestors;
print_r($val);
foreach ($val as $ancestor)
{
$this->ancestors[] = $ancestor;
}
}
else if (isset($this->properties[$key]))
{
$this->properties[$key]->set($val);
Expand Down Expand Up @@ -148,6 +174,8 @@ final public function __construct($entity = null)
if ($entity)
{
$this->__key = $entity->entity->getKey();
$this->ancestors = new Ancestors($this->__key);

foreach($entity->entity->getPropertyList() as $property)
{
$value = new Value($property->getValue());
Expand All @@ -159,7 +187,6 @@ final public function __construct($entity = null)
}

$this->properties[$property->getName()]->set($raw);
//);
}
}

Expand Down

0 comments on commit a02c634

Please sign in to comment.