PHP code example of lyhiving / mmodel

1. Go to this page and download the library: Download lyhiving/mmodel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lyhiving / mmodel example snippets




//引入autoload.php 
use lyhiving\mmodel\Mcache;
use lyhiving\mmodel\Mmodel;

//如果想使用文件缓存,请确保缓存文件的安全
// $cache = new Mcache('files', ['path' => __DIR__ . '/.cachemeta']);

//使用redis缓存
$cache = new Mcache('redis', [
    'host' => 'localhost', //Redis server
    'port' => 6379, //Redis port
    'password' => 'root', //Redis password
    'database' => 0 //Redis db
]);

//配置参数
$options = [
    // 'driver' => 'mysql',
    'host' => 'localhost',
    // 'port' => '3306',
    'username' => 'root',
    'password' => 'root',
    'dbname' => 'test',
    'prefix' => 'cloud_',
    // 'pconnect' => 1,
    'charset' => 'utf8mb4',
];

$model = new Mmodel($options);
//设置缓存
$model->set_cache($cache);
$model->quick('cms');
$data = $model->select(array('contentid' => 1));
var_dump($data);



//引入autoload.php 

use lyhiving\mmodel\Mcache;
use lyhiving\mmodel\Mmodel;
use Hprose\Http\Server;

//缓存
$cache = new Mcache('redis', [
    'host' => 'localhost',
    'port' => 6379,
    'password' => 'root',
    'database' => 0
]);

//配置
$options = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'port' => '3306',
    'username' => 'root',
    'password' => 'root',
    'dbname' => 'test',
    'prefix' => 'cloud_',
    'pconnect' => 1,
    'charset' => 'utf8mb4',
];

$model = new Mmodel($options);
$model->set_cache($cache); 

$server = new Server();
$server->addInstanceMethods($model->db);
$server->setCrossDomainEnabled();
$server->start();


//引入autoload.php 

use lyhiving\mmodel\Mcache;
use lyhiving\mmodel\Mmodel;

$options = [
    'driver' => 'rpc',
    'url' => 'http://localhost/mmodel/examples/server.php',
    'prefix' =>'cloud_'    //如果有设置前缀,一定要在配置注明
];


$model = new Mmodel($options);
$model->quick('cms');
$data = $model->select(array('contentid' => 1));
var_dump($data);


//引入autoload.php 

use lyhiving\mmodel\Mmodel;

//初始化指定路径
$template = Mmodel::template(['dir' => __DIR__ . '/templates/']);

//赋值方法一
$template->assign('time', time());

//赋值方法二
$template->assign(['time2'=>microtime(true)]);

$template->assign('data',[['id'=>1,'name'=>'name1'],['id'=>2,'name'=>'name2']]);

//选择模板
$template->display('demo');