tuupola / php_record
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
| name | age | message | |
|---|---|---|---|
| |
README.textile | ||
| |
Record.php | ||
| |
Record/ | ||
| |
tests/ |
README.textile
Record – Simple Active Record implementation in PHP
This code is combination of Model class taken from Green Framework and DB_DataContainer and MDB2_DataContainer2 classes.
Usage
Simple example.
try {
$dbh = new PDO('sqlite:/tmp/record.sqlite');
} catch(PDOException $e) {
print $e->getMessage();
}
class Person extends Record {
protected $first_name;
protected $last_name;
protected $age;
public function beforeSave() {
if (120 > $this->age()) {
return false;
} else {
return true;
}
};
}
class Computer extends Record {
protected $mark;
protected $person_id;
protected static $belongs_to = array('person');
}
Person::connection($dbh);
$p = new Person();
$p->firstName('Mika');
$p->lastName('Tuupola');
$p->age(100);
$p->save();
$c = new Computer();
$c->mark('Apple');
$p->computer($c);
printf('Guy called %s %s is %d years old.',
$p->firstName(),
$p->lastName(),
$p->age());
$all_mika = Person::findByFirstName('Mika');
$one_mika = Person::findByFirstName(':one', 'Mika');
$apple = $one_mika->computer();

