private function add($pid, $slug, $sort, $sortpath) { $level = 0; if ($pid != 0) { $pcate = Category::find($pid); $level = $pcate->levels; } //找出父类别 if ($level == 0) { $cateid = 10; } if ($level == 1) { $cateid = $pid * 100; } //第二级 if ($level == 2) { $cateid = $pid * 1000; } //第三级 if ($level == 3) { $cateid = $pid * 1000; } //第三级 //找出当前的编号 $findid = Category::where("levels", "=", $level + 1)->where("id", ">=", $cateid)->max("id"); echo "the find id is:" . $findid . "<br />"; if ($findid) { $cateid = $findid + 1; } //在当前的编号上加1 echo "the cateid is:" . $cateid . "<br />"; $pinyin = CUtf8_PY::encode($slug); $tpcate = Category::where("slug", "=", $slug)->count(); if ($tpcate >= 1) { return; } $cate = new Category(); $cate->id = $cateid; $cate->parent_id = $pid; $cate->name = $pinyin; $cate->slug = $slug; $cate->sort = $sort; $cate->levels = $level + 1; $cate->sortpath = $sortpath . $cateid . ","; $cate->save(); }
public function eidtCategory($cid, $pid, $slug, $sort) { $level = 0; $sortpath = "0,"; if ($pid != 0) { $pcate = Category::find($pid); $level = $pcate->levels; $sortpath = $pcate->sortpath; } //找出父类别 if ($level == 0) { $cateid = 10; } if ($level == 1) { $cateid = $pid * 100; } //第二级 if ($level == 2) { $cateid = $pid * 1000; } //第三级 if ($level == 3) { $cateid = $pid * 1000; } //第三级 //找出当前的编号 $findid = Category::where("levels", "=", $level + 1)->where("id", ">=", $cateid)->max("id"); //echo "the find id is:".$findid."<br />"; if ($findid) { $cateid = $findid + 1; } //在当前的编号上加1 // echo "the cateid is:".$cateid."<br />"; $pinyin = CUtf8_PY::encode($slug); $cate = new Category(); $cate->id = $cateid; $cate->parent_id = $pid; $cate->name = $pinyin; $cate->slug = $slug; $cate->sort = $sort; $cate->levels = $level + 1; $cate->sortpath = $sortpath . $cateid . ","; $cate->save(); $oldcate = Category::find($cid); $allcateneedmodify = Category::where("sortpath", 'like', $oldcate->sortpath)->select(); foreach ($allcateneedmodify as $key => $val) { $newsortpath = str_replace($val->oldpath, $cate->sortpath, $val->sortpath); $val::update(['sortpath' => $newsortpath]); } // $uinfos=array( // "sortpath"=>"replace(sortpath,'".$oldcate->sortpath."','".$cate->sortpath // ."')" // ); // $cate::where("sortpath",'like',$oldcate->sortpath)->update($uinfos); // DB::statement("update categories set sortpath=replace(sortpath,'".$oldcate->sortpath."','".$cate->sortpath // ."') where sortpath like '".$oldcate->sortpath."'"); // $cate::statement("update categories set sortpath="); // $cate::where("sortpath",'like','%,'.$cid.',%')->update($uinfos); $cate::where("id", '=', $cid)->delete(); }