# Backend Speicherung von assoziierten Media Objekten

**URL:** <https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354>\
**Category:** Programmierung\
**Created:** [24. Februar 2018 um 12:59 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354 "2018-02-24T12:59:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Misengo](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/misengo/32/21446_2.png) [@Misengo](https://forum.shopware.com/u/Misengo)\
**Post date:** [24. Februar 2018 um 12:59 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354/1 "2018-02-24T12:59:12Z")

</div>

Ich habe im Backend meinen Controller abgebildet welcher unter anderem Bilder ablegt - das funktioniert auch alles - bis auf das speichern von Bilder:&nbsp;

```
:
"You're trying to decode an invalid JSON String: ↵Fatal error: Uncaught Doctrine\ORM\ORMInvalidArgumentException: Expected value of type "Shopware\Models\Media\Media" for association field "MEINPLUGIN\Models\Bug#$media", got "array" instead. in /var/www/shopware533/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php:206↵Stack trace:↵#0 /var/www/shopware533/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(844): Doctrine\ORM\ORMInvalidArgumentException::invalidAssociation(Object(Doctrine\ORM\Mapping\ClassMetadata), Array, Array)↵#1 /var/www/shopware533/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(741): Doctrine\ORM\UnitOfWork->computeAssociationChanges(Array, Array)↵#2 /var/www/shopware533/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(803): Doctrine\ORM\UnitOfWork->computeChangeSet(Object(Doctrine\ORM\Mapping\ClassMetadata), Object(MEINPLUGIN\Models\Bug))↵#3 /var/www/shopware533/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(340): Doctrine\ORM\UnitOfWork->computeChangeSets()↵#4 /var/www/shopware533/vendor/doctrine/orm/lib/D in /var/www/shopware533/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php on line 206↵"

```

&nbsp;

In meinem Model nutze ich:

```
/**
	 * @var
	 * @ORM\OneToOne(targetEntity="Shopware\Models\Media\Media")
	 * @ORM\JoinColumn(name="media_id", referencedColumnName="id", nullable=true)
	 */
	protected $media;

	/**
	 * @var int
	 * @ORM\Column(name="media_id", type="integer", nullable=true)
	 */
	private $mediaId = null;

	/**
	 * @return Media
	 */
	public function getMedia()
	{
		return $this->media;
	}

	/**
	 * @param Media $media
	 */
	public function setMedia($media)
	{
		$this->media = $media;
	}

```

&nbsp;

Was mache ich hier falsch?

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [24. Februar 2018 um 14:10 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354/2 "2018-02-24T14:10:06Z")

</div>

Wie sehen deine Getter- und Setter-Methoden für $media aus ?

Viele Grüße

---

<div class="post-metadata">

**Author:** ![Misengo](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/misengo/32/21446_2.png) [@Misengo](https://forum.shopware.com/u/Misengo)\
**Post date:** [24. Februar 2018 um 14:13 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354/3 "2018-02-24T14:13:17Z")

</div>

> [@simkli schrieb:](https://forum.shopware.com/profile/16741/simkli "simkli")
> 
> Wie sehen deine Getter- und Setter-Methoden für $media aus ?
> 
> Viele Grüße

Die sind oben mit drin, hier nochmal 🙂

&nbsp;

```
/**
	 * @return Media
	 */
	public function getMedia()
	{
		return $this->media;
	}

	/**
	 * @param Media $media
	 */
	public function setMedia($media)
	{
		$this->media = $media;
	}

```

Beste Grüße&nbsp;

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [24. Februar 2018 um 14:18 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354/4 "2018-02-24T14:18:30Z")

</div>

Hallo Misengo,  
schau’ mal hier: [Backend Components - Associations](https://developers.shopware.com/developers-guide/backend-components/associations/#the-product-model-(%5Cmodels%5Cproduct.php))

Dieser Artikel beschreibt Assoziationen. Beim Produktbeispiel sind die Attribute (protected $attribute; ) ebenfalls eine OneToOne-Assoziation.

Jetzt vergleiche mal deine Setter-Methode mit der aus dem Beispiel 😉

Viele Grüße

---

<div class="post-metadata">

**Author:** ![Misengo](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/misengo/32/21446_2.png) [@Misengo](https://forum.shopware.com/u/Misengo)\
**Post date:** [24. Februar 2018 um 19:20 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354/5 "2018-02-24T19:20:18Z")

</div>

> [@simkli schrieb:](https://forum.shopware.com/profile/16741/simkli "simkli")
> 
> Hallo Misengo,  
> schau’ mal hier: [https://developers.shopware.com/developers-guide/backend-components/associations/#the-product-model-](https://developers.shopware.com/developers-guide/backend-components/associations/?_ga=2.20516784.940218697.1519470595-1508825486.1514737756#the-product-model-)(\models\product.php)
> 
> Dieser Artikel beschreibt Assoziationen. Beim Produktbeispiel sind die Attribute (protected $attribute; ) ebenfalls eine OneToOne-Assoziation.
> 
> Jetzt vergleiche mal deine Setter-Methode mit der aus dem Beispiel ![;)](https://dub1.discourse-cdn.com/flex013/resources/emoji/wink.png ";)")
> 
> Viele Grüße

Hi simkli,

danke für deine Rückmeldung - dann bekomme ich aber&nbsp;

&nbsp;

```
 Uncaught Error: Call to undefined method Shopware\Proxies\ __CG__ \Shopware\Models\Media\Media::setBug() in /var/www/shopware533/engine/Shopware/Components/Model/ModelEntity.php

/**
	 * @return Media
	 */
	public function getMedia()
	{
		return $this->media;
	}

	/**
	 * @param Media $media
	 * @return \Shopware\Components\Model\ModelEntity
	 */
	public function setMedia($media)
	{
		return $this->setOneToOne(
			$media,
			Media::class,
			'media',
			'bug'
		);
	}

```

&nbsp;

Ich kann ja keinen setter in das Media Model einbinden. Bin etwas verwirrt.

---

<div class="post-metadata">

**Author:** ![simkli](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/simkli/32/13124_2.png) [@simkli](https://forum.shopware.com/u/simkli)\
**Post date:** [24. Februar 2018 um 20:47 UTC](https://forum.shopware.com/t/backend-speicherung-von-assoziierten-media-objekten/51354/6 "2018-02-24T20:47:15Z")

</div>

> Ich kann ja keinen setter in das Media Model einbinden. Bin etwas verwirrt.

Ja, das ist richtig. Das kannst du nur bei deinen eigenen Models machen. Daher setzte den letzten Parameter (reference) einfach auf null, bzw. lass‘ ihn weg&nbsp; ![Wink](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/b3785da0cbf2c8566cb535dcf55b2730b5224899.png "Wink")

Viele Grüße
