Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CTable Class Reference

Inherited by COrderBase, COrderDetailBase, COrderHistoryBase, COrderTrackingBase, CPaymentHistoryBase, CPersonBase, CProductBase, and CSetupBase.

Public Member Functions

 __construct (\SDExtension\DB\CDataBase $oDB)
 
 GetDB ()
 
 Reset ()
 
 Save ($aActualValues=null)
 
 UpdateSave ($aActualValues=[], $bModified=true, $bAutoId=false)
 
 Set ($aActualValues=[], $bModified=true)
 
 Get ()
 
 GetRecords ()
 
 Load ($aWhere=[], $nReturnType=\PDO::FETCH_ASSOC)
 
 LoadByValue ($sColumn, $sValue)
 
 LoadByID ($sValue)
 
 Reload ($sID=null)
 
 CreateOriginalCopies ()
 
 Validate ($aActualValues=[])
 

Protected Member Functions

 GetRules ()
 

Protected Attributes

 $m_oDB = null
 
 $m_sTable = 'table'
 
 $m_sIdColumn = 'id'
 
 $m_bAutoID = true
 
 $m_aDefaultValues = []
 
 $m_aActualValues = []
 
 $m_aRules = []
 
 $m_bNewRecord
 
 $m_bModified
 

Detailed Description

Definition at line 14 of file CTable.php.

Constructor & Destructor Documentation

__construct ( \SDExtension\DB\CDataBase  $oDB)

__construct - Object constructor method

public

Parameters
CDataBase$oDBDatabase Object
Returns
void

Definition at line 70 of file CTable.php.

71  {
72  $this->m_oDB = $oDB;
73  $this->Reset();
74  }

Member Function Documentation

CreateOriginalCopies ( )

CreateOriginalCopies - Creates Original Copies

public

Returns
void

Definition at line 294 of file CTable.php.

295  {
296  foreach ($this->m_aActualValues as $sKey => $sValue)
297  {
298  foreach (array("Original ", "dOriginal", "sOriginal", "sOrig") as $sOriginalPrefix)
299  {
300  $sAltKey = substr($sKey, 1);
301  if (isset($this->m_aDefaultValues["$sOriginalPrefix$sKey"]))
302  {
303  $this->m_aActualValues["$sOriginalPrefix$sKey"] = $sValue;
304  }
305  elseif (isset($this->m_aDefaultValues["$sOriginalPrefix$sAltKey"]))
306  {
307  $this->m_aActualValues["$sOriginalPrefix$sAltKey"] = $sValue;
308  }
309  }
310  }
311  }
Get ( )

Get - Gets Active record values

public

Returns
array Actual record values

Definition at line 177 of file CTable.php.

178  {
179  return $this->m_aActualValues;
180  }
GetDB ( )

GetDB - DB

public

Returns
object DB

Definition at line 82 of file CTable.php.

83  {
84  return $this->m_oDB->GetDB();
85  }
GetRecords ( )

GetRecords - Gets all records from the DB

public

Returns
GeneratorObject

Definition at line 188 of file CTable.php.

189  {
190  foreach ($this->m_oDB->Select($this->m_sTable) as $row)
191  {
192  yield $row;
193  }
194  }
GetRules ( )
protected

GetRules - Get Validator rules

protected

Returns
array Rules

Definition at line 340 of file CTable.php.

341  {
342  return $this->m_aRules;
343  }
Load (   $aWhere = [],
  $nReturnType = \PDO::FETCH_ASSOC 
)

Load - Loads one record from the DB (the first record according to the condition)

public

Parameters
array$aWhereConditions
int$nReturnTypeReturn Type (associative array by default)
Returns
array|object Record

Definition at line 204 of file CTable.php.

205  {
206  $oQueryResult = $this->m_oDB->Select($this->m_sTable, "*", $aWhere);
207  if ($oQueryResult === false)
208  {
209  return [];
210  }
211  $this->m_bNewRecord = false;
212  $vRecord = $oQueryResult->fetch($nReturnType);
213  if ($vRecord === false)
214  {
215  return [];
216  }
217  return $this->Set($vRecord, false);
218  }
Set($aActualValues=[], $bModified=true)
Definition: CTable.php:164
LoadByID (   $sValue)

LoadByID - Loads one record by given ID value

public

Parameters
string$sValueColumn value
Returns
array Record

Definition at line 241 of file CTable.php.

242  {
243  $aWhere = $this->GenerateWhereID($sValue);
244  return $this->Load($aWhere);
245  }
Load($aWhere=[], $nReturnType=\PDO::FETCH_ASSOC)
Definition: CTable.php:204
LoadByValue (   $sColumn,
  $sValue 
)

LoadByValue - Loads one record by condition: Column = Value

public

Parameters
string$sColumnColumn name
string$sValueColumn value
Returns
array Record

Definition at line 228 of file CTable.php.

229  {
230  $aWhere = $this->GenerateWhereCondition($sColumn, $sValue);
231  return $this->Load($aWhere);
232  }
Load($aWhere=[], $nReturnType=\PDO::FETCH_ASSOC)
Definition: CTable.php:204
Reload (   $sID = null)

Reload - Reloads Actual record, or loads a new one by given ID value

public

Parameters
string$sValueColumn value
Returns
array Record

Definition at line 254 of file CTable.php.

255  {
256  if ($sID == null)
257  {
258  $sID = $this->m_aActualValues[$this->m_sIdColumn];
259  }
260  return $this->LoadByID($sID);
261  }
Reset ( )

Reset - Resets class variables

public

Returns
void

Definition at line 93 of file CTable.php.

94  {
95  $this->m_bNewRecord = true;
96  $this->m_bModified = false;
97  $this->m_aActualValues = $this->m_aDefaultValues;
98  }
Save (   $aActualValues = null)

Save - Saves (inserts or updates) actual record to DB

public

Parameters
array | null$aActualValuesActual Values
Returns
void

Definition at line 107 of file CTable.php.

108  {
109  if ($aActualValues == null)
110  {
111  $aActualValues = $this->m_aActualValues;
112  }
113  elseif (is_array($aActualValues))
114  {
115  $this->m_aActualValues = $aActualValues;
116  }
117  $sID = arr_get($aActualValues, $this->m_sIdColumn, '');
118  if ($this->m_bModified)
119  {
120  if ($this->m_bNewRecord)
121  {
122  if (empty($sID))
123  {
124  unset($aActualValues[$this->m_sIdColumn]);
125  }
126  $sID = $this->m_oDB->Insert($this->m_sTable, $aActualValues, $this->m_bAutoID, $this->m_sIdColumn);
127  }
128  else
129  {
130  unset($aActualValues[$this->m_sIdColumn]);
131  $this->m_oDB->Update($this->m_sTable, $aActualValues, $this->GenerateWhereID($sID));
132  }
133  $this->m_bModified = false;
134  }
135  return $sID;
136  }
Set (   $aActualValues = [],
  $bModified = true 
)

Set - Sets Active record values

public

Parameters
array$aActualValuesValues to set
bool$bModifiedIs modified?
Returns
array Actual record values

Definition at line 164 of file CTable.php.

165  {
166  $this->m_bModified = $bModified;
167  $this->m_aActualValues = array_merge($this->m_aActualValues, $aActualValues);
168  return $this->m_aActualValues;
169  }
UpdateSave (   $aActualValues = [],
  $bModified = true,
  $bAutoId = false 
)

UpdateSave - Updates record and saves it to the DB

public

Parameters
array$aActualValuesValues to set
bool$bModifiedIs modified?
bool$bAutoIdGet id from Actual Record
Returns
array Actual record values

Definition at line 147 of file CTable.php.

148  {
149  $vID = arr_get($bAutoId ? $this->m_aActualValues : $aActualValues, $this->m_sIdColumn, '');
150  unset($aActualValues[$this->m_sIdColumn]);
151  $this->m_oDB->Update($this->m_sTable, $aActualValues, $this->GenerateWhereID($vID));
152  $aActualValues[$this->m_sIdColumn] = $vID;
153  $this->Set($aActualValues, $bModified);
154  }
Set($aActualValues=[], $bModified=true)
Definition: CTable.php:164
Validate (   $aActualValues = [])

Validate - Validates record

public

Parameters
array$aActualValuesColumn value
Returns
mixed true | Array of errors

Definition at line 320 of file CTable.php.

321  {
322  $oValidationResult = \SimpleValidator\Validator::validate($aActualValues, $this->GetRules());
323  $oValidationResult->customErrors(load_config(CONFIG_APP_ROOT . "validator/en.php"));
324  if ($oValidationResult->isSuccess() == true)
325  {
326  return true;
327  }
328  else
329  {
330  return $oValidationResult->getErrors();
331  }
332  }

The documentation for this class was generated from the following file: