2. Data Manipulation

If you prefer the ORM way to save data Figure 4 provides several method examples on how to acheive this.

Figure 4. Data Manilpulation
$settings = array(
	'user_name'		=> 'Chris'
	'user_email'	=> 'myemail@mail.com');
$filter[] = array('user_id=%s', 1);		

$database->insertRow('user', $settings);			// inserts row into 'user' table
$database->updateRows('user', $settings, $filter); 	// updates rows in 'user' table where user_id is 1
$database->deleteRows('user', $filter);			// delete rows in 'user' table where user_id is 1

//updates data if there is a user_email with the value of anemail@gmail.com otherwise will insert
$database->setRow('user', 'user_email', 'anemail@gmail.com', $settings);			

Inserting data is pretty trivial. We included 2 ways to insert data. Like getRow(), there's no need to worry about binded data because Eden wil do this for you. Figure 4 shows the 2 kind of inserts mentioned.

Figure 4. Two ways to insert
$settings = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com');
$database->insertRow('user', $settings);			// inserts row into 'user' table

$settings = array();
$settings[] = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com');
$settings[] = array('user_name' => 'Dan', 'user_email' => 'myemail2@mail.com');
$settings[] = array('user_name' => 'Clark', 'user_email' => 'myemail3@mail.com');
$database->insertRows('user', $settings);			// inserts multiple rows into 'user' table

So obviously insertRow() should be used if you just want to insert one row. Inserting two or more rows at the same time, you should use insertRows(). This method expects an array of arrays, or an array table.

A common error found amongst programmers using Eden, is simply using insertRows() instead of insertRow().
Using models and collections, you don't really need to worry about this method because it's covered in the save() method in a collection or model object. We'll go over models and collections later in this section.

Updating is about as easy as inserting. There's only one method you need to know.

Figure 5. Updating
$settings = array('user_name' => 'Chris', 'user_email' => 'myemail@mail.com');
$filter[] = array('user_id=%s', 1);
$database->updateRows('user', $settings, $filter);			// inserts row into 'user' table

A common scenario is when you need to insert if a column value is not found and update if it is. We added an extra method called setRow() to simply to save you some lines of redundancy.

Figure 6. Insert or update
$settings = array('user_name' => 'Chris2', 'user_email' => 'myemail@mail.com');
$database->setRow('user', 'user_email', 'myemail@mail.com', $settings);

Figure 6 basically says, in user table, if 'myemail@mail.com' exists in the 'user_email' column, then update that row. If not then insert. Removing data is simple enough as well in Eden.

Figure 7. Remove
$filter[] = array('user_id=%s', 1);
$database->deleteRows('user', $filter); // delete rows in 'user' table where user_id is 1

© 2012 Openovate Labs. All rights reserved.