勾股OA5.0二次开发常用代码片段
1、读取附件数据
if($info['file_ids'] !=''){
$file_array = Db::name('File')->where('id','in',$info['file_ids'])->select()->toArray();
$info['file_array'] = $file_array;
}
else{
$info['file_array'] = [];
}
2、页面展示附件
{notempty name="$detail.file_ids"}
<tr>
<td class="layui-td-gray">相关附件</td>
<td colspan="5">
<div class="layui-row">
{volist name="$detail.file_array" id="vo"}
<div class="layui-col-md4" id="uploadImg{$vo.id}">{:file_card($vo,'view')}</div>
{/volist}
</div>
</td>
</tr>
{/notempty}
3、递归查询分类表的,读取某个分类的父分类的id的集合,有两种方法,一种是递归法,一种使用while方法。
//递归法
public function getParentIds($categoryId)
{
// 查询当前分类信息
$category = Category::where('id', $categoryId)->find();
if (!$category) {
return []; // 如果分类不存在,返回空数组
}
// 如果当前分类有父分类,则递归查询
if ($category['parent_id'] > 0) {
$parentIds = $this->getParentIds($category['parent_id']);
$parentIds[] = $category['parent_id']; // 将父分类ID加入结果
return $parentIds;
}
// 没有父分类时返回空数组
return [];
}
//while方法
public function getParentIdsByLoop($categoryId)
{
$parentIds = [];
while ($categoryId > 0) {
$category = Category::where('id', $categoryId)->find();
if ($category && $category['parent_id'] > 0) {
$parentIds[] = $category['parent_id'];
$categoryId = $category['parent_id'];
} else {
break;
}
}
return $parentIds;
}