<?php
namespace App\Entity;
use App\Repository\CityRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation as Serializer;
/**
* @Gedmo\Tree(type="nested")
* @Serializer\ExclusionPolicy("ALL")
*/
#[ORM\Entity(repositoryClass: CityRepository::class)]
#[ORM\Table(name: '`city`')]
class City
{
/**
* @Serializer\Expose()
*/
#[ORM\Id]
#[ORM\GeneratedValue(strategy:"AUTO")]
#[ORM\Column(type: "integer")]
protected ?int $id = null;
/**
* @Serializer\Expose()
*/
#[ORM\Column(type: "string", length: 200)]
protected $name;
#[ORM\Column(type: "string", nullable: true)]
protected $number;
/**
* @Gedmo\TreeParent
* @Serializer\Expose()
*/
#[ORM\ManyToOne(targetEntity:"City", inversedBy: "children" ,fetch:"EAGER")]
#[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id", onDelete: "CASCADE")]
protected $parent;
#[ORM\OneToMany(targetEntity:"City", mappedBy:"parent", cascade: ['persist', 'remove'])]
#[ORM\OrderBy(["lft" => "ASC"])]
protected $children;
/**
* @Gedmo\TreeLeft
*/
#[ORM\Column(type: "integer")]
protected $lft;
/**
* @Gedmo\TreeRight
*/
#[ORM\Column(type: "integer")]
protected $rgt;
/**
* @Gedmo\TreeLevel
*/
#[ORM\Column(type: "integer")]
protected $lvl;
/**
* @Gedmo\TreeRoot
*/
#[ORM\Column(type: "integer", nullable: true)]
protected $root;
public function __construct()
{
$this->children = new ArrayCollection;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function getNumber()
{
return $this->number;
}
public function setNumber($number)
{
$this->number = $number;
}
public function setParent(City $parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function addChild(City $child)
{
$this->children[] = $child;
$child->setParent($this);
}
public function removeChild(City $child)
{
$child->setParent(null);
return $this->children->removeElement($child);
}
public function getChildren()
{
return $this->children;
}
public function __toString()
{
return $this->name;
}
public function setLft($lft)
{
$this->lft = $lft;
}
public function getLft()
{
return $this->lft;
}
public function setRgt($rgt)
{
$this->rgt = $rgt;
}
public function getRgt()
{
return $this->rgt;
}
public function setLvl($lvl)
{
$this->lvl = $lvl;
}
public function getLvl()
{
return $this->lvl;
}
public function setRoot($root)
{
$this->root = $root;
}
public function getRoot()
{
return $this->root;
}
public function isChildOf(City $category)
{
$me = $this;
while ($me->parent) {
if ($category === $me->parent) {
return true;
}
$me = $me->parent;
}
return false;
}
}