-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAO.php
More file actions
57 lines (54 loc) · 1.6 KB
/
DAO.php
File metadata and controls
57 lines (54 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Inspired by factory_girl
*/
class DAO {
/**
* Gets a built DO object , saves it and returns it
* @param string table name
* @param array (optional) array of key/values to override default faketory
* @return DB_DataObject_Pluggable
*/
public static function faketory($tablename,$overridefields = array())
{
$do = self::build($tablename,$overridefields);
$do->save();
return $do;
}
/**
* Returns a DO object.
* if the object containing the overridefields values is found in the database then it is returned.
* Otherwise a new fake object is created.
* unlike faketory, $overridefields is mandatory
* @param string table name
* @param string key/value pairs for search criteria
*/
public static function faketory_or_find($tablename,$overridefields)
{
$do = DB_DataObject::factory($tablename);
$do->setFrom($overridefields);
if($do->find(true)) return $do;
return self::faketory($tablename,$overridefields);
}
/**
* returns a built DO object, not saved (except linked records that need to)
* use values that are defined in project/faketories/...
* @param string table name
* @param array (optional) array of key/values to override default faketory
* @return DB_DataObject_Pluggable
*/
public static function build($tablename,$overridefields = array())
{
$do = DB_DataObject::factory($tablename);
extract($overridefields);
require APP_ROOT.'tests/Faketories/'.ucfirst($tablename).'.php';
foreach($overridefields as $k=>$v) {
if($v instanceOf DB_DataObject_Pluggable) {
$do->setLinkObj($v,$k);
} else {
$do->$k = $v;
}
}
return $do;
}
}