Permalink
Browse files

Fix Mongo\Mapper and setTargetModel in Relation\BelongsTo.

Tests for fetchRelations.
  • Loading branch information...
Bukarinov committed Dec 29, 2012
1 parent 03e418d commit 1880698bee07766d7067bf825402bea73892fe1f
@@ -22,7 +22,7 @@ public function fetchRelations($relationNames = null, $refresh = false)
$relationNames = $this->parseRelationNames($relationNames);
@shumkov

shumkov Dec 29, 2012

Member

проверяем есть ли вообще релейшены

}
- foreach ($relationNames as $relationName => $childRelations) {
+ foreach ((array) $relationNames as $relationName => $childRelations) {
$relation = $this->getRelation($relationName);
if ($relation !== null) {
$relation->setTargetObjectsToCollection($this, $refresh, $childRelations);
@@ -50,7 +50,7 @@ public function setTargetModel(ModelInterface $targetModel = null)
throw new \InvalidArgumentException('Target property is null');
}
- $this->targetModel = false;
+ $this->targetModel = $targetModel;
} else {
$targetPropertyValue = null;
@@ -101,7 +101,12 @@ public function setTargetObjectsToCollection(Collection $collection, $refresh =
foreach ($collection as $model) {
/* @var $model \GeometriaLab\Model\Persistent\AbstractModel */
$relation = $model->getRelation($this->getProperty()->getName());
- if ($refresh || !$relation->hasTargetModel()) {
+ if ($relation instanceof HasMany) {
+ $hasTargetModel = $relation->hasTargetModels();
+ } else {
+ $hasTargetModel = $relation->hasTargetModel();
+ }
+ if ($refresh || !$hasTargetModel) {
// TODO '0' value will not pass check, should it?
$value = $model->get($this->getProperty()->getOriginProperty());
if ($value) {
@@ -114,13 +114,18 @@ public function setTargetObjectsToCollection(Collection $collection, $refresh =
foreach ($collection as $model) {
/* @var $model \GeometriaLab\Model\Persistent\AbstractModel */
// TODO 0 value will not pass check, should it ?
- $values = $model->get($this->getProperty()->getOriginProperty());
+ $values = (array) $model->get($this->getProperty()->getOriginProperty());
@shumkov

shumkov Dec 29, 2012

Member

сомнительный кусок

if ($values) {
$relation = $model->getRelation($this->getProperty()->getName());
- if ($refresh || !$relation->hasTargetModel()) {
+ if ($relation instanceof HasMany) {
+ $hasTargetModel = $relation->hasTargetModels();
+ } else {
+ $hasTargetModel = $relation->hasTargetModel();
+ }
+ if ($refresh || !$hasTargetModel) {
$localModels[] = array(
'model' => $model,
- 'values' => (array) $values,
+ 'values' => $values,
);
$fetchValues = array_merge($fetchValues, $values);
}
@@ -109,7 +109,12 @@ public function setTargetObjectsToCollection(Collection $collection, $refresh =
foreach ($collection as $model) {
/* @var $model \GeometriaLab\Model\Persistent\AbstractModel */
$relation = $model->getRelation($this->getProperty()->getName());
- if ($refresh || !$relation->hasTargetModel()) {
+ if ($relation instanceof HasMany) {
+ $hasTargetModel = $relation->hasTargetModels();
+ } else {
+ $hasTargetModel = $relation->hasTargetModel();
+ }
+ if ($refresh || !$hasTargetModel) {
// TODO '0' value will not pass check, should it?
$value = $model->get($this->getProperty()->getOriginProperty());
if ($value) {
@@ -542,10 +542,24 @@ static public function setServiceManager(ZendServiceManager $serviceManager)
protected function transformModelDataForStorage(array $data)
{
if (isset($data['id'])) {
- $data['_id'] = new \MongoId($data['id']);
- if ((string)$data['_id'] != $data['id']) {
+ if (is_array($data['id'])) {
+ foreach ($data['id'] as &$value) {
+ if (is_array($value)) {
+ foreach ($value as &$val) {
+ $val = new \MongoId($val);
+ }
+ } else {
+ $value = new \MongoId($value);
+ }
+ }
$data['_id'] = $data['id'];
+ } else {
+ $data['_id'] = new \MongoId($data['id']);
+ if ((string)$data['_id'] != $data['id']) {
+ $data['_id'] = $data['id'];
+ }
}
+
unset($data['id']);
}
@@ -2,15 +2,180 @@
namespace GeometriaLabTest\Model\Persistent;
-use GeometriaLab\Model\Persistent\Collection,
- GeometriaLabTest\Model\Persistent\TestModels\Model;
+use GeometriaLabTest\Mongo\AbstractTestCase,
+ GeometriaLabTest\Model\Persistent\TestModels\Relations\Man,
+ GeometriaLabTest\Model\Persistent\TestModels\Relations\Dog,
+ GeometriaLabTest\Model\Persistent\TestModels\Relations\Woman;
-class CollectionTest extends \PHPUnit_Framework_TestCase
+class CollectionTest extends AbstractTestCase
{
- public function testFetchRelations()
+ public function testFetchAllRelations()
{
- $this->setExpectedException('\RuntimeException', 'Not implemented yet!');
- $c = new Collection();
- $c->fetchRelations(array('foo'));
+ $this->createModels();
+ $this->createModels();
+
+ $collection = Man::getMapper()->getAll();
+
+ foreach ($collection as $model) {
+ /* @var Man $model */
+ $this->assertFalse($model->getRelation('dog')->hasTargetModel());
+ $this->assertFalse($model->getRelation('women')->hasTargetModels());
+ }
+
+ $collection->fetchRelations(
+ null
+ );
+
+ foreach ($collection as $model) {
+ /* @var Man $model */
+ $this->assertTrue($model->getRelation('dog')->hasTargetModel());
+ $this->assertTrue($model->getRelation('women')->hasTargetModels());
+ }
+ }
+
+ public function testFetchOneRelation()
+ {
+ $this->createModels();
+ $this->createModels();
+
+ $collection = Man::getMapper()->getAll();
+
+ foreach ($collection as $model) {
+ /* @var Man $model */
+ $this->assertFalse($model->getRelation('dog')->hasTargetModel());
+ $this->assertFalse($model->getRelation('women')->hasTargetModels());
+ }
+
+ $collection->fetchRelations(
+ 'dog'
+ );
+
+ foreach ($collection as $model) {
+ /* @var Man $model */
+ $this->assertTrue($model->getRelation('dog')->hasTargetModel());
+ $this->assertFalse($model->getRelation('women')->hasTargetModels());
+ }
+ }
+
+ public function testNestedHasOneRelation()
+ {
+ $this->createModels();
+ $this->createModels();
+
+ $collection = Man::getMapper()->getAll();
+
+ foreach ($collection as $model) {
+ /* @var Man $model */
+ $this->assertFalse($model->getRelation('dog')->hasTargetModel());
+ }
+
+ $collection->fetchRelations(array(
+ 'dog' => 'man',
+ ));
+
+ foreach ($collection as $model) {
+ /* @var Man $model */
+ $this->assertTrue($model->getRelation('dog')->hasTargetModel());
+ $this->assertTrue($model->get('dog')->getRelation('man')->hasTargetModel());
+ }
+ }
+
+ public function testNestedBelongsToRelation()
+ {
+ $this->createModels();
+ $this->createModels();
+
+ $collection = Dog::getMapper()->getAll();
+
+ foreach ($collection as $model) {
+ /* @var Dog $model */
+ $this->assertFalse($model->getRelation('man')->hasTargetModel());
+ }
+
+ $collection->fetchRelations(array(
+ 'man' => 'women',
+ ));
+
+ foreach ($collection as $model) {
+ /* @var Dog $model */
+ $this->assertTrue($model->getRelation('man')->hasTargetModel());
+ $this->assertTrue($model->get('man')->getRelation('women')->hasTargetModels());
+
+ foreach ($model->get('man')->get('women') as $woman) {
+ $this->assertInstanceOf('\GeometriaLabTest\Model\Persistent\TestModels\Relations\Woman', $woman);
+ }
+ }
+ }
+
+ public function testNestedHasManyRelation()
+ {
+ $this->createModels();
+ $this->createModels();
+
+ $query = Woman::getMapper()->createQuery();
+ $collection = Woman::getMapper()->getAll($query);
+
+ foreach ($collection as $model) {
+ /* @var Woman $model */
+ $this->assertFalse($model->getRelation('man')->hasTargetModel());
+ }
+
+ $collection->fetchRelations(array(
+ 'man' => 'dog',
+ ));
+
+ foreach ($collection as $model) {
+ /* @var Woman $model */
+ $this->assertTrue($model->getRelation('man')->hasTargetModel());
+ $this->assertTrue($model->get('man')->getRelation('dog')->hasTargetModel());
+ }
+ }
+
+ public function testNotExistentRelation()
+ {
+ $this->setExpectedException("\\InvalidArgumentException", "Model doesn't have 'bad' relation");
+
+ $this->createModels();
+
+ $collection = Woman::getMapper()->getAll();
+ $collection->fetchRelations('bad');
+ }
+
+ public function testBadChildRelationName()
+ {
+ $this->setExpectedException("\\InvalidArgumentException", "Child relation must be a string but integer is given.");
+
+ $this->createModels();
+
+ $collection = Woman::getMapper()->getAll();
+ $collection->fetchRelations(array('man' => 1));
+ }
+
+ protected function createModels()
+ {
+ $philip = new Man(array(
+ 'name' => 'Philip',
+ ));
+ $philip->save();
+
+ $seymour = new Dog(array(
+ 'name' => 'Seymour',
+ 'manId' => $philip->id,
+ ));
+ $seymour->save();
+
+ $michelle = new Woman(array(
+ 'name' => 'Michelle',
+ 'manId' => $philip->id,
+ ));
+ $michelle->save();
+
+ $leela = new Woman(array(
+ 'name' => 'Leela',
+ 'manId' => $philip->id,
+ ));
+ $leela->save();
+
+
}
}
@@ -0,0 +1,18 @@
+<?php
+
+namespace GeometriaLabTest\Model\Persistent\TestModels\Relations;
+
+use GeometriaLab\Model\Persistent\AbstractModel;
+
+/**
+ * @property string $id {"primary": true}
+ * @property string $name
+ * @property string $manId
+ * @property \GeometriaLabTest\Model\Persistent\TestModels\Relations\Man $man {"relation": "belongsTo", "originProperty": "manId"}
+ *
+ * @method static \GeometriaLab\Mongo\Model\Mapper getMapper() {"mongoInstanceName": "default", "collectionName": "dog"}
+ */
+class Dog extends AbstractModel
+{
+
+}
@@ -0,0 +1,21 @@
+<?php
+
+namespace GeometriaLabTest\Model\Persistent\TestModels\Relations;
+
+use GeometriaLab\Model\Persistent\AbstractModel;
+
+/**
+ * @property string $id {"primary": true}
+ * @property string $name
+ *
+ * @property \GeometriaLabTest\Model\Persistent\TestModels\Relations\Dog $dog {"relation": "hasOne", "targetProperty": "manId"}
+ * @property \GeometriaLab\Model\Persistent\Collection $women {"relation": "hasMany",
+ * "targetProperty": "manId",
+ * "targetModelClass": "\\GeometriaLabTest\\Model\\Persistent\\TestModels\\Relations\\Woman"}
+ *
+ * @method static \GeometriaLab\Mongo\Model\Mapper getMapper() {"mongoInstanceName": "default", "collectionName": "man"}
+ */
+class Man extends AbstractModel
+{
+
+}
@@ -0,0 +1,18 @@
+<?php
+
+namespace GeometriaLabTest\Model\Persistent\TestModels\Relations;
+
+use GeometriaLab\Model\Persistent\AbstractModel;
+
+/**
+ * @property string $id {"primary": true}
+ * @property string $name
+ * @property string $manId
+ * @property \GeometriaLabTest\Model\Persistent\TestModels\Relations\Man $man {"relation": "belongsTo", "originProperty": "manId"}
+ *
+ * @method static \GeometriaLab\Mongo\Model\Mapper getMapper() {"mongoInstanceName": "default", "collectionName": "woman"}
+ */
+class Woman extends AbstractModel
+{
+
+}

0 comments on commit 1880698

Please sign in to comment.