You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Richard edited this page Jun 16, 2016
·
1 revision
Saving Data
To save an object back into the data mapper, simply create an instance of stdClass:
$pdo = newPDO('mysql:dbname=maphpertest;host=127.0.0.1', 'username', 'password');
$blogSource = new \Maphper\DataSource\Database($pdo, 'blog', 'id');
$blogs = new \Maphper\Maphper($blogSource);
$blog = newstdClass;
$blog->title = 'My Blog Title';
$blog->content = 'This is my first blog entry';
//Store the blog using the next available ID$blogs[] = $blog;
echo'The new blog ID is :' . $blog->id;
Alternatively, you can write a record to a specific ID by specifying the index:
$blog = newstdClass;
$blog->title = 'My Blog Title';
$blog->content = 'This is my first blog entry';
//Store the blog with the primary key of 7$blogs[7] = $blog;
Note: The behaviour of this is identical to setting the id property on the $blog object directly:
$blog = newstdClass;
$blog->id = 7;
$blog->title = 'My Blog Title';
$blog->content = 'This is my first blog entry';
//Store the blog with the primary key of 7$blogs[] = $blog;