Browse Source

* Make Router::url() static
* Fix arguments for Request->json()
* Formatting
* require_once/use Router in Response
* Clean up ORM creation

Nathaniel van Diepen 7 years ago
parent
commit
ed56a58fdd
5 changed files with 17 additions and 13 deletions
  1. 1 1
      App/router.class.php
  2. 2 2
      Http/request.class.php
  3. 3 2
      Http/response.class.php
  4. 1 1
      PDO/table.class.php
  5. 10 7
      orm.abstract.class.php

+ 1 - 1
App/router.class.php

@@ -45,7 +45,7 @@
 		public function base(string $base){
 			$this->_base = $base;
 		}
-		public function url(string $url){
+		public static function url(string $url){
 			return preg_replace('/(\/+)/','/',$url);
 		}
 		public function prefix(string $prefix, callable $fn){

+ 2 - 2
Http/request.class.php

@@ -36,8 +36,8 @@
 		public function text(){
 			return $this->body;
 		}
-		public function json(bool $pretty = true){
-			return json_decode($this->body, $pretty);
+		public function json(bool $assoc = true){
+			return json_decode($this->body, $assoc);
 		}
 		public function xml(string $classname = 'SimpleXMLElement', int $options, string $ns = '', bool $is_prefix = false){
 			return simplexml_load_string($this->body, $classname, $options, $ns, $is_prefix);

+ 3 - 2
Http/response.class.php

@@ -1,7 +1,8 @@
 <?php
 	namespace Juju\Http;
 	require_once(realpath(dirname(__DIR__).'/events.trait.php'));
-	use \Juju\Events;
+	require_once(realpath(dirname(__DIR__).'/App/router.class.php'));
+	use \Juju\{App\Router, Events};
 
 	class Response {
 		use Events;
@@ -74,7 +75,7 @@
 		}
 		public function redirect(string $url){
 			$this->fire('redirect', $url);
-			$this->header('Location',Router::url($url));
+			$this->header('Location', Router::url($url));
 			return $this;
 		}
 		public function end(string $chunk=''){

+ 1 - 1
PDO/table.class.php

@@ -358,7 +358,7 @@
 			return $this->exists ? $this->pdo->exec("insert into `{$this->name}` {$this->pdo->stringSet($data)}") : 0;
 		}
 		public function update(array $data, array $filter = null){
-			return $this->exists ? $this->pdo->exec("update `{$this->name} {$this->pdo->stringSet($data)} {$this->pdo->stringFilter($filter)}`") : 0;
+			return $this->exists ? $this->pdo->exec("update `{$this->name}` {$this->pdo->stringSet($data)} {$this->pdo->stringFilter($filter)}") : 0;
 		}
 		public function delete(array $filter = null){
 			return $this->exists ? $this->pdo->exec("delete from `{$this->name}` {$this->pdo->stringFilter($filter)}") : 0;

+ 10 - 7
orm.abstract.class.php

@@ -72,7 +72,7 @@
 					throw new \Exception('Instance already cached');
 				}
 				foreach($idOrData as $key => $val){
-					$this->_data[$key] = $val;
+					$this->set($key, $val);
 				}
 			}else{
 				if(self::cached($idOrData)){
@@ -107,7 +107,8 @@
 				case 'has_one':case 'has_many':case 'belongs_to':
 					return self::$aliases[$this->name][$name];
 				case 'id':
-					return $this->get(static::primary_key());
+					$pk = static::primary_key();
+					return $this->has($pk) ? $this->get($pk) : null;
 				default:
 					throw new \Exception('Unknown property '.$name);
 			}
@@ -311,13 +312,15 @@
 						$data[$key] = null;
 					}
 				}
-				if(!is_null($this->id) && !in_array(static::primary_key(), $this->_changed)){
-					static::table()->update($data, [
-						static::primary_key() => $this->id
+				$pk = static::primary_key();
+				$table = static::table();
+				if($this->has($pk) && !is_null($this->id) && !in_array($pk, $this->_changed)){
+					$table->update($data, [
+						$pk => $this->id
 					]);
 				}else{
-					static::table()->insert($data);
-					$this->_data[static::primary_key()] = self::$pdo->lastInsertId();
+					$table->insert($data);
+					$this->_data[$pk] = self::$pdo->lastInsertId();
 				}
 				foreach($this->_related as $related){
 					$related->save();