-
Notifications
You must be signed in to change notification settings - Fork 7
Saving Data
Richard edited this page Jun 16, 2016
·
1 revision
To save an object back into the data mapper, simply create an instance of stdClass:
$pdo = new PDO('mysql:dbname=maphpertest;host=127.0.0.1', 'username', 'password');
$blogSource = new \Maphper\DataSource\Database($pdo, 'blog', 'id');
$blogs = new \Maphper\Maphper($blogSource);
$blog = new stdClass;
$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 = new stdClass;
$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 = new stdClass;
$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;