My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 05, 2007 by VaultedCeilings
Labels: Phase-Deploy, Phase-Support
PerformCrud  
How to use Junction to perform CRUD tasks

Junction Quick-start

Performing CRUD operations

Alright, now that we have Junction installed, configured, and a data object created we can start to performing operations which affect the database. In order to do so we must include:

So, keeping in mind our directory structure we'll include our dependencies as follows:

require("/Libs/Junction/Junction.php");
require("/Domain/JunctionUser.php");

Following this we need to get an instance of the JunctionSession. To do this we issue the following command:

Retrieve a Junction session

$junction = Junction::construct("JunctionUser");

It's worth pointing out that the string JunctionUser is important, as it points to the entry in the mapping file we added earlier. This command tells Junction to create a new session for dealing with instances of JunctionUser. Yes, that means you'll need to create a new session for each data object you wish to persist.

Now that the session has been instantiated we can perform some typical operations. Here's an example of creating a new user and saving it to the database.

Creating a new JunctionUser

$user = new JunctionUser();
$user->setEmail("foo@bar.com");
$user->setPassword("password");
$user->setDate(time());

$junction->save($user); // the user has been committed to the database, and has been assigned an id

Retrieving existing JunctionUser objects

$users = $junction->loadWhere();
foreach ($users as $user) {
	addRow($user);
}

$clause = $junction->createQuery("id = ?");
$clause->bind(0, 1);
$users = $junction->loadWhere($clause);
$user = $users->current();
echo $user->getEmail() . " was registered on " . date("c", $user->getDate());

Deleting existing objects

$clause = $junction->createQuery("date_added < ?");
$clause->bind(0, time());
$deleted = $junction->deleteWhere($clause);
echo $deleted . " user's were deleted";

Putting it all together

$user = new JunctionUser();
$user->setEmail("foo@bar.com");
$user->setPassword("password");
$user->setDate(time());

$junction->save($user);

$clause = $junction->createQuery("password = ?");
$clause->bind(0, "password");
$users = $junction->loadWhere($clause);
$updated = 0;
foreach ($users as $user) {
	$user->setPassword("newpassword");
	$junction->save($user);
	$updated++;
}

$clause = $junction->createQuery("password != ?");
$clause->bind(0, "newpassword");
$deleted = $junction->deleteWhere($clause);
echo "Deleted " . $deleted . " users and updated " . $updated . " users.";


Comment by fonant, Oct 05, 2007

$user++ should read $updated++ in "Putting it all together"?

Comment by VaultedCeilings, Oct 05, 2007

Thanks fonant, you're right.

Comment by kardos.martin, Nov 03, 2007

I have a question. And how to when I want to join two or more tables together? For example when I need make SELECT with 2 tables and join the tables with foreign key? Can I use Junction on this? thanks

Comment by VaultedCeilings, Nov 04, 2007

Kardos, at the moment that isn't supported. We hope to implement that functionality by the end of this year.

Comment by makc.the.great, Dec 12, 2007

what do you put in /Domain/JunctionUser?.php file?

Comment by AdvilsDevocate, Jan 08, 2008

makc.the.great, the JunctionUser.php file should contain your class definition. See the WriteDataObjects step for the example that goes along with this tutorial.

Comment by einarcesar, Feb 08, 2008

Great API! I try to put set and get "magic" functions in JunctionUser? class and it works! I could get my variables from $user->id sintax. I will put Junction in some projects that i have work... Greetings!


Sign in to add a comment
Hosted by Google Code