PHP code example of jijihohococo / ichi-orm

1. Go to this page and download the library: Download jijihohococo/ichi-orm 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/ */

    

jijihohococo / ichi-orm example snippets


composer 


use JiJiHoHoCoCo\IchiORM\Database\Connector;

$connector = new Connector;
$connector->createConnection('mysql',[
	'dbname' => 'database_name',
	'charset' => 'utf8mb4',
	'collation' => 'utf8mb4_unicode_ci',
	'host' => '127.0.0.1',
	'user_name' => 'user_name',
	'user_password' => 'user_password'
]);

$connector->addConnection('new_mysql_connection')->createConnection('new_mysql_connection',[
	'driver' => 'mysql',
	'dbname' => 'database_name',
	'charset' => 'utf8mb4',
	'collation' => 'utf8mb4_unicode_ci',
	'host' => '127.0.0.1',
	'user_name' => 'user_name',
	'user_password' => 'user_password'
]);

$connector->selectConnection('mysql');

#!/usr/bin/env php


\Command\ModelCommand;


$modelCommand = new ModelCommand;
$modelCommand->run(__DIR__,$argv);



php ichi make:model Blog



$modelCommand = new ModelCommand;
$modelCommand->setPath('new_app/Models');
$modelCommand->run(__DIR__,$argv);


namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;

class Blog extends Model{
	
	protected function getTable(){
		return "order_item_details";
	}
}

namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;

class Blog extends Model{
	
	protected function getID(){
		return "blog_id";
	}
}

namespace App\Models;
use JiJiHoHoCoCo\IchiORM\Database\Model;
class Blog extends Model{

	publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at;

}


Blog::create([
	'author_id' => 1,
	'content' => 'Content'
]);




protected function autoIncrementId(){
		return FALSE;
}



Blog::create([
	'id' => 1 ,
	'author_id' => 1,
	'content' => 'Content'
]);


use App\Models\Blog;

$contents = $_REQUEST['content'];
$insertBlogs = [];
foreach ($contents as $key => $content) {
	$insertBlogs[] = [
		'content' => $content,
		'author_id' => $_REQUEST['author_id'][$key]
	];
}

Blog::insert($insertBlogs);

Blog::find(1);

Blog::findBy('content','Content');

namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;

class Blog extends Model{

	publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at;

	public function author(){
		return $this->refersTo('App\Models\Author','author_id');
	}
}

namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;

class Blog extends Model{

	publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at;

	public function author(){
		return $this->refersTo('App\Models\Author','author_id','authorID');
	}
}

use App\Models\Blog;

$blogObject = Blog::find(1);
$authorObject = $blogObject->author();
$authorId = $authorObject->id;

namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;

class Author extends Model{
 	
 	publilc $id,$name,$created_at,$updated_at,$deleted_at;

 	public function blogs(){
 		return $this->refersMany('App\Models\Blog','author_id')->get();
 	}

}

namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;

class Author extends Model{
 	
 	publilc $authorID,$name,$created_at,$updated_at,$deleted_at;

 	public function blogs(){
 		return $this->refersMany('App\Models\Blog','author_id','authorID')->get();
 	}

}

return $this->refersMany('App\Models\Blog','author_id','authorID')->latest()->get();

use App\Models\Author;

$authorObject = Author::find(1);
$blogs=$authorObject->blogs();

Blog::find(1)->update([
	'content' => 'New Content'
]);

use App\Models\Blog;

$blogs = Blog::get();
$updateBlogs = [];

foreach($blogs as $key => $blog){

	$updateBlogs[] = [
		'content' => $_REQUEST['content'][$key],
		'author_id' => $_REQUEST['author_id'][$key],
	];
}

Blog::bulkUpdate($updateBlogs);

Blog::find(1)->delete();

Blog::find(1)->restore();

Blog::find(1)->forceDelete();

Blog::select(['id'])

Blog::select(['blogs.id'])

Blog::select(['id','content'])

Blog::select(['blogs.id','blogs.content'])

$blogs = Blog::select(['id','content'])->get();

foreach($blogs as $blog){
	echo $blog->id . '<br>';
	echo $blog->author()->name . '<br>';
}

Blog::get();

$blogs = Blog::select(['id','content'])->toArray();

foreach($blogs as $blog){
	echo $blog['id'] . '<br>';
}

Blog::toArray();

Blog::withTrashed()->select(['id','content'])->get();

Blog::withTrashed()->select(['id','content'])->toArray();

Blog::withTrashed()->get();

Blog::withTrashed()->toArray();

Blog::limit(1)->get();

Blog::limit(1)->toArray();

Blog::whereIn('id',function($query){
	return $query->select(['id'])->limit(1)->get();
})->get();

Blog::whereIn('id',function($query){
	return $query->select(['id'])->limit(1)->get();
})->toArray();

Blog::where('id',1)->get();

Blog::where('id','=',1)->get();

Blog::where('content','like','%Content%')->get();

Blog::where('id',1)->orWhere('content','Content')->get();

Blog::where('id',1)->orWhere('content','=','Content')->get();

Blog::where('id',1)->orWhere('content','like','%Content%')->get();

Blog::whereIn('id',[1,2])->get();

Blog::whereNotIn('id',[1,2])->get();

Author::innerJoin('blogs','authors.id','=','blogs.author_id')
->select(['authors.*','blogs.id AS blog_id'])
->get();

Blog::where('id',function($query){
	return $query->from('App\Models\Author')
	->innerJoin('blogs','authors.id','=','blogs.author_id')
	->select(['blogs.id AS blog_id'])
	->get();
})->get();

Author::leftJoin('blogs','authors.id','=','blogs.author_id')
->select(['authors.*','blogs.id AS blog_id'])
->get();

Blog::where('id',function($query){
	return $query->from('App\Models\Author')
	->leftJoin('blogs','authors.id','=','blogs.author_id')
	->select(['blogs.id AS blog_id'])
	->get();
})->get();

Author::rightJoin('blogs','authors.id','=','blogs.author_id')
->select(['authors.*','blogs.id AS blog_id'])
->get();

Blog::where('id',function($query){
	return $query->from('App\Models\Author')
	->rightJoin('blogs','authors.id','=','blogs.author_id')
	->select(['blogs.id AS blog_id'])
	->get();
})->get();

Blog::where('id',1)->union(function(){
	return Blog::where('id',2)->toSQL()->get();
})->get();

Blog::whereIn('id', function($query) {
	return $query->select(['id'])->where('id',1)->union(function($query){
		return $query->select(['id'])->where('id',2)->get();
	})->get();
} )->get();

[
	'current_page' => 'current page number',
	'data' => 'paginated data',
	'first_page_url' => 'first page url',
	'from' => 'The number of paginated data which starts to show in current page',
	'last_page' => 'The last page number',
	'last_page_url' => 'The last page url',
	'next_page_url' => 'The next page url',
	'path' => 'the current page url',
	'per_page' => 'The number of how many data will be shown per page',
	'prev_page_url' => 'The previous page url',
	'to' => 'The number of paginated data which is last data to show in current page',
	'total' => 'The total number of paginated data in current page'
]

$paginatedBlogs = Blog::whereIn('id',[1,2,3,4,5])->paginate();

$paginatedBlogs = Blog::whereIn('id',[1,2,3,4,5])->paginate(12);

foreach($paginatedBlogs['data'] as $blog){
	echo $blog->id.'<br>';
	echo $blog->author()->name . '<br>';
}

(new  JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs);

(new JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs,'#000000');

use JiJiHoHoCoCo\IchiORM\Pagination\ArrayPagination;

$blogs = ['Blog One','Blog Two','Blog Three','Blog Four','Blog Five'];

$paginatedBlogs = (new ArrayPagination)->paginate($blogs);


use JiJiHoHoCoCo\IchiORM\Pagination\ArrayPagination;

$blogs = [
			[
				'content' => 'Blog One',
				'author_name' => 'John Doe'
			],
			[
				'content' => 'Blog Two',
				'author_name' => 'Joe Blow'
			],
			[
				'content' => 'Blog Three',
				'author_name' => 'Everyman'
			],
			[
				'content' => 'Blog Four',
				'author_name' => 'John Doe'
			],
			[
				'content' => 'Blog Five',
				'author_name' => 'John Doe'
			]
];

$paginatedBlogs = (new ArrayPagination)->paginate($blogs);


$paginatedBlogs = (new ArrayPagination)->paginate($blogs,2);

(new  JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs);

(new JiJiHoHoCoCo\IchiORM\UI\Pagination)->paginate($paginatedBlogs,'#000000');

Blog::whereIn('author_id',function($query){
return $query->select(['id'])->where('id',1)->get();
})->get();

Blog::whereIn('author_id',function($query){
	return $query->from('App\Models\Author')
	->select(['id'])
	->where('id',1)
	->get();
})->get();

Blog::select(['id','author_id'])
->addSelect(['autor_name' => function($query){
	return $query->from(['App\Models\Author'])
	->whereColumn('authors.id','blogs.author_id')
	->limit(1)
	->get();
}])->get();

Blog::addOnlySelect(['autor_name' => function($query){
	return $query->from(['App\Models\Author'])
	->whereColumn('authors.id','blogs.author_id')
	->limit(1)
	->get();
}])->get();

$pdo=connectPDO();


use JiJiHoHoCoCo\IchiORM\Database\Connector;

$pdo=Connector::getInstance()->executeConnect('new_mysql_connection');


namespace App\Models;

use JiJiHoHoCoCo\IchiORM\Database\Model;
use JiJiHoHoCoCo\IchiORM\Database\Connector;
class Author extends Model{

	protected function connectDatabase(){
		return Connector::getInstance()->executeConnect('new_mysql_connection');
	}
}

return jsonResponse([
	'blogs' => Blog::get()
]);

return jsonResponse([
	'blogs' => Blog::get()
],202);

namespace App\Resources;

use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection;

class BlogResourceCollection extends ResourceCollection{
	
	public function getSelectedResource($data){
		return [
			'id' => $data->id,
			'author_id' => $data->author_id,
			'content' => $data->content,
			'created_at' => $data->created_at,
			'updated_at' => $data->updated_at
		];
	}
} 


php ichi make:resource BlogResourceCollection



$modelCommand = new ModelCommand;
$modelCommand->setResourcePath('new_app/Resources');
$modelCommand->run(__DIR__,$argv);


return jsonResponse([
	'blogs' => (new BlogResourceCollection)->collection( Blog::get() ) 
]);

return jsonResponse([
	'blog' => (new BlogResourceCollection)->singleCollection( Blog::find(1) )
]);

namespace App\Resources;

use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection;

class BlogResourceCollection extends ResourceCollection{
	
	public function getSelectedResource($data){
		return [
			'id' => $data->id,
			'author' => $data->author(),
			'content' => $data->content,
			'created_at' => $data->created_at,
			'updated_at' => $data->updated_at
		];
	}
}

namespace App\Resources;

use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection;
use App\Resources\AuthorResourceCollection;

class BlogResourceCollection extends ResourceCollection{
	
	public function getSelectedResource($data){
		return [
			'id' => $data->id,
			'author_id' => $data->author_id,
			'author' => (new AuthorResourceCollection)->singleCollection( $data->author() )  ,
			'content' => $data->content,
			'created_at' => $data->created_at,
			'updated_at' => $data->updated_at
		];
	}
}

namespace App\Resources\AuthorResourceCollection;

use JiJiHoHoCoCo\IchiORM\Resource\ResourceCollection;

class AuthorResourceCollection extends ResourceCollection{

	public function getSelectedResource($data){
		return [
			'id' => $data->id,
			'name' => $data->name
		];
	}

}

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;
use Redis;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
CacheModel::setCacheObject($redis);

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;
use Memcached;
$memcached = new Memcached();
$memcached->addServer('127.0.0.1',11211);
CacheModel::setCacheObject($memcached);

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;
use App\Models\Blog;

$blogs = CacheModel::remember('blogs',function(){
		 	return  Blog::whereIn('author_id',[1,2,3])->get();
		 },100);

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;

CacheModel::remove('blogs');

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;

$blogs = CacheModel::save('blogs',function(){
		 	return  Blog::whereIn('author_id',[1,2,3])->get();
		 },100);

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;

$cachedBlogs = CacheModel::get('blogs');


use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;

$redisObject = CacheModel::getRedis();

use JiJiHoHoCoCo\IchiORM\Cache\CacheModel;

$memcachedObject = CacheModel::getMemcached();

namespace App\Observers;

use JiJiHoHoCoCo\IchiORM\Observer\ModelObserver;
use App\Models\Blog;
class BlogObserver implements ModelObserver{

	public function create($blog){
		
	}

	public function update($blog){
		
	}

	public function delete($blog){

	}

	public function restore($blog){

	}

	public function forceDelete($blog){

	}

}



php ichi make:observer BlogObserver



$modelCommand = new ModelCommand;
$modelCommand->setObserverPath('new_app/Observers');
$modelCommand->run(__DIR__,$argv);


use App\Models\Blog;
use App\Observers\BlogObserver;

Blog::observe(new BlogObserver);

use App\Models\Blog;
use App\Observers\{BlogObserver,BlogDataObserver};

Blog::observe(new BlogObserver);
Blog::observe(new BlogDataObserver);

namespace App\Models;
use JiJiHoHoCoCo\IchiORM\Database\Model;
class Blog extends Model{

	publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at;

	public function customFunction(){
		/*----- your business logic -----*/
		
		//--- Example to pass one parameter into observer function ---//
		$currentObject=$this;
		self::$observerSubject->use(get_class($this),'customFunction',$currentObject);
	}

}


namespace App\Observers;

use JiJiHoHoCoCo\IchiORM\Observer\ModelObserver;
use App\Models\Blog;
class BlogObserver implements ModelObserver{

	public function customFunction($blog){

	}
}

namespace App\Models;
use JiJiHoHoCoCo\IchiORM\Database\Model;
class Blog extends Model{

	publilc $id,$author_id,$content,$created_at,$updated_at,$deleted_at;

	public function author(){
		return $this->refersTo('App\Models\Author','author_id');
	}

	public function customFunction(){
		/*----- your business logic -----*/
		
		//--- Example to pass multiple parameter into observer function ---//
		$currentObject=$this;
		$author=$this->author();
		self::$observerSubject->use(get_class($this),'customFunction',[$currentObject,$author]);
	}

}


namespace App\Observers;

use JiJiHoHoCoCo\IchiORM\Observer\ModelObserver;
use App\Models\{Blog,Author};
class BlogObserver implements ModelObserver{

	public function customFunction($blog,$author){
	
	}
}