来源:蜘蛛抓取(WebSpider)
时间:2016-06-02 06:03
标签:
thinkphp联表数据合并
ThinkPHP3.2.3使用关联模型无法正确插入数据
[问题点数:40分]
ThinkPHP3.2.3使用关联模型无法正确插入数据
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。ThinkPHP处理海量数据分表机制详细代码及说明
应用ThinkPHP内置的分表算法处理百万级用户数据.
数据表:
house_member_0
house_member_1
house_member_2
house_member_3
模型中
class MemberModel extends AdvModel {
protected $partition = array('field'=&'username','type'=&'id','num'=&'4');
public function getDao($data=array()) {
$data = empty($data) ? $_POST : $
$table = $this-&getPartitionTableName($data);
return $this-&table($table);
方法中
class MemberAction extends BaseAction {
public function login() {
if($this-&isPost()) {
$this-&validToken();
$dao = D('Member')-&getDao();
$res = $dao-&where('username = '.$_POST['username'])-&find();
// output 为自定义方法
// $isAjax - bool
$this-&output(false);
$this-&display();
+----------------------------------------------------------
* 得到分表的的数据表名
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $data 操作的数据
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
public function getPartitionTableName($data=array()) {
// 对数据表进行分区
if(isset($data[$this-&partition['field']])) {
$field = $data[$this-&partition['field']];
switch($this-&partition['type']) {
case 'id':
// 按照id范围分表
$step = $this-&partition['expr'];
$seq = floor($field / $step)+1;
case 'year':
// 按照年份分表
if(!is_numeric($field)) {
$field = strtotime($field);
$seq = date('Y',$field)-$this-&partition['expr']+1;
case 'mod':
// 按照id的模数分表
$seq = ($field % $this-&partition['num'])+1;
case 'md5':
// 按照md5的序列分表
$seq = (ord(substr(md5($field),0,1)) % $this-&partition['num'])+1;
default :
if(function_exists($this-&partition['type'])) {
// 支持指定函数哈希
$fun = $this-&partition['type'];
$seq = (ord(substr($fun($field),0,1)) % $this-&partition['num'])+1;
}else{
// 按照字段的首字母的值分表
$seq = (ord($field{0}) % $this-&partition['num'])+1;
return $this-&getTableName().'_'.$
}else{
// 当设置的分表字段不在查询条件或者数据中
// 进行联合查询,必须设定 partition['num']
$tableName = array();
for($i=0;$i&$this-&partition['num'];$i++)
$tableName[] = 'SELECT * FROM '.$this-&getTableName().'_'.$i;
$tableName = '( '.implode(& UNION &,$tableName).') AS '.$this-&
return $tableN
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------该如何使用thinkphp分表呢?
thinkphp分表怎么使用
我预设了10个表 分别是 table_1、table_2、table_3....table_10
在Model中设置了partition:
protected $partition = array(
'field' => 'id',
'type' => 'id',
'expr' => '',
'num' => 10,
也使用了getPartitionTableName()
官方文档说是必须传入当前的数据。然后根据数据分析应该实际操作哪个数据表
而这个传入当前数据是什么数据, 我每次实验的结果都是联合查询!不能找到当前表名!
------解决方法--------------------
你的分表字段是 id (PS:T不错的PHP
Q扣n:,验证:csl)
所以在传给 getPartitionTableName 的数据数组中必须含有关联键为 id 的元素
不过他的算法有点奇特
$data[$this->partition['field']];
switch($this->partition['type']) {
case 'id':
// 按照id范围分表
$this->partition['expr'];
floor($field / $step)+1;
return $this->getTableName().'_'.$
按照你的设置,似乎应是 'type' => 'mod'
case 'mod':
// 按照id的模数分表
($field % $this->partition['num'])+1;
------解决方法--------------------
你用 getPartitionTableName 取得的不就是表名吗?
有了表名不就和不分表时一样操作了吗?
以上是本文关于thinkphp分表如何使用的方法,希望本文对广大php开发者有所帮助,感谢本文。
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'ThinkPHP处理海量数据分表机制详细代码及说明
应用ThinkPHP内置的分表算法处理百万级用户数据. 数据表: house_member_0 house_member_1 house_member_2 house_member_3 模型中 class MemberModel extends AdvModel { protected $partition = array('field'='username','type'='id','num'='4'); public f
   
& 应用ThinkPHP内置的分表算法处理百万级用户数据.
数据表:
house_member_0
house_member_1
house_member_2
house_member_3
模型中
class MemberModel extends AdvModel {
protected $partition =
array('field'=&'username','type'=&'id','num'=&'4');
public function getDao($data=array()) {
$data = empty($data) ? $_POST : $
$table = $this-&getPartitionTableName($data);
return $this-&table($table);
方法中
class MemberAction extends BaseAction {
public function login() {
if($this-&isPost()) {
$this-&validToken();
$dao = D('Member')-&getDao();
$res = $dao-&where('username = '.$_POST['username'])-&find();
// output 为自定义方法
// $isAjax - bool
$this-&output(false);
$this-&display();
+----------------------------------------------------------
* 得到分表的的数据表名
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param array $data 操作的数据
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
public function getPartitionTableName($data=array()) {
// 对数据表进行分区
if(isset($data[$this-&partition['field']])) {
$field = $data[$this-&partition['field']];
switch($this-&partition['type']) {
case 'id':
// 按照id范围分表
$step = $this-&partition['expr'];
$seq = floor($field / $step)+1;
case 'year':
// 按照年份分表
if(!is_numeric($field)) {
$field = strtotime($field);
$seq = date('Y',$field)-$this-&partition['expr']+1;
case 'mod':
// 按照id的模数分表
$seq = ($field % $this-&partition['num'])+1;
case 'md5':
// 按照md5的序列分表
$seq = (ord(substr(md5($field),0,1)) % $this-&partition['num'])+1;
default :
if(function_exists($this-&partition['type'])) {
// 支持指定函数哈希
$fun = $this-&partition['type'];
$seq = (ord(substr($fun($field),0,1)) % $this-&partition['num'])+1;
}else{
// 按照字段的首字母的值分表
$seq = (ord($field{0}) % $this-&partition['num'])+1;
return $this-&getTableName().'_'.$
}else{
// 当设置的分表字段不在查询条件或者数据中
// 进行联合查询,必须设定 partition['num']
$tableName = array();
for($i=0;$i&$this-&partition['num'];$i++)
$tableName[] = 'SELECT * FROM '.$this-&getTableName().'_'.$i;
$tableName = '( '.implode(& UNION &,$tableName).') AS '.$this-&
return $tableN
(责任编辑:lvtao)
本文关键字:
广告赞助商
最新视频推荐
Copyright (C) 2007-, All Rights Reserved 版权所有 . 沪ICP备号
地址:上海徐汇区零陵路585号 爱邦大厦26H座
传真(FAX):021-
电话(Tel):021-
PHP100 Website Powered by PHPCMS. For PHP100. 服务器维护:阿里巴巴-阿里云