【问题标题】:SQL request or php solution to manage employers管理雇主的 SQL 请求或 php 解决方案
【发布时间】:2020-04-07 05:39:33
【问题描述】:

我的实体:

class User{
    id   : int 
    name : string
    boss : User()
}

我想创建一个函数,该函数返回在给定用户下工作的用户数组()。 示例:

public function MyEmployers( User $user , array $usersList  )
{
    $myEmployers = array();

   ...

    return $myEmployers;
}

$results =  $this->myEmployers ( $employer1 , $allEmployers)
dump ( $results );

$results  = [ 4 , 5 , 6 ];

如果有人可以随意改进,我找到了解决方案:

public $tree = array();

public function MyEmployers(User $user)
{

    $superior_key_id = array();
    $all_users = $this->getallusers();
    $id = $user->getId();

    foreach ($all_users as $user) {
        if ($user->getSuperior())
            $superior_key_id[$user->getSuperior()->getId()][] = $user;
    }
    $this->getSubEmployee($this->getUser(), $superior_key_id);

    return ($this->tree);

}

public function getSubEmployee($user, $superior_key_id)
{
    if (isset($superior_key_id[$user->getId()])) {
        foreach ($superior_key_id[$user->getId()] as $user) {
            $this->tree[] = $user;
            $this->getSubEmployee($user, $superior_key_id);
        }
    }
    return $user;
}

如果你有兴趣,这个会得到所有老板:

public function MyBosses(User $user)
{
    $bosses = array();

    while ($user->getSuperior()) {

        array_push($bosses, $user->getSuperior());

        $user = $user->getSuperior();
    }

    return $bosses;

} 

【问题讨论】:

  • 那么,如果我问谁在boss 下工作,我会返回employer1empoyer6?或者只是employer1employer2
  • 每个人都在老板手下工作,结果将是这样的 $results = [1 , 2 , 3 , 4 , 5 , 6 ];

标签: php sql symfony


【解决方案1】:

您可以在bossemployee 之间建立一对多关系

所以你的用户类可能看起来像:

class User{
    /**
    * @ORM\Id()
    * @ORM\GeneratedValue()
    * @ORM\Column(type="integer")
    */
    private $id;

    /**
    * @ORM\Column(type="string")
    */
    private $name;

    /**
    * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="employee")
    */
    private $boss;

    /**
    * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="boss")
    */
    private $employees;

    public function __construct()
    {
        $this->employees = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
    * @return Collection|employees[]
    */
    public function getEmployees(): Collection
    {
        return $this->employees;
    }

    public function addEmployee(employee $employee): self
    {
        if (!$this->employees->contains($employee)) {
            $this->employees[] = $employee;
        }

        return $this;
    }

    public function removeEmployee(employee $employee): self
    {
        if ($this->employees->contains($employee)) {
            $this->employees->removeElement($employee);
        }

        return $this;
    }

    public function getBoss(): ?Usesr
    {
        return $this->boss;
    }

    public function setBoss(?User $boss): self
    {
        $this->boss = $boss;

        return $this;
    }

}

您需要设置与symfony make:entity command 的数据库关系。

要将所有员工都归入一个用户,您可以执行以下操作:

function getAllEmployeesUnder(User $user)
{
    $allEmployees = [];

    foreach ($user->getEmployees as $employee) {
        $allEmployees[] = $employee;
        $allEmployees = array_merge($allEmployees, $this->getAllEmployeesUnder($employee));
    }

    return $allEmployees;
}

【讨论】:

  • 我找到了一个解决方案,我想分享它,也许有一天它会对开发人员有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 2019-04-29
相关资源
最近更新 更多