-
Notifications
You must be signed in to change notification settings - Fork 278
Aliases as foreing keys with sample code
marios88 edited this page Jun 28, 2018
·
2 revisions
Assume we have a table "users" and table "page" and want to save who created it and who made the last edit
if(!R::testConnection()){
$aliases = array();
$aliases[] = array('editedfrom'=> 'users');
$aliases[] = array('createdfrom'=> 'users');
R::aliases($aliases);
R::setup("mysql:host=".$hostname.";dbname=".$database,$username,$password);
if(ENVIRONMENT === 'development'){
R::debug( TRUE, 3 );
}
}
Aside from the usual init, we also called R::aliases
that practically "translates" the relations
$page = R::dispense('page');
....
$page->createdtime = date('Y-m-d H:i:s')
$page->createdfrom = R::load('users',$users_id);
$id = R::store($page);
$page = R::load('page',$page_id);
...
$page->editedtime = date('Y-m-d H:i:s');
$page->editedfrom = R::load('users',$users_id);
$id = R::store($page);