views:

67

answers:

2

I want to save an object or form to the database. Only I can't find the easiest (or normal) way for how to do this.

I found a lot of tutorials, but none seem to be easy or current. Can someone please help me with this?

I use version 1.9.3 of the Zend Framework.

A: 

Instantiate any object that you need and serialize it. Once serialized, you can store it or transmit it to pretty much any medium. Is this what you are referring to?

Andrew Sledge
+4  A: 

The easiest way (aka the way using the smallest amount of code) to insert a row into a database table using Zend_Db is:

$data = array(
    'created_on'      => '2007-03-22',
    'bug_description' => 'Something wrong',
    'bug_status'      => 'NEW'
);

$db->insert('bugs', $data);

The above code will insert a new row into the bugs table whereas $db is the Zend_Db_Adapter_Abstract-subclass you created with Zend_Db::factory(). Please see Writing Changes to the Database in the Zend Framework manual for more details and the whole spectrum of features Zend_Db provides.

For the sake of completeness, the above code will issue a query to the database similar to:

INSERT INTO bugs (created_on, bug_description, bug_status) 
    VALUES ('2007-03-22', 'Something wrong', 'NEW')

The next step would be a more sophisticated approach using Zend_Db_Table.

EDIT:

Given that you have a Zend_Form ($form) with the appropriate fields created_on, bug_description and bug_status and provided that you have the right filters and validators in place, adding a new row with values given in the form is as easy as

if ($form->isValid($_POST)) {
    $db->insert('bugs', $form->getValues());
}

Storing a custom object is also very easy:

// $bug is your custom object representing a bug
$db->insert('bugs', array(
    'created_on'      => $bug->getCreatedOn(),
    'bug_description' => $bug->getDescription(),
    'bug_status'      => $bug->getStatus()
));
Stefan Gehrig
This clarification helps a lot. You are explaining it very well. Thanks for all of your time.
Rene Stuifzand