Changeset 241 for trunk/trax/vendor/trax/active_record.php
- Timestamp:
- 08/22/06 03:18:53 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/active_record.php (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/active_record.php
r227 r241 452 452 $this->save_associations[$association_type][] = $value; 453 453 if($association_type == "belongs_to") { 454 $foreign_key = Inflector::singularize($value->table_name)."_id"; 455 $this->$foreign_key = $value->id; 454 $primary_key = $value->primary_keys[0]; 455 $foreign_key = Inflector::singularize($value->table_name)."_".$primary_key; 456 $this->$foreign_key = $value->$primary_key; 456 457 } 457 458 } … … 463 464 } 464 465 465 // Assignment to something else, do it466 // Assignment to something else, do it 466 467 $this->$key = $value; 467 468 } … … 649 650 */ 650 651 private function find_all_has_many($other_table_name, $parameters = null) { 651 652 $additional_conditions = null; 652 653 # Use any passed-in parameters 653 654 if (is_array($parameters)) { … … 704 705 705 706 $foreign_key_value = $this->$this_primary_key; 706 $conditions = is_numeric($foreign_key_value) ?707 $conditions = (is_numeric($foreign_key_value) ? 707 708 "$foreign_key = {$foreign_key_value}" : 708 "$foreign_key = '{$foreign_key_value}'" . $additional_conditions;709 "$foreign_key = '{$foreign_key_value}'") . $additional_conditions; 709 710 } 710 711 … … 722 723 */ 723 724 private function find_one_has_one($other_object_name, $parameters = null) { 724 725 726 $additional_conditions = null; 725 727 # Use any passed-in parameters 726 728 if (is_array($parameters)) { … … 757 759 758 760 $foreign_key_value = $this->$this_primary_key; 759 $conditions = is_numeric($foreign_key_value) ?761 $conditions = (is_numeric($foreign_key_value) ? 760 762 "{$foreign_key} = {$foreign_key_value}" : 761 "{$foreign_key} = '{$foreign_key_value}'" ;763 "{$foreign_key} = '{$foreign_key_value}'") . $additional_conditions; 762 764 763 765 # Get the list of other_class_name objects … … 782 784 private function find_one_belongs_to($other_object_name, $parameters = null) { 783 785 786 $additional_conditions = null; 784 787 # Use any passed-in parameters 785 788 if (is_array($parameters)) { 786 789 //echo "<pre>";print_r($parameters); 787 790 if(@array_key_exists("conditions", $parameters)) { 788 $additional_conditions = $parameters['conditions'];791 $additional_conditions = " AND (".$parameters['conditions'].")"; 789 792 } elseif($parameters[0] != "") { 790 $additional_conditions = $parameters[0];793 $additional_conditions = " AND (".$parameters[0].")"; 791 794 } 792 795 if(@array_key_exists("order", $parameters)) { … … 816 819 817 820 $other_primary_key_value = $this->$foreign_key; 818 $conditions = is_numeric($other_primary_key_value) ?821 $conditions = (is_numeric($other_primary_key_value) ? 819 822 "{$other_primary_key} = {$other_primary_key_value}" : 820 "{$other_primary_key} = '{$other_primary_key_value}'" ;823 "{$other_primary_key} = '{$other_primary_key_value}'") . $additional_conditions; 821 824 822 825 # Get the list of other_class_name objects … … 1129 1132 $offset = null; 1130 1133 $per_page = null; 1134 $select = null; 1131 1135 1132 1136 # this is if they passed in an associative array to emulate … … 1150 1154 $sql = $conditions; 1151 1155 } else { 1156 1157 # If select fields not specified just do a SELECT * 1158 if(is_null($select)) { 1159 $select = "*"; 1160 } 1161 1152 1162 # SQL will be built from specifications in argument 1153 $sql = "SELECT * FROM ".$this->table_name."";1163 $sql = "SELECT {$select} FROM {$this->table_name} "; 1154 1164 1155 1165 # If join specified, include it 1156 1166 if(!is_null($joins)) { 1157 if(substr($joins,0,4) != "LEFT") $sql .= ",";1158 1167 $sql .= " $joins "; 1159 1168 } … … 1219 1228 $object = new $class_name(); 1220 1229 $object->new_record = false; 1230 $objects_key = null; 1221 1231 foreach($row as $field => $value) { 1222 1232 $object->$field = $value; … … 1227 1237 $objects[$objects_key] = $object; 1228 1238 unset($object); 1229 unset($objects_key);1230 1239 } 1231 1240 return $objects; … … 1326 1335 * @throws {@link ActiveRecordError} 1327 1336 */ 1328 function find_first($conditions , $order = null, $limit = null, $joins = null) {1337 function find_first($conditions = null, $order = null, $limit = null, $joins = null) { 1329 1338 if(is_array($conditions)) { 1330 1339 $options = $conditions; … … 1542 1551 } else { 1543 1552 $habtm_result = true; 1544 $this->id = $this->get_insert_id(); 1545 if($this->id > 0) { 1553 $primary_key = $this->primary_keys[0]; 1554 $primary_key_value = $this->get_insert_id(); 1555 $this->$primary_key = $primary_key_value; 1556 if($primary_key_value > 0) { 1546 1557 if($this->auto_save_habtm) { 1547 $habtm_result = $this->add_habtm_records($ this->id);1558 $habtm_result = $this->add_habtm_records($primary_key_value); 1548 1559 } 1549 1560 $this->save_associations(); … … 1578 1589 $updates = $this->get_updates_sql(); 1579 1590 $conditions = $this->get_primary_key_conditions(); 1580 $sql = "UPDATE $this->table_name SET $updates WHERE $conditions"; 1591 $sql = "UPDATE {$this->table_name} SET {$updates} WHERE {$conditions}"; 1592 //echo "update_record:$sql<br>"; 1581 1593 //error_log("update_record: SQL: $sql<br>"); 1582 1594 $result = $this->query($sql); … … 1585 1597 } else { 1586 1598 $habtm_result = true; 1587 if($this->id > 0) { 1599 $primary_key = $this->primary_keys[0]; 1600 $primary_key_value = $this->$primary_key; 1601 if($primary_key_value > 0) { 1588 1602 if($this->auto_save_habtm) { 1589 $habtm_result = $this->update_habtm_records($ this->id);1603 $habtm_result = $this->update_habtm_records($primary_key_value); 1590 1604 } 1591 1605 $this->save_associations(); … … 1625 1639 } 1626 1640 if(is_string($this->has_one)) { 1627 if(preg_match("/\b$association_name /\b", $this->has_one)) {1641 if(preg_match("/\b$association_name\b/", $this->has_one)) { 1628 1642 $type = "has_one"; 1629 1643 } … … 1687 1701 case "has_many": 1688 1702 case "has_one": 1689 $foreign_key = Inflector::singularize($this->table_name)."_id"; 1690 $object->$foreign_key = $this->id; 1691 //echo "fk:$foreign_key = $this->id<br>"; 1703 $primary_key = $this->primary_keys[0]; 1704 $foreign_key = Inflector::singularize($this->table_name)."_".$primary_key; 1705 $object->$foreign_key = $this->$primary_key; 1706 //echo "fk:$foreign_key = ".$this->$primary_key."<br>"; 1692 1707 break; 1693 1708 } … … 1705 1720 */ 1706 1721 function delete($id = null) { 1707 if($this->id > 0 && is_null($id)) { 1708 $id = $this->id; 1709 } 1710 1722 1723 $primary_key_value = null; 1724 $primary_key = $this->primary_keys[0]; 1711 1725 if(is_null($id)) { 1712 $this->errors[] = "No id specified to delete on."; 1726 # Primary key's where clause from already loaded values 1727 $conditions = $this->get_primary_key_conditions(); 1728 $primary_key_value = $this->$primary_key; 1729 } elseif(!is_array($id)) { 1730 $primary_key_value = $id; 1731 $id = is_numeric($id) ? $id : "'".$id."'"; 1732 $conditions = "{$primary_key} = $id"; 1733 } elseif(is_array($id)) { 1734 //$ids = ($this->attribute_is_string($primary_key)) ? "'".implode("','", $id)."'" : implode(',', $id); 1735 //$conditions = "{$primary_key} IN ($ids)"; 1736 } 1737 1738 if(is_null($conditions)) { 1739 $this->errors[] = "No conditions specified to delete on."; 1713 1740 return false; 1714 1741 } 1715 1742 1716 $this->before_delete(); 1717 $result = $this->delete_all( "id IN ($id)");1718 if($this->auto_delete_habtm ) {1743 $this->before_delete(); 1744 $result = $this->delete_all($conditions); 1745 if($this->auto_delete_habtm && $primary_key_value != '') { 1719 1746 if(is_string($this->has_and_belongs_to_many)) { 1720 1747 $habtms = explode(",", $this->has_and_belongs_to_many); 1721 1748 foreach($habtms as $other_table_name) { 1722 $this->delete_all_habtm_records(trim($other_table_name), $ id);1749 $this->delete_all_habtm_records(trim($other_table_name), $primary_key_value); 1723 1750 } 1724 1751 } elseif(is_array($this->has_and_belongs_to_many)) { 1725 1752 foreach($this->has_and_belongs_to_many as $other_table_name => $values) { 1726 $this->delete_all_habtm_records($other_table_name, $ id);1753 $this->delete_all_habtm_records($other_table_name, $primary_key_value); 1727 1754 } 1728 1755 } … … 1778 1805 if(is_array($habtm_array)) { 1779 1806 if(is_string($this->has_and_belongs_to_many)) { 1780 if(preg_match("/ $key/", $this->has_and_belongs_to_many)) {1807 if(preg_match("/\b$key\b/", $this->has_and_belongs_to_many)) { 1781 1808 $this->habtm_attributes[$key] = $habtm_array; 1782 1809 } … … 1969 1996 $this->save_associations[$association_type][] = $value; 1970 1997 if($association_type == "belongs_to") { 1971 $foreign_key = Inflector::singularize($value->table_name)."_id"; 1972 $this->$foreign_key = $value->id; 1998 $primary_key = $value->primary_keys[0]; 1999 $foreign_key = Inflector::singularize($value->table_name)."_".$primary_key; 2000 $this->$foreign_key = $value->$primary_key; 1973 2001 } 1974 2002 } … … 2130 2158 # run through our fields and join them with their values 2131 2159 foreach($attributes as $key => $value) { 2132 if($key && $value&& !in_array($key, $this->primary_keys)) {2160 if($key && ($value || is_numeric($value)) && !in_array($key, $this->primary_keys)) { 2133 2161 $updates[] = "$key = $value"; 2134 2162 }
