Browse Source

* Update ORM to handle binding with just a string or secure string and automatically creating the SQL instance

Nathaniel van Diepen 7 years ago
parent
commit
5b1bf7599d
1 changed files with 12 additions and 3 deletions
  1. 12 3
      orm.abstract.class.php

+ 12 - 3
orm.abstract.class.php

@@ -2,7 +2,9 @@
 	namespace Juju {
 		require_once('sql.class.php');
 		require_once('ORM/relationship.class.php');
+		require_once('Data/securestring.class.php');
 		use \Juju\ORM\Relationship as Relationship;
+		use \Juju\Data\SecureString;
 
 		abstract class ORM implements \ArrayAccess, \JsonSerializable {
 			// Model definition
@@ -166,9 +168,16 @@
 			public static function foreign_key_suffix(){
 				return static::$foreign_key_suffix;
 			}
-			public static function bind(SQL $sql){
-				self::$sql = $sql;
-				// @todo handle updating live instances
+			public static function bind($sql){
+				if(is_string($sql) || $sql instanceof SecureString){
+					$sql = SQL::FromDSN("{$sql}");
+				}
+				if($sql instanceof SQL){
+					self::$sql = $sql;
+					// @todo handle updating live instances
+				}else{
+					throw new \Exception("Invalid argument. Must pass a DSN string or an SQL instance");
+				}
 			}
 			public static function query(...$args){
 				return self::$sql->query(...$args);