Save values to a table from backend

Hi everyone,

This is my first post here. I started learning oxid but now i am stuck. I am trying to save the values to my new table but it is not saving. Below is my controller and module file.

[B][U]controller:[/U][/B]

class homePage_controller extends oxAdminView
{
    protected $_sThisTemplate = 'dwstage.tpl';

    // Render.
    public function render()
    {
        // display php errors.
        ini_set('display_errors', true);

        parent::render();

        return $this->_sThisTemplate;
    }

    // Save.
    public function save()
    {
        // Fetch Values.
        echo $this->_aViewData['headline'] = $this->getConfig()->getRequestParameter('headline');
        $this->_aViewData['subline'] = $this->getConfig()->getRequestParameter('subline');

        $homePage = oxNew('homePage_model');

        $homePage->save();

        echo '<pre>';
        print_r($homePage);
    }
}

[B][U]module:[/U][/B]

class homePage_model extends oxI18n
{
    public function save() {
        $this->dwstage__pagename->value = '123';
        parent::save();
    }
}

[B][U]Table[/U][/B]

table name: dwstage
table column: pagename

can anyone know what i am doing wrong?

Here is an example how a minimal model could look like: http://www.urbans-blog.de/2010/05/09/oxid4all-–-es-muss-ja-nicht-immer-ein-shop-sein-…-–-teil-3/

Thanks i am still unclear. is this below model is correct?

<?php
/**
 * Created by PhpStorm.
 * User: ikhan
 * Date: 28.12.2015
 * Time: 12:36
 */

class homePage_model extends oxI18n
{
    /**
     * Name of the table.
     * @var string
     */
    protected $_sCoreTbl = 'dwstage';

    /**
     * Name of current class.
     * @var string
     */
    protected $_sClassName = 'homePage_model';

    /**
     * Class constructor, initiates parent constructor (parent::oxBase()).
     */
    public function __construct()
    {
        parent::__construct();
        $this->init( 'dwstage' );
    }

    public function save() {
        $homePage = oxNew('homePage_model');

        // Fetch Values.
        echo $homePage->dwstage__pagename = '123';

        $homePage->save();
    }
}

The save method doesn’t make sense.

Can you please give an example of save function? I am new to oxid.

Just delete it. You can use this code outside, but not inside of the object.

What your save method does right now: You are inside an instance, then you create another instance, and call the save method there, which creates another instance, and calls the save method there, which creates another instance … and so on.

Also if you want to assign values, i would rather put them into an array and use the assign() method.

ok. i will use the assign. i deleted the save function from the model. now this is my whole controller code till now.

class homePage_controller extends oxAdminView
{
    protected $_sThisTemplate = 'dwstage.tpl';

    // Render.
    public function render()
    {
        parent::render();
        return $this->_sThisTemplate;
    }

    // Save.
    public function save()
    {
        // Set variables.
        $homePage_config = $this->getConfig();

        // Creating model's object.
        $homePage = oxNew( 'homePage_model' );

        // Fetch values.
        $headline = $homePage_config->getRequestParameter('headline');
        echo $homePage->dwstage__pagename = new oxField ( $headline );

        $homePage->save();
    }
}

When i click my save button(form), nothing saves into the database. doing echo does show the input value but it is not saving. am i missing somethings?

this is the result of

print_r($homePage);

:

homePage_model Object
(
    [_sCoreTbl:protected] => dwstage
    [_sClassName:protected] => homePage_model
    [_iLanguage:protected] => 0
    [_blEmployMultilanguage:protected] => 1
    [_sOXID:protected] => 
    [_iShopId:protected] => oxbaseshop
    [_blIsSimplyClonable:protected] => 
    [_sCoreTable:protected] => dwstage
    [_sViewTable:protected] => 
    [_aFieldNames:protected] => Array
        (
            [oxid] => 0
            [dwstage_id] => 0
            [pagename] => 0
            [data] => 0
        )

    [_sCacheKey:protected] => 
    [_blUseLazyLoading:protected] => 
    [_aSkipSaveFields:protected] => Array
        (
            [0] => oxtimestamp
        )

    [_blUseSkipSaveFields:protected] => 1
    [_sExistKey:protected] => oxid
    [_blIsDerived:protected] => 
    [_blIsSeoObject:protected] => 
    [_blUpdateSeo:protected] => 1
    [_blReadOnly:protected] => 
    [_blIsInList:protected] => 
    [_isLoaded:protected] => 
    [_aInnerLazyCache:protected] => 
    [dwstage__dwstage_id] => oxField Object
        (
            [rawValue] => 
            [fldtype] => int
            [fldmax_length] => 11
        )

    [dwstage__pagename] => oxField Object
        (
            [value] => Ok
        )

    [dwstage__data] => oxField Object
        (
            [rawValue] => 
            [fldtype] => longtext
            [fldmax_length] => 10
        )

    [dwstage__oxid] => 
)

Does your table have a column “oxid”?

no. only 3 columns in dwstage:

  1. dwstage_id
  2. pagename
  3. data

You need the oxid column as primary key if you want to use oxid’s framework.

Also you could have a look at OXID’s sourcecode.
There are hundreds of examples for functions to save something.
e.g. this one: https://github.com/OXID-eSales/oxideshop_ce/blob/b-dev-ce/source/application/controllers/admin/actions_main.php#L134-L163

ok then will it work or do i need anything else?

Perfect now it is saving. thankyou very much. did thi oxid as a primary key is mentioned in oxid tutorials?

Why don’t you try? :wink:

Edit: The oxid as primary is mentioned in the tutorial i gave you.

yes it worked thanks :slight_smile: