create project
This commit is contained in:
64
common/services/CompanyService.php
Normal file
64
common/services/CompanyService.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\Company;
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class CompanyService
|
||||
{
|
||||
public Company $model;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->model = new Company();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @return array
|
||||
*/
|
||||
public function getCompaniesByUser(int $id = null): array
|
||||
{
|
||||
return $this->model->find()->where(['user_id' => $id ?? Yii::$app->user->id])->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @return array
|
||||
*/
|
||||
public function getCompaniesByUserArr(int $id = null): array
|
||||
{
|
||||
return ArrayHelper::map($this->getCompaniesByUser($id), 'id', 'name');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Company|null
|
||||
*/
|
||||
public function getCompany(int $id): ?Company
|
||||
{
|
||||
return $this->model->findOne($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @return array
|
||||
*/
|
||||
public function getAddressesByUser(int $id = null): array
|
||||
{
|
||||
$companies = $this->getCompaniesByUser($id);
|
||||
return $this->model->find()->where(['id' => ArrayHelper::getColumn($companies, 'id')])->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @return array
|
||||
*/
|
||||
public function getAddressesByUserArr(int $id = null): array
|
||||
{
|
||||
return ArrayHelper::map($this->getAddressesByUser($id), 'id', 'address');
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user