# Verhalten von Model Objekten

**URL:** https://forum.shopware.com/t/verhalten-von-model-objekten/35733
**Category:** Programmierung
**Created:** [2. April 2016 um 11:41 UTC](https://forum.shopware.com/t/verhalten-von-model-objekten/35733 "2016-04-02T11:41:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![maxxyoo](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@maxxyoo](https://forum.shopware.com/u/maxxyoo)
#### Post date: [2. April 2016 um 11:41 UTC](https://forum.shopware.com/t/verhalten-von-model-objekten/35733/1 "2016-04-02T11:41:25Z")

</div>

Hallo zusammen,

die for Schleife soll 19 Bilder aus dem Ressourcen Ordner eines&nbsp;Plugins in den DB bzw. einen Ordner schreiben.

Die Frage nun: Warum steht in DB und Ordner nur das letzte Bild “example19” … warum nicht die 18 davor… ich nulle die Objekte sogar… und oben bei fileObject kommt je Iteration ein neues Bild an die Reihe?? oder gibt es gar eine setFile **s** Methode?

&nbsp;

```
for( $i = 1; $i < 20 ; $i++) {
				$fileObject = new \Symfony\Component\HttpFoundation\File\File($example_sprite.'\example'. $i .'.jpg');
				$album = Shopware()->Models()->find('Shopware\Models\Media\Album', $insertLastId2);
				
				$media = Shopware()->Models()->find("Shopware\\Models\\Media\\Media", $insertLastId2);
				$media->setAlbum($album);
				$media->setDescription('example');
				$media->setCreated(new DateTime());
				$media->setExtension('jpg');
				$identity = Shopware()->Auth()->getIdentity();
				if ($identity !== null) {
					$media->setUserId($identity->id);
				} else {
					$media->setUserId(0);
				}
				//set the upload file into the model. The model saves the file to the directory
				$media->setFile($fileObject);
				Shopware()->Models()->persist($media);
				Shopware()->Models()->flush();
				
				$fileObject = null;
				$album = null;
				$media = null;
				$identity = null;
}

```

Vielen Dank für ein mögliches kurzes Feedback!

---

<div class="post-metadata">

### Author: ![hec](https://avatars.discourse-cdn.com/v4/letter/h/e274bd/32.png) [@hec](https://forum.shopware.com/u/hec)
#### Post date: [3. April 2016 um 10:51 UTC](https://forum.shopware.com/t/verhalten-von-model-objekten/35733/2 "2016-04-03T10:51:14Z")

</div>

Hallo,

da du den Wert für $insertLastId2 nicht veränderst, suchst du dir mit

```
$media = Shopware()->Models()->find("Shopware\\Models\\Media\\Media", $insertLastId2);

```

immer dasselbe Objekt, dessen File du dann überschreibst.

Grüße,  
Sven

---

<div class="post-metadata">

### Author: ![maxxyoo](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@maxxyoo](https://forum.shopware.com/u/maxxyoo)
#### Post date: [3. April 2016 um 18:31 UTC](https://forum.shopware.com/t/verhalten-von-model-objekten/35733/3 "2016-04-03T18:31:06Z")

</div>

Das hat nunmehr hier funktioniert. Vielen herzlichen Dank!
