Save method used to Add or Update data in database table. We can use two method to save data.
Save by Variable:
Format App::InformationSet(NAME)->Save($data);
This data variable formed like $data['Information'] = array(Field1=Value1,Field2=Value2,Field3=Value3)
Note: For all InformationSet model name is always Information.
For example
// Array Format 1
$data['Information'] = Array
(
'adminref' => 1,
'entrydate' => '2012-12-12 22:23:41',
'lastmodified' => '2012-12-14 00:13:02',
'title' => 'How to install Application',
'category' => 2,
'description' => 'Application is one of the easiest solutions to incorporate new features.',
'status' => 'Public'
);
// Array Format 2
$data['Information']['adminref'] = 1;
$data['Information']['entrydate'] = '2012-12-12 22:23:41';
$data['Information']['lastmodified'] = '2012-12-14 00:13:02';
$data['Information']['title'] = 'How to install Application';
$data['Information']['category'] = 2;
$data['Information']['description'] = 'Application is one of the easiest solutions to incorporate new features.';
$data['Information']['status'] = 'Public';
$obj = App::InformationSet('blogpost')->Save($data);
Save by Magic Method:
This is another convenient process. In the method first we set all field value in Model object then call Save function.
Format :
App::InformationSet(NAME)
->set[fieldName1](Value1)
->set[fieldName2](Value1)
->Save();
Example 1:
$obj = App::InformationSet('blogpost')
->setAdminref(1)
->setEntrydate('2012-12-12 22:23:41')
->setLastmodified('2012-12-14 00:13:02')
->setTitle('How to install Application')
->setCategory(2)
->setDescription('Application is one of the easiest solutions to incorporate new features.')
->setStatus('Public')
->Save();
Add/Update Row:
By default Save function insert a new row in table but it update existing row if we set id in save function.
Note: After inserting a row the new id stored in Model Object and update previous data on next attempt so, for repeated insert in same session we set NULL in id field.
// Create New entry update on next attempted
$obj = App::InformationSet('blogpost')
->setAdminref(1)
->setEntrydate('2012-12-12 22:23:41')
->setLastmodified('2012-12-14 00:13:02')
->setTitle('My New Entry')
->setCategory(2)
->setDescription('Hello World')
->setStatus('Public')
->Save();
// Update entry
$obj = App::InformationSet('blogpost')
->setId(5)
->setAdminref(1)
->setEntrydate('2012-12-12 22:23:41')
->setLastmodified('2012-12-14 00:13:02')
->setTitle('My New Entry')
->setCategory(2)
->setDescription('Hello World')
->setStatus('Public')
->Save();
// Always Create New entry
$obj = App::InformationSet('blogpost')
->setId(null)
->setAdminref(1)
->setEntrydate('2012-12-12 22:23:41')
->setLastmodified('2012-12-14 00:13:02')
->setTitle('My New Entry')
->setCategory(2)
->setDescription('Hello World')
->setStatus('Public')
->Save();
<form method="post" action="<?php echo App::Config()->baseUrl("/page/manage");?>" >
<input type="data[Information][name]" value="" />
<input type="data[Information][title]" value="" />
<input type="data[Information][description]" value="" />
</form>
PHP
if(!empty($this->data)){
$obj = $this->InformationSet('Post')->Save($this->data));
$error = $obj->getErrorInfo();
if (!empty($error)) {
App::Module('Notification')->Push($error,'error');
}
else {
App::Module('Notification')->Push("Saved Successfully");
}
}