Skip to content

Commit

Permalink
Test and readme update to show how to use ArrayObject to persist
Browse files Browse the repository at this point in the history
  • Loading branch information
williancarminato committed Sep 30, 2013
1 parent 7cad826 commit 05281bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -86,6 +86,8 @@ dive on automatic `JOIN` mapping, ordering and limiting below, keep reading!

### Persisting

#### Persist with stdClass

You can insert a new author into the database using the following example. We're using
`stdClass`, but you will see later on in this guide just how easy it is to use specific classes
for each mapping.
Expand All @@ -106,6 +108,21 @@ keep the state in memory, flushing sends it all to the database.
After a `flush()` if you `print $alexandre->id`, it will reflect the auto incremented
value from the database.

#### Persist with ArrayObject

You can create a new author with `ArrayObject` too. Let's supose that you get a post
request from a form with the field _name_ to create an author. You can do something like this:

```php
<?php $alexandre = new \ArrayObject($_POST, \ArrayObject::STD_PROP_LIST);
$alexandre->created_at = date('Y-m-d H:i:s');

$mapper->author->persist($alexandre);
$mapper->flush();
```

This is just to show what you can do, ofcourse you have to validate the `$_POST` var first.

### Joining

In the sample below we're going to get all the comments, from all the posts created
Expand Down
11 changes: 11 additions & 0 deletions tests/library/Respect/Relational/MapperTest.php
Expand Up @@ -806,6 +806,17 @@ public function test_typed_single() {
$this->assertEquals('Title Changed', $result->title);
}

public function test_persist_new_with_arrayobject()
{
$mapper = $this->mapper;
$arrayEntity = array('id' => 10, 'name' => 'array_object_category', 'category_id' => null);
$entity = new \ArrayObject($arrayEntity);
$mapper->category->persist($entity);
$mapper->flush();
$result = $this->conn->query('select * from category where id=10')->fetch(PDO::FETCH_OBJ);
$this->assertEquals('array_object_category', $result->name);
}

}

class Postcomment {
Expand Down

0 comments on commit 05281bd

Please sign in to comment.