|
PerformCrud
How to use Junction to perform CRUD tasks
Junction Quick-startPerforming CRUD operationsAlright, 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:
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 idRetrieving 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.";
|
Sign in to add a comment
$user++ should read $updated++ in "Putting it all together"?
Thanks fonant, you're right.
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
Kardos, at the moment that isn't supported. We hope to implement that functionality by the end of this year.
what do you put in /Domain/JunctionUser?.php file?
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.
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!