xorm/xorm

它是PHP中简单的ORM解决方案

1.0.0 2018-01-29 00:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 17:12:16 UTC


README

安装

您可以通过在项目根目录下的终端中运行以下命令来通过Composer安装此包:

composer require xorm/xorm:dev-master

use X as X; // Load class with composer  at the top of the file 
// or 
require '/X.php'; // Load direct without composer at the top of the file 

Composer详细信息

http://rapidsol.blogspot.com/2015/03/download-composerphar.html

数据库设置

// setup($constr, $user, $pass, $debugConfig=0) $debugConfig 1 will show all queries before result
X::setup( 'mysql:host=localhost;dbname=mydb',  'username' ,  'password' ,0 );

设置表以添加/更新/删除

X::manage('users'); 
//$dbc['id'] = 1;
$dbc['title'] = 'test title';
X::save($dbc); //for save and update

通过ID删除记录。

//It will find you primery coulumn auto and delete record. It does't matter primery column is id or bid.
X::delete('users' ,12);

通过自定义查询获取所有记录

$sql = 'SELECT xdata FROM `users` WHERE 1 ';
$rec = X::getAll( $sql );

执行所有查询

$sql = 'SELECT xdata FROM `users` WHERE 1 ';
$rec = X::exec_sql( $sql ); //SELECT, UPDATE, DELETE, ETC..

通过ID获取记录。

//It will find you primery coulumn auto and show record
$rec = X::load('users' ,12 );
X::debug($rec);

通过表名和ID或无ID获取所有相关记录。

//It will return mixed data with auto joins
X::setRecursive(1);
$rec = X::load('users' ,12 );
X::debug($rec);

//It will return all related data in clasified way
X::setRecursive(2);
$rec = X::load('users' ,12 );
X::debug($rec);

$rec = X::load('users' );
X::debug($rec);

分页

//paginate($length = 20, $current_page=5 )
$data = X::paginate(20,5);

清空表

 X::emptyX('users');

删除表

 X::drop('users');

导出CSV

X::download_send_headers("data_CSV_export_" . date("Y-m-d") . ".csv");
echo X::array2csv($data);

xml到数组

$dataArray = X::xmltoArray($xmlstr);
 

完整详情

// setup($constr, $user, $pass, $debugConfig=0) $debugConfig 1 will show all queries before result
X::setup( 'mysql:host=localhost;dbname=st_mysite', 'root', 'm' ,1);

Here is example database structure. I have 2 tables in db1 and 1 table in db2. I have cross joins.

relation detail: db1

CREATE TABLE `albums` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `artist` varchar(100) NOT NULL,
  `title` varchar(100) NOT NULL,
  `user_id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `customer_id` (`customer_id`),
  CONSTRAINT `FK_AlbumsCustomers` FOREIGN KEY (`customer_id`) REFERENCES `db2`.`customers` (`id`),
  CONSTRAINT `FK_AlbumsUsers` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=latin1

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `data1` varchar(111) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1

-----------------------------------------------------
**db2.** 
CREATE TABLE `customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `address` varchar(50) NOT NULL,
  `city` varchar(50) NOT NULL,
  `state` varchar(50) DEFAULT NULL,
  `zip` varchar(15) DEFAULT NULL,
  `country` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=169 DEFAULT CHARSET=latin1.

/*
CREATE TABLE IF NOT EXISTS `AppVersion` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `ClassName` enum('AppVersion') DEFAULT 'AppVersion',
  `LastEdited` datetime DEFAULT NULL,
  `Created` datetime DEFAULT NULL,
  `Version` varchar(12) DEFAULT NULL,
  `Mandatory` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `Link` varchar(255) DEFAULT NULL,
  `Platform` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `ClassName` (`ClassName`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=470 ;

--
-- Dumping data for table `AppVersion`
--

INSERT INTO `AppVersion` (`ID`, `ClassName`, `LastEdited`, `Created`, `Version`, `Mandatory`, `Link`, `Platform`) VALUES
(3, 'AppVersion', '2016-01-20 19:47:09', '2016-01-20 19:47:09', '1.0.1', 1, 'http://apple.com', 'ios'),
(4, 'AppVersion', '2016-01-20 19:47:09', '2016-01-20 19:47:09', '1.0.1', 1, 'http://google.com', 'android'),
(7, 'AppVersion', NULL, NULL, '1.0.2', 1, 'http://apple.com', 'ios'),
(8, 'AppVersion', NULL, NULL, '1.0.6', 0, 'http://apple.com', 'ios'),
(9, 'AppVersion', NULL, NULL, '1.0.10', 1, 'http://apple.com', 'ios');
*/

X::manage( 'AppVersion');

$data = X::select('Mandatory, Platform'); // PUT FIELD STATMENT LIKE FIEL1 AS F, FIELD2 AS B, FIELD3 

$data = X::where('Version','like','1.0.2'); //WHERE WORK WITH AND OPERATOR

$data = X::where('Platform','=','ios');

$data = X::groupBy('Mandatory');

$data = X::limit(1,5);

$data = X::where('Mandatory','=',0);

$data = X::orderBy('Mandatory','DESC');

//paginate($length = 10, $current_page=1 )

$data = X::paginate(20,5); 

//$data = X::whereOr('Mandatory','=',0); //It IS FOR OR CONDITION

//$data = X::where(1); //IT WILL RETURN WHOLE TABLE DATA


//$data = X::where(); //IT WILL RETURN WHOLE TABLE DATA

//https://packagist.org.cn/packages/xorm/xorm