Browse files

added to import referencs with ->fromArray()

  • Loading branch information...
1 parent 930e1d4 commit 6a8dbb33c86a34fe9023527cf8a4cc94b4172607 @pablodip pablodip committed May 11, 2012
View
17 src/Mandango/Extension/templates/Core/DocumentFromToArray.php.twig
@@ -25,6 +25,23 @@
}
{% endif %}
{% endfor %}
+{# references one #}
+{% for name, reference in config_class.referencesOne %}
+{% if reference.inherited is not defined or not reference.inherited %}
+ if (isset($array['{{ name }}'])) {
+ $this->set{{ name|ucfirst }}($array['{{ name }}']);
+ }
+{% endif %}
+{% endfor %}
+{# references Many #}
+{% for name, reference in config_class.referencesMany %}
+{% if reference.inherited is not defined or not reference.inherited %}
+ if (isset($array['{{ name }}'])) {
+ $this->remove{{ name|ucfirst }}($this->get{{ name|ucfirst }}()->all());
+ $this->add{{ name|ucfirst }}($array['{{ name }}']);
+ }
+{% endif %}
+{% endfor %}
{# embeddeds one #}
{% for name, embedded in config_class.embeddedsOne %}
{% if embedded.inherited is not defined or not embedded.inherited %}
View
33 tests/Mandango/Tests/Extension/CoreDocumentTest.php
@@ -773,6 +773,39 @@ public function testFromArray()
$infosAdd = $commentsAdd[0]->getInfos()->getAdd();
}
+ public function testFromArrayReferencesOne()
+ {
+ $author = $this->mandango->create('Model\Author');
+ $article = $this->mandango->create('Model\Article');
+ $article->fromArray(array('author' => $author));
+
+ $this->assertSame($author, $article->getAuthor());
+ }
+
+ public function testFromArrayReferencesMany()
+ {
+ $categories = array(
+ $this->mandango->create('Model\Category'),
+ $this->mandango->create('Model\Category'),
+ );
+ $article = $this->mandango->create('Model\Article');
+ $article->fromArray(array('categories' => $categories));
+
+ $this->assertSame($categories, $article->getCategories()->all());
+ }
+
+ public function testFromArrayReferencesManyRemoveCurrent()
+ {
+ $category1 = $this->mandango->create('Model\Category');
+ $category2 = $this->mandango->create('Model\Category');
+
+ $article = $this->mandango->create('Model\Article');
+ $article->addCategories($category1);
+ $article->fromArray(array('categories' => array($category2)));
+
+ $this->assertSame(array($category2), $article->getCategories()->all());
+ }
+
public function testToArray()
{
$article = $this->mandango->create('Model\Article')

0 comments on commit 6a8dbb3

Please sign in to comment.