ExtJs Record wird nur mit NULL als Wert gespeichert!

Hallo Liebe Community, ich habe wiedermal ein Problem. Ich habe ein Model und einen Store erstellt, und erstelle diese beiden in meiner Funktion zum Speichern der Form Werte. Da lediglich getestet werden soll, ob die Werte überhaupt gespeichert werden reicht dies fürs erste denk ich. Nachdem ich auf den Speichern BUtton geklickt habe, erscheinen in dem Request Post die richtigen Werte wie sie von mir eingetragen wurden. In der Antwort sind diese Felder jedoch wieder leer, und genau so werden sie auch in der Datenbank gespeichert. Weiß jmd Rat oder Hilfe? Am besten jmd von Shopware, da ich denke, es handelt sich um ein sehr spezielles Problem. Anbei füge ich die Funktion zum speichern der Daten an. Hoffe ihr könnt damit was anfangen :slight_smile: onSave: function(record) { var me = this, message = ''; var storeObject=this.getStore('Shopware.apps.IwMyProject.store.General'); //var storeObject = Ext.create('Shopware.apps.IwMyProject.store.General'); var check = Ext.create('Shopware.apps.IwMyProject.model.General'); check.set('d', true); check.set('f',true); check.set('en',false); check.set('ea','info@ideenwribler.de'); check.set('bp','mm\_\_'); storeObject.add(check); var record = storeObject.getAt(0); console.info(record); if (!(record instanceof Ext.data.Model)) { return; } me.mainWindow.setLoading(true); record.save({ success: function() { me.mainWindow.setLoading(false); message = '{s name=messages/save\_success}Eintrag erfolgreich gespeichert{/s}'; message = Ext.String.format(message, record.get('email\_address')); Shopware.Notification.createGrowlMessage('', message); }, failure: function(result, operation) { me.mainWindow.setLoading(false); var rawData = result.getProxy().getReader().rawData; var message = '{s name=messages/save\_failure}{/s}Speichern fehlgeschlagen: ' + rawData.error; message = Ext.String.format(message, record.get('id')); console.info(message); Shopware.Notification.createGrowlMessage('', message); } }); },

1 „Gefällt mir“

Hi, der Source Code sieht erstmal richtig aus. Interessant wären jetzt folgende Source Klasse: 1. PHP Controller. 2. ExtJS Model Die Zeile “record = store.getAt(0)” kannst du dir sparen. Durch store.add(check) wird der record als Referenz übergeben. Gruß Oliver

1 „Gefällt mir“

Erstmal vielen Dank für die Antworten! Anbei einmal der PHP Controller: [code]<?php class Shopware_Controllers_Backend_IwBackupShopsystem extends Shopware_Controllers_Backend_ExtJs
{

protected $repository = null;

/**
 * @return \Shopware\CustomModels\IwBackupShopsystem\Repository
 */
public function getRepository() {
    if ($this->repository === null) { $this-\>repository = Shopware()-\>Models()-\>getRepository( 'Shopware\CustomModels\IwBackupShopsystem\Backup' ); } return $this-\>repository; } /\*\* \* Controller action which can be called over an ajax request. \* This function can be used to get the general configuration. \*/ public function getGeneralConfigAction() { $this-\>View()-\>assign( $this-\>getGeneralConfig( $this-\>Request()-\>getParam('filter'), $this-\>Request()-\>getParam('sort'), $this-\>Request()-\>getParam('offset'), $this-\>Request()-\>getParam('limit') ) ); } /\*\* \* Internal helper function which selects backup configuration \* @param $filter \* @param $sort \* @param $offset \* @param $limit \* \* @return array \*/ protected function getGeneralConfig($filter, $sort, $offset, $limit) { $builder = $this-\>getRepository()-\>getListQueryBuilder( $filter, $sort, $offset, $limit ); $query = $builder-\>getQuery(); $query-\>setHydrationMode( \Doctrine\ORM\AbstractQuery::HYDRATE\_ARRAY ); $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query); return array( 'success' =\> true, 'total' =\> $paginator-\>count(), 'data' =\> $paginator-\>getIterator()-\>getArrayCopy() ); } /\*\* \* Controller action which can be called over an ajax request. \* This function can be used to create a new general config. \*/ public function createGeneralConfigAction() { $this-\>View()-\>assign( $this-\>saveGeneralBackup($this-\>Request()-\>getParams()) ); } /\*\* \* Internal helper function which can be used to save a new or existing general config. \* If the data parameter contains the id field, the function updates an existing general config, \* which identified over the id. \* @param $data \* \* @return array \*/ protected function saveGeneralBackup($data) { try { $favorite = new \Shopware\CustomModels\IwBackupShopsystemiBackuporite(); if (!empty($data['id'])) { $favorite = Shopware()-\>Models()-\>find( '\Shopware\CustomModels\IwBackupShopsystem\Backup', (int) $data['id'] ); } if (!($favorite instanceof \Shopware\CustomModels\IwBackupShopsystem\Backup)) { return array( 'success' =\> false, 'error' =\> "The passed id '" . $data['id'] . " don't exist" ); } #$data = $this-\>prepareAssociatedData($data); $backup-\>fromArray($data); Shopware()-\>Models()-\>persist($backup); Shopware()-\>Models()-\>flush(); $data = $this-\>getGeneralBackup( $backup-\>getId(), \Doctrine\ORM\AbstractQuery::HYDRATE\_ARRAY ); return array( 'success' =\> true, 'data' =\> $data ); } catch (Exception $e) { $error = $e-\>getMessage(); return array( 'success' =\> false, 'data' =\> $data, 'error' =\> $error ); } } /\*\* \* Helper function to get a single general configbacku object. \* \* @param $generalBackupId \* @param int $hydrationMode \* \* @return mixed \*/ protected function getGeneralBackup($generalBackupId, $hydrationMode = \Doctrine\ORM\AbstractQuery::HYDRATE\_OBJECT) { $builder = $this-\>getRepository()-\>getOneQueryBuilder($generalBackupId); return $builder-\>getQuery()-\>getOneOrNullResult($hydrationMode); } } [/code] und schließlich das ExtJS Model: [code]<?php namespace Shopware\CustomModels\IwBackupShopsystem;

use Shopware\Components\Model\ModelEntity,
Doctrine\ORM\Mapping AS ORM,
Symfony\Component\Validator\Constraints as Assert,
Doctrine\Common\Collections\ArrayCollection;

/**

  • Shopware\CustomModels\IwBackupShopsystem\Backup

  • @ORM\Table(name=“iw_backup_shopsystem”)

  • @ORM\Entity(repositoryClass=“Repository”)
    /
    class Backup extends ModelEntity
    {
    /
    *

    • @var integer $id
    • @ORM\Column(name=“id”, type=“integer”, precision=0, scale=0, nullable=false, unique=false)
    • @ORM\Id
    • @ORM\GeneratedValue(strategy=“IDENTITY”)
      */
      private $id;

    /**

    • @var string $database_backup
    • @ORM\Column(name=“database_backup”, type=“boolean”, unique=false)
      */
      private $database_backup;

    /**

    • @var string $fileserver_backup
    • @ORM\Column(name=“fileserver_backup”, type=“boolean”, unique=false)
      */
      private $fileserver_backup;

    /**

    • @var string $email_notification
    • @ORM\Column(name=“email_notification”, type=“boolean”, unique=false)
      */
      private $email_notification;

    /**

    • @var string $email_address
    • @ORM\Column(name=“email_address”, type=“string”, length=100, precision=0, scale=0, nullable=false, unique=false)
      */
      private $email_address;

    /**

    • @var string $backup_prefix
    • @ORM\Column(name=“backup_prefix”, type=“string”, length=100, precision=0, scale=0, nullable=false, unique=false)
      */
      private $backup_prefix;

    /**

    • @var \Doctrine\Common\Collections\ArrayCollection
    • @ORM\ManyToMany(targetEntity=“Shopware\Models\Article\Article”)
    • @ORM\JoinTable(name=“swag_favorites_articles”,
    • joinColumns={
    • @ORM\JoinColumn(name=“favorite_id”, referencedColumnName=“id”, onDelete=“NO ACTION”)
    • },
    • inverseJoinColumns={
    • @ORM\JoinColumn(name=“article_id”, referencedColumnName=“id”, onDelete=“NO ACTION”)
    • }
    • )
      */
      private $articles;

    public function __construct()
    {
    $this->articles = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set database_backup * * @param boolean $database_backup * @return Backup */ public function setDatabaseBackup($database_backup) { $this->database_backup = $database_backup; return $this; } /** * Get database_backup * * @return boolean */ public function getDatabaseBackup() { return $this->database_backup; } /** * Set fileserver_backup * * @param boolean $fileserver_backup * @return Backup */ public function setFileserverBackup($fileserver_backup) { $this->fileserver_backup = $fileserver_backup; return $this; } /** * Get fileserver_backup * * @return boolean */ public function getFileserverBackup() { return $this->fileserver_backup; } /** * Set email_notification * * @param boolean $email_notification * @return Backup */ public function setEmailNotification($email_notification) { $this->email_notification = $email_notification; return $this; } /** * Get email_notification * * @return boolean */ public function getEmailNotification() { return $this->email_notification; } /** * Set email_address * * @param string $email_address * @return Backup */ public function setEmailAddress($email_address) { $this->email_address = $email_address; return $this; } /** * Get email_address * * @return string */ public function getEmailAddress() { return $this->email_address; } /** * Set backup_prefix * * @param string $backup_prefix * @return Backup */ public function setBackupPrefix($backup_prefix) { $this->backup_prefix = $backup_prefix; return $this; } /** * Get backup_prefix * * @return string */ public function getBackupPrefix() { return $this->backup_prefix; } /** * Add articles * * @param \Shopware\Models\Article\Article $articles */ public function addArticle(\Shopware\Models\Article\Article $articles) { $this->articles = $articles; } /** * Set articles * * @param $articles * * @return \Doctrine\Common\Collections\Collection */ public function setArticles($articles) { $this->articles = $articles; return $this; } /** * Get articles * * @return \Doctrine\Common\Collections\Collection */ public function getArticles() { return $this->articles; } } [/code]

Hi, hier ist der Fehler versteckt: $favorite = new \Shopware\CustomModels\IwBackupShopsystemiBackuporite(); if (!empty($data['id'])) { $favorite = Shopware()-\>Models()-\>find( '\Shopware\CustomModels\IwBackupShopsystem\Backup', (int) $data['id'] ); } if (!($favorite instanceof \Shopware\CustomModels\IwBackupShopsystem\Backup)) { return array( 'success' =\> false, 'error' =\> "The passed id '" . $data['id'] . " don't exist" ); } #$data = $this-\>prepareAssociatedData($data); $backup-\>fromArray($data); Du machst am Anfang einen “find” oder ein “new …” was auch richtig ist. Dies weist du jedoch der Variabel $favorite zu. Später verwendest du aber $backup->fromArray($data); $backup ist jedoch nicht definiert. Entwerder du ersetzt “$backup” durch “$favorite” oder anders herum :wink: Gruß Oliver

2 „Gefällt mir“

Moin, ich bins nochmal. Hat leider etwas gedauert, bis ich mich wieder mit dem Plugin auseinander setzen konnte. Also ich habe jetzt die vorgeschlagenen Änderungen umgesetzt, doch leider funktioniert es noch nicht so wirklich. Wenn ich in meine Oberfläche (Bild1) die Checkboxen setzen und auch in den Textfeldern etwas eintrage, wird dies nicht in die Datenbank gespeichert. Dazu habe ich mal den Post des Request (Bild 2) und den Response (Bild 3) angehängt. Die Post Werte werden zum testen hart in den Code programmiert, doch nichtmal diese werden gespeichert. Deshalb füge ich nochmal das Model, den PHP- und den Main-Controller hinzug. Model -> general.js /\*\* \* Plugin: IdwBackupSystem \* Date: 05.10.13 \* Time: 13:44 \* @author Dennis Hinnenkamp \* @category: Plugin \* @package: IdwBackupSystem \*/ //{block name="backend/idw\_backup\_system/model/general"} Ext.define('Shopware.apps.IdwBackupSystem.model.General', { /\*\* \* Extends the standard Ext Model \* @string \*/ extend: 'Ext.data.Model', /\*\* \* The fields used for this model \* @array \*/ fields: [{ name: 'id', type: 'int', useNull: true }, { name: 'database\_backup', type: 'boolean' }, { name: 'fileserver\_backup', type: 'boolean' }, { name: 'email\_notification', type: 'boolean' }, { name: 'email\_address', type: 'string' }, { name: 'backup\_prefix', type: 'string' }], associations: [{ type: 'hasMany', model: 'Shopware.apps.Base.model.Article', name: 'getArticles', associationKey: 'articles' }], /\*\* \* Configure the data communication \* @object \*/ proxy:{ /\*\* \* Set proxy type to ajax \* @string \*/ type:'ajax', /\*\* \* Configure the url mapping for the different \* store operations based on \* @object \*/ api: { create: '{url controller="IdwBackupSystem" action="createGeneralConfig"}', update: '{url controller="IdwBackupSystem" action="updateGeneral"}', destroy: '{url controller="IdwBackupSystem" action="deleteGeneral"}' }, /\*\* \* Configure the data reader \* @object \*/ reader:{ type:'json', root:'data', totalProperty:'total' } } }); //{/block} PHP-Controller [code]<?php /**

  • Plugin: IdwBackupSystem

  • Date: 05.10.13

  • Time: 12:51

  • @author Dennis Hinnenkamp

  • @version 1.0.0

  • @copyright Ideenwirbler | Dennis
    */
    class Shopware_Controllers_Backend_IdwBackupSystem extends Shopware_Controllers_Backend_ExtJs
    {
    protected $repository = null;

    /**

    • @return \Shopware\CustomModels\IdwBackupSystem\Repository
      */
      public function getRepository() {
      if ($this->repository === null) { $this->repository = Shopware()->Models()->getRepository( ‚Shopware\CustomModels\IdwBackupSystem\Backup‘ ); } return $this->repository; } /** * Controller action which can be called over an ajax request. * This function can be used to get the general configuration. */ public function getGeneralConfigAction() { $this->View()->assign( $this->getGeneralConfig( $this->Request()->getParam(‚filter‘), $this->Request()->getParam(‚sort‘), $this->Request()->getParam(‚offset‘), $this->Request()->getParam(‚limit‘) ) ); } /** * Internal helper function which selects backup configuration * @param $filter * @param $sort * @param $offset * @param $limit * * @return array */ protected function getGeneralConfig($filter, $sort, $offset, $limit) { $builder = $this->getRepository()->getConfigQueryBuilder( $filter, $sort, $offset, $limit ); $query = $builder->getQuery(); $query->setHydrationMode( \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY ); $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query); return array( ‚success‘ => true, ‚total‘ => $paginator->count(), ‚data‘ => $paginator->getIterator()->getArrayCopy() ); } /** * Controller action which can be called over an ajax request. * This function can be used to create a new general config. */ public function createGeneralConfigAction() { $this->View()->assign( $this->saveGeneralBackup($this->Request()->getParams()) ); } /** * Controller action which can be called over an ajax request. * This function can be used to create a new general config. */ public function updateGeneralConfigAction() { $this->View()->assign( $this->saveGeneralBackup($this->Request()->getParams()) ); } /** * Internal helper function which can be used to save a new or existing general config. * If the data parameter contains the id field, the function updates an existing general config, * which identified over the id. * @param $data * * @return array */ protected function saveGeneralBackup($data) { try { $backup = new \Shopware\CustomModels\IdwBackupSystem\Backup(); if (!empty($data[‚id‘])) { $backup = Shopware()->Models()->find( ‚\Shopware\CustomModels\IdwBackupSystem\Backup‘, (int) $data[‚id‘] ); } if (!($backup instanceof \Shopware\CustomModels\IdwBackupSystem\Backup)) { return array( ‚success‘ => false, ‚error‘ => „The passed id '“ . $data[‚id‘] . " don’t exist" ); } #$data = $this->prepareAssociatedData($data); $backup->fromArray($data); Shopware()->Models()->persist($backup); Shopware()->Models()->flush(); $data = $this->getGeneralBackup( $backup->getId(), \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY ); return array( ‚success‘ => true, ‚data‘ => $data ); } catch (Exception $e) { $error = $e->getMessage(); return array( ‚success‘ => false, ‚data‘ => $data, ‚error‘ => $error ); } } /** * Helper function to get a single general configbacku object. * * @param $generalBackupId * @param int $hydrationMode * * @return mixed */ protected function getGeneralBackup($generalBackupId, $hydrationMode = \Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT) { $builder = $this->getRepository()->getOneQueryBuilder($generalBackupId); return $builder->getQuery()->getOneOrNullResult($hydrationMode); } }[/code] Main-Controller → main.js [code]onSaveGeneralBackup: function(record) { var me = this, message = ‚‘; //var storeObject=this.getStore(‚Shopware.apps.IdwBackupSystem.store.General‘); var storeObject = Ext.create(‚Shopware.apps.IdwBackupSystem.store.General‘); var check = Ext.create(‚Shopware.apps.IdwBackupSystem.model.General‘); check.set(‚database_backup‘, true); check.set(‚fileserver_backup‘,true); check.set(‚email_notification‘,false); check.set(‚email_address‘,‚info@ideenwirbler.de‘); check.set(‚backup_prefix‘,‚mm__‘); //check.set(‚id‘, 2); storeObject.add(check); var record = storeObject.getAt(0); if (!(record instanceof Ext.data.Model)) { //return; } me.mainWindow.setLoading(true); Shopware.Notification.createGrowlMessage(’’, ‚Record is undefined‘); record.save({ success: function() { console.log(‚onSaveGeneralBackup - success‘); me.mainWindow.setLoading(false); message = ‚{s name=messages/save_success}Backup: [0]
      Eintrag erfolgreich gespeichert{/s}‘; message = Ext.String.format(message, record.get(‚email_address‘)); Shopware.Notification.createGrowlMessage(’’, message); }, failure: function(result, operation) { console.log(‚onSaveGeneralBackup - failure‘); me.mainWindow.setLoading(false); var rawData = result.getProxy().getReader().rawData; var message = '{s name=messages/save_failure}Backup: [0]{/s}

Speichern fehlgeschlagen: ’ + rawData.error; message = Ext.String.format(message, record.get(‚id‘)); Shopware.Notification.createGrowlMessage(’’, message); } }); }, /**[/code] Vielen Dank schonmal im Voraus!

Hi, ich vermute dass dein Problem in der fromArray Methode besteht. Die fromArray Methode prüft ob in dem Model eine Setter Funktion für jedes der übergebenen Array Elemente existiert. Dies prüft Sie, indem einfach ein “ucFirst” auf die Variable gemacht wird und ein “set” davor gesetzt wird. In deinem Beispiel sieht dies wie folgt aus: Array Element: “backup_prefix” geprüft Funktion: “setBackup_prefix” Diese Funkion existiert jedoch in deinem Model nicht. Wenn du dies umgestellt hast sollte es funktionieren. Weitere Fehler sind mir nicht aufgefallen.

1 „Gefällt mir“

Hey, Vielen Dank, es lag wirklich an den falschen Methoden innerhalb des Models. Aber nun habe ich eine andere Frage: Wie kann ich die Daten, welche ich eben gespeichert habe mittels PHP-Controller beim starten des Fensters laden und die Felder mit den Werten vorbelegen? So dass ich später einfach nur noch ein Change Handler auf die Elemente setzen und die Datenbank aktualisieren muss? Ups…mit meinem anderen Account eingeloggt :slight_smile:

push

-> push!