# Eigener Controller: Richtige Verwendung der Shopware Models?

**URL:** <https://forum.shopware.com/t/eigener-controller-richtige-verwendung-der-shopware-models/11419>\
**Category:** Shopware 3.5\
**Tags:** programming\
**Created:** [5. Februar 2013 um 21:50 UTC](https://forum.shopware.com/t/eigener-controller-richtige-verwendung-der-shopware-models/11419 "2013-02-05T21:50:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Basti](https://forum.shopware.com/u/Basti)\
**Post date:** [5. Februar 2013 um 21:50 UTC](https://forum.shopware.com/t/eigener-controller-richtige-verwendung-der-shopware-models/11419/1 "2013-02-05T21:50:04Z")

</div>

Hallo, ich entwickle gerade einen eigenen Controller, in dem z. B. die Email des Kunden geändert werden kann. Um die E-Mail auszulesen nutze ich die selbsgeschriebene Funktion getCustomerEmail($customerId) und um die Daten wieder zurück zu schreiben setCustomerEmail($customerId, $email). Dabei verwende ich den Doctrine Query Builder, das funktioniert auch soweit. Das scheint aber nicht der vorgesehene Weg zu sein. Wie kann die gewünschte Funktionalität mit den Funktionen getEmail() und setEmail(string $email) des Shopwaremodels [\ Shopware \Models \Customer \Customer](http://wiki.shopware.de/%5C_doc-sw4/classes/Shopware.Models.Customer.Customer.html) realisiert werden? Auch wird der User im Frontend nach jeder Änderung ausgeloggt. Wie kann ich das verhindern? Verwende Shopware 4.0.6 RC2. [Das ganze Plugin downloaden.](http://shopware.bamoo.de/swagMeincontroller.zip) Meincontroller.php [code]\<?php class Shopware\_Controllers\_Frontend\_Meincontroller extends Enlight\_Controller\_Action  
{

```
protected $admin;

public function init()
{
     $this->admin = Shopware()-\>Modules()-\>Admin(); $this-\>View()-\>addTemplateDir(dirname(\_\_FILE\_\_) . "/../../Views/"); $this-\>View()-\>sUserData = $this-\>admin-\>sGetUserData(); $this-\>View()-\>loadTemplate("frontend/index.tpl"); } /\* Es besteht auch die Möglichkeit nach erfolgreicher Speicherung \* der Daten auf einen eine eigene Action zu verweisen, die ein \* eigenes Template lädt. Die Weiterleitung ist weiter unten auskommentiert public function successAction(){ $this-\>View()-\>loadTemplate("frontend/success.tpl"); $this-\>View()-\>sSuccess = true; } \*/ public function indexAction() { $customerModel = Shopware()-\>Models()-\>getRepository('Shopware\Models\Customer\Customer'); $customerModel-\>findOneBy(array('id' =\> intval($userID = Shopware()-\>Session()-\>sUserId))); $this-\>View()-\>sCustomerMail = $this-\>getCustomerEmail(Shopware()-\>Session()-\>sUserId); if ($this-\>Request()-\>getPost("sCustomerMail")) { $variables["sError"] = false; $validator = new Zend\_Validate\_EmailAddress(); if (!$validator-\>isValid($this-\>Request()-\>getPost("sCustomerMail"))) { $variables["sError"] = true; } if ($variables["sError"] == false) { if ($this-\>Request()-\>getPost("sCustomerMail")) { $this-\>setCustomerEmail(Shopware()-\>Session()-\>sUserId, $this-\>Request()-\>getPost("sCustomerMail")); } $this-\>View()-\>sSuccess = true; /\* Siehe sucessAction() \* $url = $this-\>Front()-\>Router()-\>assemble(array('controller' =\> 'Meincontroller', 'action' =\> 'success')); $this-\>redirect($url); \*/ } else { $this-\>View()-\>sError = true; $this-\>View()-\>sCustomerMail = $this-\>Request()-\>getPost("sCustomerMail"); } } } function getCustomerEmail($customerId) { //creates an empty query builder object $builder = Shopware()-\>Models()-\>createQueryBuilder(); //add the select and from path for the query $builder-\>select(array('customer')) -\>from('Shopware\Models\Customer\Customer', 'customer') -\>where('customer.id = :customerId') -\>setParameter('customerId', $customerId) -\>setFirstResult(0) -\>setMaxResults(1); //get generated query object from the builder object $query = $builder-\>getQuery(); $customer = $query-\>getOneOrNullResult( \Doctrine\ORM\AbstractQuery::HYDRATE\_ARRAY ); $customerEmail = $customer['email']; return $customerEmail; } function setCustomerEmail($customerId, $email) { $query = Shopware()-\>Models()-\>createQueryBuilder(); $queryupdate = $query-\>update('Shopware\Models\Customer\Customer', 'customer') -\>set('customer.email', $query-\>expr()-\>literal($email) ) -\>where('customer.id = ?1') -\>setParameter(1, $customerId) -\>getQuery(); $update = $queryupdate-\>execute(); // oder alternativ // $update = Shopware()-\>Db()-\>query( // "UPDATE s\_user SET email = ? WHERE id = ?" // , array($email, $customerId) // ); return true; } } [/code]
```

---

<div class="post-metadata">

**Author:** ![Basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Basti](https://forum.shopware.com/u/Basti)\
**Post date:** [15. Februar 2013 um 16:48 UTC](https://forum.shopware.com/t/eigener-controller-richtige-verwendung-der-shopware-models/11419/2 "2013-02-15T16:48:21Z")

</div>

Wäre super, wenn mir jemand ein kurzes Beispiel geben könnte, wie ich die get und set Funktionen eines Models verwende.

---

<div class="post-metadata">

**Author:** ![thesaint](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@thesaint](https://forum.shopware.com/u/thesaint)\
**Post date:** [20. Februar 2013 um 11:21 UTC](https://forum.shopware.com/t/eigener-controller-richtige-verwendung-der-shopware-models/11419/3 "2013-02-20T11:21:35Z")

</div>

Versuche zum Setzen der E-mail folgendes: `$customer = Shopware()-\>Models()-\>find("Shopware\\Models\\Customer\\Customer", $customerId); // TODO: Error-Handling, falls Kunde nicht gefunden wurde $customer-\>setEmail($email); Shopware()-\>Models()-\>persist($customer); Shopware()-\>Models()-\>flush();`

---

<div class="post-metadata">

**Author:** ![Basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@Basti](https://forum.shopware.com/u/Basti)\
**Post date:** [20. Februar 2013 um 20:32 UTC](https://forum.shopware.com/t/eigener-controller-richtige-verwendung-der-shopware-models/11419/4 "2013-02-20T20:32:13Z")

</div>

Danke! So kann ich mein restliches Plugin ja innerhalb von weniger als 10 Minuten fertig stellen 😉
