Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created November 13, 2010 17:36
Show Gist options
  • Save walterdavis/675505 to your computer and use it in GitHub Desktop.
Save walterdavis/675505 to your computer and use it in GitHub Desktop.
<?php
define('MYACTIVERECORD_CONNECTION_STR', 'mysql://username:password@localhost/databasename');
require_once('MyActiveRecord.php');
class User extends MyActiveRecord{
//this model will "wrap" a database table named exactly 'user'
//and having an auto-incrementing primary key named 'id'
//extend base model methods here
function save(){
//mandatory checkbox
$this->validate_existence('foo','Foo must be checked');
return parent::save();
}
}
$id = (isset($_GET['id'])) ? (int) $_GET['id'] : 0;
if($user = MyActiveRecord::FindById('User',$id)){
//welcome back
}else{
$user = MyActiveRecord::Create('User');
}
//handle updates
if(isset($_POST['save'])){
$user->populate($_POST);
$user->save();
if(false == $user->get_errors()){
header('Location: your_form.php?id=' . $user->id); //clear the POST and show the form again
exit;
}else{
die(print_r($user->get_errors(),true); //ugly error message for testing
}
}
//set global variables
extract(get_object_vars($user));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment