作者:广佛笑嘻嘻_229 | 来源:互联网 | 2023-08-24 13:51
php教程|php手册thinkphp代码,代码示例,代码参考,php短信,数据库备份代码,令牌验证,去除代码中的空白和注释多层关联php教程-php手册重载或重写getRelat
php教程|php手册
thinkphp代码,代码示例,代码参考,php短信,数据库备份代码,令牌验证,去除代码中的空白和注释多层关联
php教程-php手册
重载或重写 getRelation函数,以获得多层关联,关联参数:array('mapping_name'=>'next_mappingname_or_array', 'normal_mapping_name')
/**
* 获取返回数据的关联记录
* @access protected
* @param mixed $result 返回数据
* @param string|array $name 关联名称
* @param boolean $return 是否返回关联数据本身
* @return array
*/
protected function getRelation(&$result,$name='',$return=false) {
if(!empty($this->_link)) {
foreach($this->_link as $key=>$val) {
$mappingName = !empty($val['mapping_name'])?$val['mapping_name']:$key; // 映射名称
if(empty($name) || true === $name || $mappingName == $name || (is_array($name) && (in_array($mappingName,$name) || array_key_exists($mappingName,$name) ))) {
$mappingType = !empty($val['mapping_type'])?$val['mapping_type']:$val; // 关联类型
$mappingClass = !empty($val['class_name'])?$val['class_name']:$key; // 关联类名
$mappingFields = !empty($val['mapping_fields'])?$val['mapping_fields']:'*'; // 映射字段
$mappingCOndition= !empty($val['condition'])?$val['condition']:'1=1'; // 关联条件
$mappingKey =!empty($val['mapping_key'])? $val['mapping_key'] : $this->getPk(); // 关联键名
if(strtoupper($mappingClass)==strtoupper($this->name)) {
// 自引用关联 获取父键名
$mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id';
}else{
$mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($this->name).'_id'; // 关联外键
}
// 获取关联模型对象
$model = D($mappingClass);
switch($mappingType) {
case HAS_ONE:
$pk = $result[$mappingKey];
$mappingCondition .= " AND {$mappingFk}='{$pk}'";
if (is_array($name) && array_key_exists($mappingName,$name)){
$relatiOnData= $model->relation($name[$mappingName])->where($mappingCondition)->field($mappingFields)->find();
}else{
$relatiOnData= $model->where($mappingCondition)->field($mappingFields)->find();
}
break;
case BELONGS_TO:
if(strtoupper($mappingClass)==strtoupper($this->name)) {
// 自引用关联 获取父键名
$mappingFk = !empty($val['parent_key'])? $val['parent_key'] : 'parent_id';
}else{
$mappingFk = !empty($val['foreign_key'])?$val['foreign_key']:strtolower($model->getModelName()).'_id'; // 关联外键
}
$fk = $result[$mappingFk];
$mappingCondition .= " AND {$model->getPk()}='{$fk}'";
if (is_array($name) && array_key_exists($mappingName,$name)){
$relatiOnData= $model->relation($name[$mappingName])->where($mappingCondition)->field($mappingFields)->find();
}else
$relatiOnData= $model->where($mappingCondition)->field($mappingFields)->find();
break;
case HAS_MANY:
$pk = $result[$mappingKey];
$mappingCondition .= " AND {$mappingFk}='{$pk}'";
$mappingOrder = !empty($val['mapping_order'])?$val['mapping_order']:'';
$mappingLimit = !empty($val['mapping_limit'])?$val['mapping_limit']:'';
// 延时获取关联记录
if (is_array($name) && array_key_exists($mappingName,$name)){
$relatiOnData= $model->relation($name[$mappingName])->where($mappingCondition)->field($mappingFields)->order($mappingOrder)->limit($mappingLimit)->select();
}else
$relatiOnData= $model->where($mappingCondition)->field($mappingFields)->order($mappingOrder)->limit($mappingLimit)->select();
break;
case MANY_TO_MANY:
$pk = $result[$mappingKey];
$mappingCOndition= " {$mappingFk}='{$pk}'";
$mappingOrder = $val['mapping_order'];
$mappingLimit = $val['mapping_limit'];
$mappingRelatiOnFk= $val['relation_foreign_key']?$val['relation_foreign_key']:$model->getModelName().'_id';
$mappingRelatiOnTable= $val['relation_table']?$val['relation_table']:$this->getRelationTableName($model);
$sql = "SELECT b.{$mappingFields} FROM {$mappingRelationTable} AS a, ".$model->getTableName()." AS b WHERE a.{$mappingRelationFk} = b.{$model->getPk()} AND a.{$mappingCondition}";
if(!empty($val['condition'])) {
$sql .= ' AND '.$val['condition'];
}
if(!empty($mappingOrder)) {
$sql .= ' ORDER BY '.$mappingOrder;
}
if(!empty($mappingLimit)) {
$sql .= ' LIMIT '.$mappingLimit;
}
if (is_array($name) && array_key_exists($mappingName,$name)){php源码记账,vscode锁定只读文件,ubuntu系统压缩,tomcat加ssl证书,sqlite数据库和c,爬虫的相关法律风险有哪些,php 联动下拉列表,辽宁seo排名优化,网站源代码文件,html动态模板下载地址lzw
$mappingOrder = !empty($val['mapping_order'])?$val['mapping_order']:'';
$mappingLimit = !empty($val['mapping_limit'])?$val['mapping_limit']:'';
$relatiOnData= $model->relation($name[$mappingName])
->field($model->getTableName().'.'.$mappingFields)
->join($mappingRelationTable." on $mappingRelationTable.$mappingRelatiOnFk= ".$model->getTableName().".".$model->getPk())
->where($mappingRelationTable.".{$mappingFk}='{$pk}'")
->order($mappingOrder)
->limit($mappingLimit)
->select();
}else
$relatiOnData= $this->query($sql);
break;
}
if(!$return){
if(isset($val['as_fields']) && in_array($mappingType,array(HAS_ONE,BELONGS_TO)) ) {
// 支持直接把关联的字段值映射成数据对象中的某个字段
// 仅仅支持HAS_ONE BELONGS_TO
$fields = explode(',',$val['as_fields']);
foreach ($fields as $field){
if(strpos($field,':')) {
list($relationName,$nick) = explode(':',$field);
$result[$nick] = $relationData[$relationName];
}else{
$result[$field] = $relationData[$field];
}
}
}else{
$result[$mappingName] = $relationData;
}
unset($relationData);
}else{
return $relationData;
}
}
}
}
return $result;
}
采集微信群的网站源码,ubuntu经常异常假死,胖胖的爬虫店,php cronrun,seo-992lzw
AD:真正免费,域名+虚机+企业邮箱=0元
.net 积分 源码,vscode不能编译dll,ubuntu安装xsdl,tomcat服务日志文件,SQLite中清空数据表,可以爬虫失信被执行人吗,php 找不到指定的模块,柳州谷歌seo优化方法,网站源码盗取,dw网页设计模板lzw