Browse Source

php/phalcon: Define metadata in model (#5730)

* Use native Phalcon json methods
Use Phalcon to output content

* Define metadeta in model
Ruud Boon 5 years ago
parent
commit
792aa15c08

+ 5 - 12
frameworks/PHP/phalcon/app/controllers/BenchController.php

@@ -14,14 +14,14 @@ class BenchController extends \Phalcon\Mvc\Controller
 
     public function jsonAction()
     {
-        return $this->sendContentAsJson(array(
+        return $this->response->setJsonContent(array(
             'message' => 'Hello, World!'
         ));
     }
 
     public function dbAction()
     {
-        return $this->sendContentAsJson($this->getRandomWorld());
+        return $this->response->setJsonContent($this->getRandomWorld());
     }
 
     public function queriesAction()
@@ -35,7 +35,7 @@ class BenchController extends \Phalcon\Mvc\Controller
             $worlds[] = $this->getRandomWorld();
         }
 
-        return $this->sendContentAsJson($worlds);
+        return $this->response->setJsonContent($worlds);
     }
 
     public function fortunesAction()
@@ -64,7 +64,7 @@ class BenchController extends \Phalcon\Mvc\Controller
             $worlds[] = $world;
         }
 
-        return $this->sendContentAsJson($worlds);
+        return $this->response->setJsonContent($worlds);
     }
 
     public function plaintextAction()
@@ -72,7 +72,7 @@ class BenchController extends \Phalcon\Mvc\Controller
         $this->view->disable();
         $this->response->setContentType('text/plain');
         $this->response->setContent("Hello, World!");
-        $this->response->send();
+        return $this->response;
     }
 
     protected function getRandomWorld()
@@ -103,11 +103,4 @@ class BenchController extends \Phalcon\Mvc\Controller
                 });
         return $fortunes;
     }
-
-    private function sendContentAsJson($content)
-    {
-        $response = new Phalcon\Http\Response(json_encode($content));
-        $response->setHeader("Content-Type", "application/json");
-        return $response;
-    }
 }

+ 63 - 0
frameworks/PHP/phalcon/app/models/Fortunes.php

@@ -1,5 +1,8 @@
 <?php
 
+use Phalcon\Mvc\Model\MetaData;
+use Phalcon\Db\Column;
+
 class Fortunes extends \Phalcon\Mvc\Model
 {
     public $id;
@@ -10,4 +13,64 @@ class Fortunes extends \Phalcon\Mvc\Model
     {
         $this->setSource('Fortune');
     }
+
+    public function metaData()
+    {
+        return [
+            // Every column in the mapped table
+            MetaData::MODELS_ATTRIBUTES => [
+                'id',
+                'message'
+            ],
+
+            // Every column part of the primary key
+            MetaData::MODELS_PRIMARY_KEY => [
+                'id',
+            ],
+
+            // Every column that isn't part of the primary key
+            MetaData::MODELS_NON_PRIMARY_KEY => [
+                'message'
+            ],
+
+            // Every column that doesn't allows null values
+            MetaData::MODELS_NOT_NULL => [
+                'id',
+                'message'
+            ],
+
+            // Every column and their data types
+            MetaData::MODELS_DATA_TYPES => [
+                'id' => Column::TYPE_INTEGER,
+                'message' => Column::TYPE_VARCHAR
+            ],
+
+            // The columns that have numeric data types
+            MetaData::MODELS_DATA_TYPES_NUMERIC => [
+                'id' => true
+            ],
+
+            // The identity column, use boolean false if the model doesn't have
+            // an identity column
+            MetaData::MODELS_IDENTITY_COLUMN => 'id',
+
+            // How every column must be bound/casted
+            MetaData::MODELS_DATA_TYPES_BIND => [
+                'id' => Column::BIND_PARAM_INT,
+                'message' => Column::BIND_PARAM_STR
+            ],
+
+            // Fields that must be ignored from INSERT SQL statements
+            MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => [],
+
+            // Fields that must be ignored from UPDATE SQL statements
+            MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => [],
+
+            // Default values for columns
+            MetaData::MODELS_DEFAULT_VALUES => [],
+
+            // Fields that allow empty strings
+            MetaData::MODELS_EMPTY_STRING_VALUES => [],
+        ];
+    }
 }

+ 64 - 0
frameworks/PHP/phalcon/app/models/Worlds.php

@@ -1,5 +1,8 @@
 <?php
 
+use Phalcon\Mvc\Model\MetaData;
+use Phalcon\Db\Column;
+
 class Worlds extends \Phalcon\Mvc\Model
 {
     public $id;
@@ -10,4 +13,65 @@ class Worlds extends \Phalcon\Mvc\Model
     {
         $this->setSource('World');
     }
+
+    public function metaData()
+    {
+        return [
+            // Every column in the mapped table
+            MetaData::MODELS_ATTRIBUTES => [
+                'id',
+                'randomNumber'
+            ],
+
+            // Every column part of the primary key
+            MetaData::MODELS_PRIMARY_KEY => [
+                'id',
+            ],
+
+            // Every column that isn't part of the primary key
+            MetaData::MODELS_NON_PRIMARY_KEY => [
+                'randomNumber'
+            ],
+
+            // Every column that doesn't allows null values
+            MetaData::MODELS_NOT_NULL => [
+                'id',
+                'randomNumber'
+            ],
+
+            // Every column and their data types
+            MetaData::MODELS_DATA_TYPES => [
+                'id' => Column::TYPE_INTEGER,
+                'randomNumber' => Column::TYPE_INTEGER
+            ],
+
+            // The columns that have numeric data types
+            MetaData::MODELS_DATA_TYPES_NUMERIC => [
+                'id' => true,
+                'randomNumber' => true,
+            ],
+
+            // The identity column, use boolean false if the model doesn't have
+            // an identity column
+            MetaData::MODELS_IDENTITY_COLUMN => 'id',
+
+            // How every column must be bound/casted
+            MetaData::MODELS_DATA_TYPES_BIND => [
+                'id' => Column::BIND_PARAM_INT,
+                'randomNumber' => Column::BIND_PARAM_INT
+            ],
+
+            // Fields that must be ignored from INSERT SQL statements
+            MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => [],
+
+            // Fields that must be ignored from UPDATE SQL statements
+            MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => [],
+
+            // Default values for columns
+            MetaData::MODELS_DEFAULT_VALUES => [],
+
+            // Fields that allow empty strings
+            MetaData::MODELS_EMPTY_STRING_VALUES => [],
+        ];
+    }
 }

+ 1 - 1
frameworks/PHP/phalcon/public/index.php

@@ -95,7 +95,7 @@ try {
     $request = new Phalcon\Http\Request();
     $application = new \Phalcon\Mvc\Application();
     $application->setDI($di);
-    echo $application->handle($request->getURI())->getContent();
+    $application->handle($request->getURI())->send();
 
 } catch(\Phalcon\Exception $e) {
     echo "PhalconException: ", $e->getMessage();