auzadventure/yii2-jsondb

用于添加和保存平面文件json的扩展

安装次数: 1,215

依赖项: 0

建议者: 0

安全: 0

星级: 1

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

0.1.2 2018-10-31 00:25 UTC

This package is not auto-updated.

Last update: 2024-09-26 06:10:19 UTC


README

用于添加和保存平面文件json的扩展

安装

安装此扩展的首选方式是通过 Composer

运行以下命令之一

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

"auzadventure/yii2-jsondb": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

使用方法

扩展安装完成后,只需在您的代码中通过以下方式使用它:

<?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
    }
}

插入

$jsonDB->insert($array)

查找所有

$jsonDB->findAll()

查找字段 = 值


# Find 

->findOne($id) 

示例模型扩展

<?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);
	}

}