```
<?php
namespace app\agent\model;
use think\Model;
class AgentCategoryModel extends Model
{
/**
* 添加文章分类
* @param $data
* @return bool
*/
public function addCategory($data)
{
$result = true;
self::startTrans();
try {
$this->allowField(true)->save($data);
$id = $this->id;
if (empty($data['parent_id'])) {
$this->where( ['id' => $id])->update(['path' => '0-' . $id]);
} else {
$parentPath = $this->where('id', intval($data['parent_id']))->value('path');
$this->where( ['id' => $id])->update(['path' => "$parentPath-$id"]);
}
self::commit();
} catch (\Exception $e) {
self::rollback();
$result = false;
}
return $result;
}
}
```