-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBLayer.php
157 lines (145 loc) · 4.58 KB
/
DBLayer.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Description of DBLayer
*This class is responsible for making DB Connection , create ,update ,delete or insert operations.
* This is being used through out application.
* @author Dishna
*/
class DBLayer
{
//Create method to make database connection
private $i = 0;
private $colName;
private $conn;
private $Collect;
private $dbObj;
private $Id;
private $abc;
function __construct()
{
$username = 'kwilliams';
$password = 'mongo1234';
$conn= singleton::singleton($username, $password);
$this->dbObj = $conn->recipe;
}
Function setCollectionObj($colName)
{
$this->Collect=$this->dbObj->selectCollection("$colName");
}
//Retrieve Collection Method
public function get_CollectionObject($colName)
{
$this->Collect=$this->dbObj->selectCollection("$colName");
$cursor = $this->Collect->find();
return $cursor;
}
public function get_CollectionObjectbyId($colName,$Id)
{
$this->Collect=$this->dbObj->selectCollection("$colName");
$cursor = $this->Collect->find();
return $cursor;
}
//get object collection by ID
public function get_CollectionObjectbysearchParameter($colName,$SrchParm,$srchprmval)
{
$this->Collect=$this->dbObj->selectCollection("$colName");
$cursor = $this->Collect->find(array($SrchParm => $srchprmval));
return $cursor;
}
#End Region
//Save collection
public function InsertCollection($obj,$id)
{
//Insert obj values into Collection
if(!is_null($obj)|| !is_null($this->Collect))
$this->Collect->remove();
if (!is_null($id))
{
$obj['_id']=$id;
}
echo $obj["Thing"] ;
$Recipe=$obj["Recipe"];
$RecipeCollection=$this->dbObj->selectCollection("RecipeTest");
$RecipeCollection->Insert($Recipe);
//echo $Recipe['_id'].'\n';
$RecipeRef = MongoDBRef::create($RecipeCollection->getName(),$Recipe['_id']);
$CreativeWork=$obj["CreativeWork"];
$CreativeWork["RecipeReference"]=$RecipeRef;
$CreativeWorkCollection=$this->dbObj->selectCollection("CreativeWorkTest");
$CreativeWorkCollection->Insert($CreativeWork);
//echo $CreativeWork['_id'];
$CreativeWrokRef = MongoDBRef::create($CreativeWorkCollection->getName(), $CreativeWork['_id']);
$thing=$obj["Thing"];
$thing["CreativeWorkRef"]=$CreativeWrokRef;
$thingCollection=$this->dbObj->selectCollection("Thingtest");
$thingCollection->Insert($thing);
//Back ref
$recipeback=$this->dbObj->RecipeTest;
$RecipeResult = $recipeback->findOne(array("ingredients" => "suth"));
echo 'Result'.$RecipeResult['_id'];
print_r($RecipeResult);
$CWback=$this->dbObj->CreativeWorkTest;
//$CWbackResult = MongoDBRef::get($CWback->db, $RecipeResult['_id']);
$CWbackResult = $CWback->findOne(array("about" => "test1"));
print_r($CWbackResult);
}
//Update collection based on Criteria and New data.
public function SaveCollection($obj,$id)
{
//save obj values into Collection
// save will insert if obj doesn't exists in database or updates obj if exists.
if(!is_null($obj)|| !is_null($this->Collect))
if (!is_null($id))
{
$obj['_id']=$id;
}
$this->Collect->save($obj);
return $obj['_id'];
}
//Update collection based on Criteria and New data.
public function UpdateCollection($colName,$criteria,$newData)
{
//Insert obj values into Collection
if(!is_null($colName)|| !is_null($this->Collect))
$this->Collect=$this->dbObj->selectCollection("$colName");
$this->Collect->update($criteria, $newData);
}
//Remove collection Record
public function RemoveCollection($colName,$criteria)
{
//Insert obj values into Collection
if(!is_null($colName)|| !is_null($this->Collect))
$this->Collect=$this->dbObj->selectCollection("$colName");
$this->Collect->remove($criteria, true );
}
}
/*SingleTon design Pattern Implementation*/
class singleton
{
private static $instance;
private $count = 0;
private function __construct()
{
}
public static function singleton($username,$password)
{
if (!(self::$instance)) {
$className = __CLASS__;
self::$instance = new Mongo("mongodb://${username}:${password}@localhost/test",array("persist" => "x"));;
}
return self::$instance;
}
public function increment()
{
return $this->count++;
}
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup()
{
trigger_error('Unserializing is not allowed.', E_USER_ERROR);
}
}
?>