Prechádzať zdrojové kódy

[tests] explanation related to issue, closes #75

gediminasm 14 rokov pred
rodič
commit
2d109e8a8b

+ 19 - 5
tests/Gedmo/Translatable/Fixture/Issue75/Article.php

@@ -34,6 +34,13 @@ class Article
      */
     private $files;
 
+    public function __construct()
+    {
+        // $images is not an array, its a collection
+        // if you want to do such operations you have to cunstruct it
+        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
+    }
+
     public function getId()
     {
         return $this->id;
@@ -43,11 +50,18 @@ class Article
     {
         $this->images[] = $image;
     }
-	
-	public function setImages(array $images)
-	{
-		$this->images = $images;
-	}
+
+    public function setImages(array $images)
+    {
+        foreach ($images as $img) {
+            // first check if it does not contain it allready
+            // because all entity objects are allready in memory
+            // simply $em->find('Image', $id); and you will get it from this collection
+            if (!$this->images->contains($img)) {
+                $this->addImage($img);
+            }
+        }
+    }
 
     public function getImages()
     {

+ 16 - 11
tests/Gedmo/Translatable/Issue75Test.php

@@ -40,29 +40,34 @@ class Issue75Test extends BaseTestCaseORM
 
     public function testIssue75()
     {
-    	$repo = $this->em->getRepository(self::TRANSLATION);
-	
-		// Step1: article creation in default locale
-		$image1 = new Image;
+        $repo = $this->em->getRepository(self::TRANSLATION);
+
+        // Step1: article creation in default locale
+        $image1 = new Image;
         $image1->setTitle('img1');
         $this->em->persist($image1);
 
         $image2 = new Image;
         $image2->setTitle('img2');
         $this->em->persist($image2);
-		
+
         $article = new Article;
         $article->setTitle('en art');
-		$article->setImages(array($image1, $image2));
+        // images is not an array
+        $article->setImages(array($image1, $image2));
         $this->em->persist($article);
-		
-		$this->em->flush();
-		//Step2: article update in another locale
-		$article = $this->em->find(self::ARTICLE, $article->getId());
+
+        $this->em->flush();
+        //Step2: article update in another locale
+        $article = $this->em->find(self::ARTICLE, $article->getId());
         $image1 = $this->em->find(self::IMAGE, $image1->getId());
         $image2 = $this->em->find(self::IMAGE, $image2->getId());
         $article->setTitle('en updated');
-		$article->setImages(array($image1, $image2));
+        /**
+         * here you duplicate the objects in collection, it allready
+         * contains them. Read more about doctrine collections
+         */
+        $article->setImages(array($image1, $image2));
         $this->em->persist($article);
         $this->em->flush();
     }