49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
||
|
||
namespace app\admin\service;
|
||
|
||
use app\common\model\Customers as CustomersModel;
|
||
use app\common\model\CustomerEmployees;
|
||
|
||
class CustomersService
|
||
{
|
||
public function selectCustomersWithEmployees()
|
||
{
|
||
//with 预载入 ,解决N+1问题
|
||
$list = CustomersModel::with('employees')->select();
|
||
return $list;
|
||
}
|
||
|
||
public function addCustomer($companyName)
|
||
{
|
||
$id = CustomersModel::create([
|
||
'company_name' => $companyName
|
||
]);
|
||
return $id;
|
||
}
|
||
|
||
public function addEmployee($empName, $Id)
|
||
{
|
||
$id = CustomersModel::where('id', $Id)->find()->employees()->save([
|
||
'employee_name' => $empName
|
||
]);
|
||
return $id;
|
||
}
|
||
|
||
|
||
public function deleteEmployee($empId)
|
||
{
|
||
$result = CustomerEmployees::destroy($empId);
|
||
return $result; // 返回受影响的行数,删除一行就1,没有删除就是0
|
||
}
|
||
|
||
public function deleteCustomer($customerId)
|
||
{
|
||
// 删除客户单位会自动删除关联的员工(假设你在模型中设置了级联删除)
|
||
$result = CustomersModel::destroy($customerId);
|
||
return $result; // 返回受影响的行数,删除一行就1,没有删除就是0
|
||
}
|
||
|
||
|
||
}
|