PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 13, 2022

[FIXED] CakePHP3 - Standalone model for arrays/JSON data

 February 13, 2022     cakephp, cakephp-3.0, json, php     No comments   

Issue

I retrieve JSON data from a JSON file in my CakePHP project. The application receives mainly data through JSON.

I currently have a Model (Table Model) in CakePHP, as following:

namespace App\Model\Table;


use Cake\ORM\Table;
use Cake\Filesystem\File;
use Cake\Validation\Validator;

 class DataTable extends Table
 {

        public function getJson()
        {
            $file = new File('data.json');
            $json = $file->read(true, 'r');
            $jsonstd = json_decode($json);

            // remove STD classes
            $json2array = json_decode(json_encode($jsonstd), true);

            // return data
            return $json2array;
        }

}

As you can see, we still use the ORM of CakePHP while we are only retrieving data from a JSON file. How can I make a standalone CakePHP Model which is based on retrieving JSON data (without using CakePHP's ORM which purpose has database connection).


Solution

Model is a layer, a concept and doesn't equal to a table. Obviously, you're not working with a relational databases table here you don't need it at all.

Read about the following topics, you'll find plenty of info about it on Google

  • Abstraction in OOP
  • MVC Design Pattern
  • Separation of Concerns (SoC)

They all play into what you try to do and having understood them will make you a better programmer because your code will become better to maintain.


You can place any kind of class anywhere - technically. Good practice is good abstraction where it makes sense and SoC.

What you want to do is business logic and data manipulation which goes into the model layer. Pay attention to the namespace I've put it in. You could put it into App\Model\Json as well or wherever else you want it. I usually group my classes in namespaces / folders in a meaningful way.

namespace App\Model;

use Cake\Filesystem\File;

 class JsonData
 {
        public static function get($file)
        {
            $file = new File($file);
            $json = $file->read(true, 'r');
            $jsonstd = json_decode($json);

            // remove STD classes
            $json2array = json_decode(json_encode($jsonstd), true);

            // return data
            return $json2array;
        }
}

// Import the class and use it
$json = JsonData::get($file);


Answered By - floriank
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing