auzadventure/yii2-jsondb

Extension to Add and Save a flat file json

Installs: 2 004

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

pkg:composer/auzadventure/yii2-jsondb

0.1.2 2018-10-31 00:25 UTC

This package is not auto-updated.

Last update: 2026-01-01 11:52:58 UTC


README

Extension to Add and Save a flat file json

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist auzadventure/yii2-jsondb "*"

or add

"auzadventure/yii2-jsondb": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

<?php 
$json_path = Url::to('@app/path/json_file.json')

$jsonDB = \auzadventure\jsondb\JsonDB($json_path); 

It will save a record in the json format with a lastID 

{
    "data": {
        "1": {
            "date": "today",
            "msg": "milk is good"
        }
    },
    "conf": {
        "lastID": 1
    }
}

Insert

$jsonDB->insert($array)

FindAll

$jsonDB->findAll()

Find Field = Val


# Find 

->findOne($id) 

Sample Model to extend

<?php 

namespace app\models;

use Yii;
use yii\base\Model;
use auzadventure\jsondb\JsonDB;
use yii\helpers\Url;


class Ads extends Model {
	
	public $title;
	public $url;
	public $blurp;
	
	public $filepath;
	public $jsondb; 
	public $id; 
	
	public function rules()
    {
        return [
            // name, email, subject and body are required
            [['title', 'url', 'blurp'], 'required'],
            
        ];
    }
	
	
	public function __construct() {
		$this->filepath = $this->getFilePath();
		$this->jsondb = new JsonDB($this->filepath);
	}
	
	
	public function getFilePath() {
		return Url::to('@app/data/ads.json');
	}
	
	
	public function findAll() {
		return $this->jsondb->findAll()->data;
	}
	
	public function save() {
		
		$file = self::getFilePath();
		
		$jsondb = new JsonDB($file);
		$jsondb->insert(['title'=>$this->title,
						 'url'=>$this->url,
						 'blurp'=>$this->blurp
						]);
			
	}
	
	public function delete($id) {
		return $this->jsondb->deleteByID($id);
	}

}