| author | Bashkim Isai |
|---|---|
| package | OXRM |
In file D:\oblib-core\php\oblib\dataOXRM\dataEntity.datatype.php between lines 51 and 534.
<?php
/**
* The dataEntities class deals with handling entity objects. In
* 'java' terms, this would act like a container for accessing
* items in a database.
*
* @author Bashkim Isai
* @package OXRM
* @see Object [data]
*/
abstract class dataEntities extends data
{
/**
* The dataOXRM object which instantiated this dataEntities entity
*
* @type Object [dataOXRM]
*/
public $_doxOXRM;
/**
* The DOMXPath which allows the traversing of the entire OXRM
* document which this dataEntities entity relates to
*
* @type Object [DOMXPath]
*/
public $_dxpOXRM;
/**
* The DOMNode Object which contains the OXRM representation of
* this dataEntities entity
*
* @type Object [DOMNode]
*/
public $_dnoEntity;
/**
* The name of the type of class which represents one single
* row in the database of this entity type
*
* @type String
*/
private $_strClassOne;
/**
* The name of the Class which holds 'many' of these objects. This
* will be the name of the class which instantiates this dataEntities
* object
*
* @type String
*/
private $_strClassMany;
/**
* The table in the database which corresponds with this Object
*
* @type String
*/
private $_strTable;
/**
* A list of properties which are attached to this particular
* entity type. These will relate to fields in the database and
* fields represented in the ObLib dataEntity object for this
* class type
*
* @type Object [DOMNodeList]
*/
private $_dnlProperty;
/**
* The primary key field. This field is persistant throughout the
* entirity of all dataEntities objects.
*
* @type String
*/
private $_strPrimary;
/**
* Receives the associated OXRM document for this Entity type
* and the OXRM XML Node which contains all the information about
* where data resides in the system
*
* @see Object [data]
* @see Object [dataOXRM]
*/
final function __construct (dataOXRM &$doxOXRM, &$dnoEntity)
{
$this->_doxOXRM =& $doxOXRM;
$this->_dxpOXRM =& $doxOXRM->_dxpOXRM;
$this->_dnoEntity =& $dnoEntity;
$this->_strClassOne = $this->_dxpOXRM->Query ('./class[@multiplicity="1"]', $dnoEntity)->item (0)->nodeValue;
$this->_strClassMany = $this->_dxpOXRM->Query ('./class[@multiplicity="*"]', $dnoEntity)->item (0)->nodeValue;
$this->_strTable = $this->_dxpOXRM->Query ('./table', $dnoEntity)->item (0)->nodeValue;
$this->_dnlProperty = $this->_dxpOXRM->Query ('./property', $dnoEntity);
$dnoPrimary = $this->_dxpOXRM->Query ('./primary', $dnoEntity)->item (0);
$this->_strPrimary = ($dnoPrimary) ? $dnoPrimary->nodeValue : 'Id';
parent::__construct ($this->_strClassMany);
if (method_exists ($this, '___construct'))
{
$this->___construct ();
}
}
/**
* Accessor method for self property _strClassOne
* Will tell you the name of the single instance of
* this entity type containing a database row
*
* @return String
*/
final public function ___ClassOne ()
{
return $this->_strClassOne;
}
/**
* Accessor method for self property _strClassMany
* Will tell you the name of the expected instantiator
* for this class
*
* @return String
*/
final public function ___ClassMany ()
{
return $this->_strClassMany;
}
/**
* Accessor method for self property _strTable
* Will tell you which database table is being used
*
* @return String
*/
final public function ___Table ()
{
return $this->_strTable;
}
/**
* Accessor method for self property _dnlProperty
* Will tell you what properties are associated with this entity
* (e.g.: database fields)
*
* @return Object [DOMNodeList]
*/
final public function ___PropertyList ()
{
return $this->_dnlProperty;
}
/**
* Will write up the Field Listing for a standard SELECT statement
*
* @return String
*/
final public function ___PropertySelect ($strTableAlias)
{
$arrProperty = Array ();
foreach ($this->_dnlProperty as $dnoProperty)
{
$strPropertyName = $dnoProperty->getAttribute ('name');
if (!$strPropertyName)
{
throw new ExceptionCore ('A field does not contain a name');
}
$arrProperty [] = '`' . $strTableAlias . '`.`' . $strPropertyName . '`';
}
return implode (', ', $arrProperty);
}
/**
* Get an object from the database by its Id. Returns a Query
* and expects you to render it (incase you want to add more
* items to the WHERE clause). This method is optimized to only
* return 1 item
*
* @return Object [dataEntityQuery]
*/
final public function ___ByPrimary ($intId)
{
if (!$this->_strPrimary)
{
return NULL;
}
$deqEntity = $this->___Search ();
$deqEntity->Sample (1, 1);
$eqbEntity = $deqEntity->Where ();
$eqbEntity->Constrain ($this->_strPrimary, SQL_EQUALS, $intId);
return $deqEntity;
}
/**
* Skeleton for searching the Database for an Entity's row
*
* @return Object [dataEntityQuery]
*/
final public function ___Search ()
{
return new dataEntityQuery ($this);
}
/**
* Create a new instance of an object of this entity type.
* By passing through a blank Array for the first parameter,
* you can create a blank object which is useful for when you
* want to validate information or when you are going to do
* an INSERT statement
*
* @return Object [typeOf _strClassOne]
*/
final public function ___Load ($arrData=Array())
{
return new $this->_strClassOne ($this, $arrData);
}
/**
* Inserts a Sterile Object into the database for this entity type
*
* @return Boolean
*/
final public function ___Insert (&$objEntity)
{
if (!$objEntity->___Sterile ())
{
return FALSE;
}
$arrField = Array ();
$arrValue = Array ();
$arrParam = Array ();
$strParam = '';
foreach ($this->_dnlProperty as $dnoProperty)
{
$strFieldName = $dnoProperty->getAttribute ('name');
$bolFieldNull = $dnoProperty->getAttribute ('null');
$strNodeValue = $objEntity->___Pull ($strFieldName)->___getSQL ();
$arrField [] = $strFieldName;
if ($this->_strPrimary != $strFieldName && $strNodeValue === NULL)
{
if ($bolFieldNull)
{
$arrValue [] = 'NULL';
}
else
{
throw new ExceptionCore ('The value NULL was passed for the non-null field: ' . $strFieldName);
}
}
else
{
$strParam .= 's';
$arrValue [] = '?';
$arrParam [] = $strNodeValue;
}
}
$strStatement = 'INSERT INTO `' . $this->_strTable . '` ';
$strStatement .= '(`' . implode ('`, `', $arrField) . '`) ';
$strStatement .= 'VALUES(' . implode (', ', $arrValue) . ')';
array_unshift ($arrParam, $strParam);
// Prepare and Execute the Insert Statement
$smtStatement = $GLOBALS ['MySQL']->Prepare ($strStatement);
if (!$smtStatement)
{
throw new ExceptionMysql ($GLOBALS ['MySQL']);
}
call_user_func_array (
Array ($smtStatement, 'bind_param'),
$arrParam
);
$bolResponse = $smtStatement->execute ();
$smtStatement->free_result ();
// If there was a failure, tell us
if (!$bolResponse)
{
throw new ExceptionMysql ($GLOBALS ['MySQL']);
}
if ($GLOBALS ['MySQL']->insert_id)
{
$objEntity->{$this->_strPrimary} = $GLOBALS ['MySQL']->insert_id;
}
return TRUE;
}
/**
* Updates the database to reflect the new information for an object
* if it is sterile
*
* @return Boolean
*/
final public function ___Update (&$objEntity)
{
if (!$objEntity->___Sterile ())
{
return FALSE;
}
// Setup the Update Params (b [binary] i [integer] s [string] d [double], etc) and the Params being Passed
$arrField = Array ();
$arrParam = Array ();
$strParam = '';
// Start the Update Statement
foreach ($this->_dnlProperty as $dnoProperty)
{
$strFieldName = $dnoProperty->getAttribute ('name');
$bolFieldNull = $dnoProperty->getAttribute ('null');
$arrField [] = '`' . $strFieldName . '` = ?';
$arrParam [] = $objEntity->___Pull ($strFieldName)->___getSql ();
$strParam .= 's';
}
$strStatement = 'UPDATE `' . $this->_strTable . '` ';
$strStatement .= 'SET ' . implode (', ', $arrField) . ' ';
$strStatement .= 'WHERE `' . $this->_strPrimary . '` = ? ';
$strStatement .= 'LIMIT 1';
$arrParam [] = $objEntity->{$this->_strPrimary};
$strParam .= 'i';
array_unshift ($arrParam, $strParam);
// Prepare and Execute the Update Statement
$smtStatement = $GLOBALS ['MySQL']->Prepare ($strStatement);
if (!$smtStatement)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
call_user_func_array (
Array ($smtStatement, 'bind_param'),
$arrParam
);
$bolResponse = $smtStatement->execute ();
$smtStatement->free_result ();
// If there was a failure, tell us
if (!$bolResponse)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
return TRUE;
}
/**
* Skeleton for Deleting entity rows from the Database
*
* @return Object [dataEntityQueryCull]
*/
final public function ___Cull ()
{
return new dataEntityQueryCull ($this);
}
/**
* Deletes a single item from the Database
*
* @return Boolean
*/
final public function ___Pluck (&$objEntity)
{
$decCull = $this->___Cull ();
$eqbCull = $decCull->Where ();
$eqbCull->Constrain ($this->_strPrimary, SQL_EQUALS, $objEntity->{$this->_strPrimary});
$decCull->___Render ();
$objEntity->{$this->_strPrimary} = NULL;
return TRUE;
}
/**
* Get the Primary Key (Field Name)
*
* @return String
*/
public function ___Primary ()
{
return $this->_strPrimary;
}
/**
* Executes a custom SQL statement and returns the result as an Array of Entities
*
* @see Class [dataSeries]
* @see Class [dataOXRM]
* @return Object [dataSeries [Mix]]
*/
public function ___RenderSql ($strStatement, $arrParam)
{
// This is what the Information will be outputted into
$arrResponse = new dataSeries ($this->_strClassMany, $this->_strClassOne);
// Build the Response
$smtStatement = $GLOBALS ['MySQL']->Prepare ($strStatement);
if (!$smtStatement)
{
throw new ExceptionMysql ($GLOBALS ['MySQL'], $strStatement);
}
if ($arrParam)
{
call_user_func_array (
Array ($smtStatement, 'bind_param'),
$arrParam
);
}
$smtStatement->execute ();
// Put the Database response into an ObLib object
$rmdMetaData = $smtStatement->result_metadata ();
$arrData = Array ();
$arrReference = Array ();
foreach ($rmdMetaData->fetch_fields () as $objField)
{
$arrData [$objField->name] = '';
$arrReference [] =& $arrData [$objField->name];
}
call_user_func_array (
Array ($smtStatement, 'bind_result'),
$arrReference
);
while ($smtStatement->Fetch ())
{
$objRow = $this->___Load ($arrData);
$arrResponse->___Push ($objRow);
}
$smtStatement->free_result ();
// Return the Response
return $arrResponse;
}
}
?>