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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
|
/**
* Base DataMapper Model
*
* @package phpDataMapper
* @link http://phpdatamapper.com
* @link http://github.com/vlucas/phpDataMapper
*/
class phpDataMapper_Base
{
// Class Names for required classes - Here so they can be easily overridden
protected $_entityClass = 'phpDataMapper_Entity';
protected $_queryClass = 'phpDataMapper_Query';
protected $_collectionClass = 'phpDataMapper_Collection';
protected $_exceptionClass = 'phpDataMapper_Exception';
// Stored adapter connections
protected $_adapter;
protected $_adapterRead;
// Array of error messages and types
protected $_errors = array();
// Query log
protected static $_queryLog = array();
// Store cached field info
protected $_fields = array();
protected $_relations = array();
protected $_primaryKey;
// Data source setup info
protected $_datasource;
/**
=== EXAMPLE fields ===
public $id = array('type' => 'int', 'primary' => true);
public $name = array('type' => 'string', 'required' => true);
public $date_created = array('type' => 'datetime');
=== EXAMPLE Relationship associations ===
public $comments = array(
'type' => 'relation',
'relation' => 'HasMany',
'mapper' => 'CommentsModel',
'where' => array('comment_id' => 'entity.id'),
);
======================
*/
/**
* Constructor Method
*/
public function __construct(phpDataMapper_Adapter_Interface $adapter, $adapterRead = null)
{
$this->_adapter = $adapter;
// Ensure required classes for minimum activity are loaded
$this->loadClass($this->_entityClass);
$this->loadClass($this->_queryClass);
$this->loadClass($this->_collectionClass);
$this->loadClass($this->_exceptionClass);
// Slave adapter if given (usually for reads)
if(null !== $adapterRead) {
if($adapterRead instanceof phpDataMapper_Adapter_Interface) {
$this->_adapterRead = $adapterRead;
} else {
throw new InvalidArgumentException("Secondary/Slave adapter must implement 'phpDataMapper_Adapter_Interface'");
}
}
// Ensure table has been defined
if(!$this->_datasource) {
throw new $this->_exceptionClass("Error: Datasource name must be defined - please define the \$_datasource variable. This can be a database table name, collection or bucket name, a file name, or a URL, depending on your adapter.");
}
// Ensure fields have been defined for current table
if(!$this->fields()) {
throw new $this->_exceptionClass("Error: Fields must be defined");
}
// Call init for extension without overriding constructor
$this->init();
}
/**
* Initialization function, run immediately after __construct() so that the constructor is never overridden
*/
public function init()
{
}
/**
* Get current adapter object
*/
public function adapter()
{
return $this->_adapter;
}
/**
* Get adapter object that will serve as the 'slave' for reads
*/
public function adapterRead()
{
if($this->_adapterRead) {
return $this->_adapterRead;
} else {
return $this->_adapter;
}
}
/**
* Get entity class name to use
*
* @return string
*/
public function entityClass()
{
return $this->_entityClass;
}
/**
* Get query class name to use
*
* @return string
*/
public function queryClass()
{
return $this->_queryClass;
}
/**
* Get collection class name to use
*
* @return string
*/
public function collectionClass()
{
return $this->_collectionClass;
}
/**
* Get name of the data source
*/
public function datasource()
{
return $this->_datasource;
}
/**
* Get formatted fields with all neccesary array keys and values.
* Merges defaults with defined field values to ensure all options exist for each field.
*
* @return array Defined fields plus all defaults for full array of all possible options
*/
public function fields()
{
if($this->_fields) {
$returnFields = $this->_fields;
} else {
$getFields = create_function('$obj', 'return get_object_vars($obj);');
$fields = $getFields($this);
// Default settings for all fields
$fieldDefaults = array(
'type' => 'string',
'default' => null,
'length' => null,
'required' => false,
'null' => true,
'unsigned' => false,
'primary' => false,
'index' => false,
'unique' => false,
'serial' => false,
'relation' => false
);
// Type default overrides for specific field types
$fieldTypeDefaults = array(
'string' => array(
'length' => 255
),
'float' => array(
'length' => array(10,2)
),
'int' => array(
'length' => 10,
'unsigned' => true
)
);
$returnFields = array();
foreach($fields as $fieldName => $fieldOpts) {
// Format field will full set of default options
if(isset($fieldInfo['type']) && isset($fieldTypeDefaults[$fieldOpts['type']])) {
// Include type defaults
$fieldOpts = array_merge($fieldDefaults, $fieldTypeDefaults[$fieldOpts['type']], $fieldOpts);
} else {
// Merge with defaults
$fieldOpts = array_merge($fieldDefaults, $fieldOpts);
}
// Store primary key
if($fieldOpts['primary'] === true) {
$this->_primaryKey = $fieldName;
}
// Store relations (and remove them from the mix of regular fields)
if($fieldOpts['type'] == 'relation') {
$this->_relations[$fieldName] = $fieldOpts;
continue; // skip, not a field
}
$returnFields[$fieldName] = $fieldOpts;
}
$this->_fields = $returnFields;
}
return $returnFields;
}
/**
* Get defined relations
*/
public function relations()
{
if(!$this->_relations) {
$this->fields();
}
return $this->_relations;
}
/**
* Get value of primary key for given row result
*/
public function primaryKey(phpDataMapper_Entity $entity)
{
$pkField = $this->primaryKeyField();
return $entity->$pkField;
}
/**
* Get value of primary key for given row result
*/
public function primaryKeyField()
{
return $this->_primaryKey;
}
/**
* Check if field exists in defined fields
*/
public function fieldExists($field)
{
return array_key_exists($field, $this->fields());
}
/**
* Load record from primary key
*/
public function get($primaryKeyValue = 0)
{
// Create new row object
if(!$primaryKeyValue) {
$entity = new $this->_entityClass();
// Find record by primary key
} else {
$entity = $this->first(array($this->primaryKeyField() => $primaryKeyValue));
}
return $entity;
}
/**
* Load defined relations
*/
public function getRelationsFor(phpDataMapper_Entity $entity)
{
$relatedColumns = array();
$rels = $this->getEntityRelationWithValues($entity);
if(count($rels) > 0) {
foreach($rels as $column => $relation) {
$mapperName = isset($relation['mapper']) ? $relation['mapper'] : false;
if(!$mapperName) {
throw new $this->_exceptionClass("Relationship mapper for '" . $column . "' has not been defined.");
}
// Set conditions for relation query
$relConditions = array();
if(isset($relation['where'])) {
$relConditions = $relation['where'];
}
// Create new instance of mapper
$mapper = new $mapperName($this->adapter());
// Load relation class
$relationClass = 'phpDataMapper_Relation_' . $relation['relation'];
if($loadedRel = $this->loadClass($relationClass)) {
// Set column equal to relation class instance
$relationObj = new $relationClass($mapper, $relConditions, $relation);
$relatedColumns[$column] = $relationObj;
}
}
}
return (count($relatedColumns) > 0) ? $relatedColumns : false;
}
/**
* Replace entity value placeholders on relation definitions
* Currently replaces 'entity.[col]' with the column value from the passed entity object
*/
public function getEntityRelationWithValues(phpDataMapper_Entity $entity)
{
$rels = $this->relations();
if(count($rels) > 0) {
foreach($rels as $column => $relation) {
// Load foreign keys with data from current row
// Replace 'entity.[col]' with the column value from the passed entity object
if(isset($relation['where'])) {
foreach($relation['where'] as $relationCol => $col) {
if(is_string($col) && strpos($col, 'entity.') !== false) {
$col = str_replace('entity.', '', $col);
$rels[$column]['where'][$relationCol] = $entity->$col;
}
}
}
}
}
return $rels;
}
/**
* Get result set for given PDO Statement
*/
public function getResultSet($stmt)
{
if($stmt instanceof PDOStatement) {
$results = array();
$resultsIdentities = array();
// Set object to fetch results into
$stmt->setFetchMode(PDO::FETCH_CLASS, $this->_entityClass);
// Fetch all results into new DataMapper_Result class
while($entity = $stmt->fetch(PDO::FETCH_CLASS)) {
// Load relations for this row
$relations = $this->getRelationsFor($entity);
if($relations && is_array($relations) && count($relations) > 0) {
foreach($relations as $relationCol => $relationObj) {
$entity->$relationCol = $relationObj;
}
}
// Store in array for ResultSet
$results[] = $entity;
// Store primary key of each unique record in set
$pk = $this->primaryKey($entity);
if(!in_array($pk, $resultsIdentities) && !empty($pk)) {
$resultsIdentities[] = $pk;
}
// Mark row as loaded
$entity->loaded(true);
}
// Ensure set is closed
$stmt->closeCursor();
return new $this->_collectionClass($results, $resultsIdentities);
} else {
return array();
//throw new $this->_exceptionClass(__METHOD__ . " expected PDOStatement object");
}
}
/**
* Find records with given conditions
* If all parameters are empty, find all records
*
* @param array $conditions Array of conditions in column => value pairs
*/
public function all(array $conditions = array())
{
return $this->select()->where($conditions);
}
/**
* Find first record matching given conditions
*
* @param array $conditions Array of conditions in column => value pairs
*/
public function first(array $conditions = array())
{
$query = $this->select()->where($conditions)->limit(1);
$entitys = $this->adapterRead()->read($query);
if($entitys) {
return $entitys->first();
} else {
return false;
}
}
/**
* Find records with custom SQL query
*
* @param string $sql SQL query to execute
* @param array $binds Array of bound parameters to use as values for query
* @throws phpDataMapper_Exception
*/
public function query($sql, array $binds = array())
{
// Add query to log
self::logQuery($sql, $binds);
// Prepare and execute query
if($stmt = $this->adapter()->prepare($sql)) {
$results = $stmt->execute($binds);
if($results) {
$r = $this->getResultSet($stmt);
} else {
$r = false;
}
return $r;
} else {
throw new $this->_exceptionClass(__METHOD__ . " Error: Unable to execute SQL query - failed to create prepared statement from given SQL");
}
}
/** dieng test query **/
public function querytwo($sql, array $binds = array())
{
// Add query to log
self::logQuery($sql, $binds);
// Prepare and execute query
if($stmt = $this->adapter()->prepare($sql)) {
$results = $stmt->execute($binds);
return $results;
} else {
throw new $this->_exceptionClass(__METHOD__ . " Error: Unable to execute SQL query - failed to create prepared statement from given SQL");
}
}
/** end test query **/
/**
* Begin a new database query - get query builder
* Acts as a kind of factory to get the current adapter's query builder object
*
* @param mixed $fields String for single field or array of fields
*/
public function select($fields = "*")
{
$query = new $this->_queryClass($this);
$query->select($fields, $this->datasource());
return $query;
}
/**
* Save related rows of data
*/
protected function saveRelatedRowsFor($entity, array $fillData = array())
{
$relationColumns = $this->getRelationsFor($entity);
foreach($entity->toArray() as $field => $value) {
if($relationColumns && array_key_exists($field, $relationColumns) && (is_array($value) || is_object($value))) {
foreach($value as $relatedRow) {
// Determine relation object
if($value instanceof phpDataMapper_Relation) {
$relatedObj = $value;
} else {
$relatedObj = $relationColumns[$field];
}
$relatedMapper = $relatedObj->mapper();
// Row object
if($relatedRow instanceof phpDataMapper_Entity) {
$relatedRowObj = $relatedRow;
// Associative array
} elseif(is_array($relatedRow)) {
$relatedRowObj = new $this->_entityClass($relatedRow);
}
// Set column values on row only if other data has been updated (prevents queries for unchanged existing rows)
if(count($relatedRowObj->dataModified()) > 0) {
$fillData = array_merge($relatedObj->foreignKeys(), $fillData);
$relatedRowObj->data($fillData);
}
// Save related row
$relatedMapper->save($relatedRowObj);
}
}
}
}
/**
* Save record
* Will update if primary key found, insert if not
* Performs validation automatically before saving record
*
* @param mixed $entity Entity object or array of field => value pairs
*/
public function save($entity)
{
if(is_array($entity)) {
$entity = $this->get()->data($entity);
}
if(!($entity instanceof phpDataMapper_Entity)) {
throw new $this->_exceptionClass(__METHOD__ . " first argument must be entity object or array");
}
// Run validation
if($this->validate($entity)) {
$pk = $this->primaryKey($entity);
// No primary key, insert
if(empty($pk)) {
$result = $this->insert($entity);
// Has primary key, update
} else {
$result = $this->update($entity);
}
} else {
$result = false;
}
return $result;
}
/**
* Insert record
*
* @param mixed $entity Entity object or array of field => value pairs
*/
public function insert($entity)
{
if(is_array($entity)) {
$entity = $this->get()->data($entity);
}
if(!($entity instanceof phpDataMapper_Entity)) {
throw new $this->_exceptionClass(__METHOD__ . " first argument must be entity object or array");
}
$data = array();
$entityData = $entity->toArray();
foreach($entityData as $field => $value) {
if($this->fieldExists($field)) {
// Empty values will be NULL (easier to be handled by databases)
$data[$field] = $this->isEmpty($value) ? null : $value;
}
}
// Ensure there is actually data to update
if(count($data) > 0) {
$result = $this->adapter()->create($this->datasource(), $data);
// Update primary key on row
$pkField = $this->primaryKeyField();
$entity->$pkField = $result;
// Load relations for this row so they can be used immediately
$relations = $this->getRelationsFor($entity);
if($relations && is_array($relations) && count($relations) > 0) {
foreach($relations as $relationCol => $relationObj) {
$entity->$relationCol = $relationObj;
}
}
} else {
$result = false;
}
// Save related rows
if($result) {
$this->saveRelatedRowsFor($entity);
}
return $result;
}
/**
* Update given row object
*/
public function update(phpDataMapper_Entity $entity)
{
// Ensure fields exist to prevent errors
$binds = array();
foreach($entity->dataModified() as $field => $value) {
if($this->fieldExists($field)) {
// Empty values will be NULL (easier to be handled by databases)
$binds[$field] = $this->isEmpty($value) ? null : $value;
}
}
// Handle with adapter
if(count($binds) > 0) {
$result = $this->adapter()->update($this->datasource(), $binds, array($this->primaryKeyField() => $this->primaryKey($entity)));
} else {
$result = true;
}
// Save related rows
if($result) {
$this->saveRelatedRowsFor($entity);
}
return $result;
}
/**
* Delete items matching given conditions
*
* @param mixed $conditions Array of conditions in column => value pairs or Entity object
*/
public function delete($conditions)
{
if($conditions instanceof phpDataMapper_Entity) {
$conditions = array(
0 => array('conditions' => array($this->primaryKeyField() => $this->primaryKey($conditions)))
);
}
if(is_array($conditions)) {
return $this->adapter()->delete($this->datasource(), $conditions);
} else {
throw new $this->_exceptionClass(__METHOD__ . " conditions must be entity object or array, given " . gettype($conditions) . "");
}
}
/**
* Truncate data source
* Should delete all rows and reset serial/auto_increment keys to 0
*/
public function truncateDatasource() {
return $this->adapter()->truncateDatasource($this->datasource());
}
/**
* Drop/delete data source
* Destructive and dangerous - drops entire data source and all data
*/
public function dropDatasource() {
return $this->adapter()->dropDatasource($this->datasource());
}
/**
* Run set validation rules on fields
*
* @todo A LOT more to do here... More validation, break up into classes with rules, etc.
*/
public function validate(phpDataMapper_Entity $entity)
{
// Check validation rules on each feild
foreach($this->fields() as $field => $fieldAttrs) {
if(isset($fieldAttrs['required']) && true === $fieldAttrs['required']) {
// Required field
if(empty($entity->$field)) {
$this->error($field, "Required field '" . $field . "' was left blank");
}
}
}
// Check for errors
if($this->hasErrors()) {
return false;
} else {
return true;
}
}
/**
* Migrate table structure changes from model to database
*/
public function migrate()
{
return $this->adapter()->migrate($this->datasource(), $this->fields());
}
/**
* Check if a value is empty, excluding 0 (annoying PHP issue)
*
* @param mixed $value
* @return boolean
*/
public function isEmpty($value)
{
return (empty($value) && 0 !== $value && '0' !== $value);
}
/**
* Check if any errors exist
*
* @param string $field OPTIONAL field name
* @return boolean
*/
public function hasErrors($field = null)
{
if(null !== $field) {
return isset($this->_errors[$field]) ? count($this->_errors[$field]) : false;
}
return count($this->_errors);
}
/**
* Get array of error messages
*
* @return array
*/
public function errors($msgs = null)
{
// Return errors for given field
if(is_string($msgs)) {
return isset($this->_errors[$field]) ? $this->_errors[$field] : array();
// Set error messages from given array
} elseif(is_array($msgs)) {
foreach($msgs as $field => $msg) {
$this->error($field, $msg);
}
}
return $this->_errors;
}
/**
* Add an error to error messages array
*
* @param string $field Field name that error message relates to
* @param mixed $msg Error message text - String or array of messages
*/
public function error($field, $msg)
{
if(is_array($msg)) {
// Add array of error messages about field
foreach($msg as $msgx) {
$this->_errors[$field][] = $msgx;
}
} else {
// Add to error array
$this->_errors[$field][] = $msg;
}
}
/**
* Attempt to load class file based on phpDataMapper naming conventions
*/
public static function loadClass($className)
{
$loaded = false;
// If class has already been defined, skip loading
if(class_exists($className, false)) {
$loaded = true;
} else {
// Require phpDataMapper_* files by assumed folder structure (naming convention)
if(strpos($className, "phpDataMapper") !== false) {
$classFile = str_replace("_", "/", $className);
$loaded = require_once(dirname(dirname(__FILE__)) . "/" . $classFile . ".php");
}
}
// Ensure required class was loaded
/*
if(!$loaded) {
throw new Exception(__METHOD__ . " Failed: Unable to load class '" . $className . "'!");
}
*/
return $loaded;
}
/**
* Prints all executed SQL queries - useful for debugging
*/
public function debug($entity = null)
{
echo "<p>Executed " . $this->queryCount() . " queries:</p>";
echo "<pre>\n";
print_r(self::$_queryLog);
echo "</pre>\n";
}
/**
* Get count of all queries that have been executed
*
* @return int
*/
public function queryCount()
{
return count(self::$_queryLog);
}
/**
* Log query
*
* @param string $sql
* @param array $data
*/
public static function logQuery($sql, $data = null)
{
self::$_queryLog[] = array(
'query' => $sql,
'data' => $data
);
}
} |