-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBLayer.php
176 lines (161 loc) · 5.53 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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;
Protected $RecipeArray;
private $arr;
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 Search Paramter,Retrive all child documents and then create nested array
and sent back to Caller.*/
public function get_CollectionObjectbysearchParameter($colName,$SrchParm,$srchprmval)
{
$this->Collect=$this->dbObj->selectCollection("Thingtest");
$cursor = $this->Collect->find(array($SrchParm => $srchprmval));
$this->i=0;
/*Loop through all parent records and retrive child based on _ID attribute*/
//if ($cursor->hasNext())
//{
while ($document = $cursor->getNext())
{
//echo $document['_id'];
$CWback=$this->dbObj->CreativeWorkTest;
//$CWbackResult = MongoDBRef::get($CWback->db, $RecipeResult['_id']);
$CWbackResult = $CWback->findone(array("_id" => $document['_id']));
$recipeback=$this->dbObj->RecipeTest;
$RecipeResult = $recipeback->findOne(array("_id" => $CWbackResult['_id']));
/*Creating array of one recipe document*/
$this->RecipeArray=array(
"Thing"=>$document,
"CreativeWork"=>$CWbackResult,
"Recipe"=>$RecipeResult
);
$this->arr[$this->i]=$this->RecipeArray;
$this->i=$this->i+1;
}
//print_r($arr);
return $this->arr;
}
public function InsertCollection($obj)
{
$Recipe=$obj["Recipe"];
$RecipeCollection=$this->dbObj->selectCollection("RecipeTest");
$RecipeCollection->Insert($Recipe);
//$RecipeRef = MongoDBRef::create($RecipeCollection->getName(),$Recipe['_id']);
$CreativeWork=$obj["CreativeWork"];
//$CreativeWork["RecipeReference"]=$RecipeRef;
$CreativeWork['_id']=$Recipe['_id'];
$CreativeWorkCollection=$this->dbObj->selectCollection("CreativeWorkTest");
$CreativeWorkCollection->Insert($CreativeWork);
//$CreativeWrokRef = MongoDBRef::create($CreativeWorkCollection->getName(), $CreativeWork['_id']);
$thing=$obj["Thing"];
$thingCollection=$this->dbObj->selectCollection("Thingtest");
$thing['_id']=$CreativeWork['_id'];
$thingCollection->Insert($thing);
$recipeback=$this->dbObj->RecipeTest;
$RecipeResult = $recipeback->findOne(array("ingredients" => "Chicken"));
//echo 'Result'.$RecipeResult['_id'];
//print_r($RecipeResult);
$CWback=$this->dbObj->CreativeWorkTest;
$CWbackResult = $CWback->findOne(array("_id" => $RecipeResult['_id']));
//print_r($CWbackResult);
$Thback=$this->dbObj->Thingtest;
//$CWbackResult = MongoDBRef::get($CWback->db, $RecipeResult['_id']);
$ThbackResult = $Thback->findOne(array("_id" => $CreativeWork['_id']));
//print_r($ThbackResult);
}
//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);
}
}
?>