This chapter looks at the details of adding, updating, and deleting objects from the datastore.
Adding objects to your datastore is in effect inserting rows into your table. This is quite easily done in Propel by simply creating a new instance of the appropriate class and invoking the save() method.
// Create new Author $a = new Author(); $a->setFirstName("Leo"); $a->setLastName("Tolstoy"); $a->save(); // now pkey is set $author_id = $a->getAuthorId();
In the example above a single row is being inserted in the database. In many cases, however, you will be working with inter-related records, and a single save() call may result in writing more than one record to the database.
// Create Author $author = new Author(); $author->setFirstName("John"); $author->setLastName("Smith"); // Create Publisher $publisher = new Publisher(); $publisher->setName("Penguin"); // Create J.Smith's book $book = new Book(); $book->setISBN("111-2222-3333"); $book->setTitle("Fake Title"); $book->setPublisher($publisher); $book->setAuthor($author); // Saving the book will result in save of // author and publisher records $book->save(); // will add 3 records to the db
That's the easy way to add objects and have them automatically when the related item is saved. You can also manually specify the foreign keys, which means that you'd need to add a few steps to the code above:
// Create Author $author = new Author(); $author->setFirstName("John"); $author->setLastName("Smith"); $author->save(); . . $book = new Book(); $book->setAuthorId($author->getAuthorId()); . .
A simple update is accomplished when an object is modified (using set*() methods) and the save() method is invoked. In your code this looks the same as the code to insert a new record; the only difference is that the object being modified will have a primary key already set.
$obj = AuthorPeer::retrieveByPK(1); // get Author where pkey is 1 $obj->setFirstName($obj->getFirstName() . "-modiified"); $obj->save(); // performs UPDATE // UPDATE author // SET author.FIRST_NAME = '".$obj->getFirstName()."-modified' // WHERE author.AUTHOR_ID = 1
To delete records you can either call the object's delete() method or use the Peer's doDelete() method.
The object delete() method deletes the object from the database (by primary key). It also sets the object state to "delete". You can still retrieve properties of deleted objects, but you cannot save deleted objects.
$author = AuthorPeer::retrieveByPk(1); $author->delete(); // DELETE FROM author WHERE author.AUTHOR_ID = 1
When using the Peer doDelete() method, you can specify whatever criteria you wish when performing the delete; in this example we are deleting a single record from the author table.
$crit = new Criteria(); $crit->add(AuthorPeer::AUTHOR_ID, 1); AuthorPeer::doDelete($crit); // DELETE FROM author WHERE author.AUTHOR_ID = 1
Propel also supports cascading deletes (including emulation for databases that don't support them natively), which are described in more detail in the Relationships chapter [Cascading Delete].