0

I have this function in CakePHP's AppModel (This is used to install initial data in CakePHP) However, I can't seem to get it to save my data, and I get no errors.

Here is my function inside App/Model/AppModel.php:

public function importData() {
$initialOptionData = array(
array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
    );
$this->create();
$this->saveMany($initialOptionData);
}
0

2 Answers 2

2

From you posted code it seems you're trying to save you data to options table, and to do that you need to use Option model.

But you're code is within AppModel, so first import Option model and then execute your save statements.

Your code should look like:

public function importData() {

  $initialOptionData = array(
       array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
       array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
    );

   App::import('model','Option');  // Import the Option Model
   $Option = new Option();  // create instance of Option class

   // save statements

   $Option->create();
   $Option->saveMany($initialOptionData);

}

Note

Code you're trying will work if you write that within app/model/Option.php file.

Sign up to request clarification or add additional context in comments.

Comments

0

I suppose it would be enough to store the values as model property. No need to call $this->save...

public $initialOptionData = array(
  array( 'Option' => array( 'name' => 'version', 'value' => '1.0.0', )),
  array( 'Option' => array( 'name' => 'site-name', 'value' => 'Site Title', )),
);

all models will inherit initialOptionData...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.