Answer the question
In order to leave comments, you need to log in
How to get an entry by slug correctly?
Good evening.
Help, please, to solve the problem.
There is an entity Region, there is also an embeddable class Name.
/*
* @ORM\Entity
* @ORM\Table(name="location_regions")
*/
class Region
{
/**
* @var Id
* @ORM\Column(type="location_region_id")
* @ORM\Id
*/
private Id $id;
/**
* @var Name
* @ORM\Embedded(class="Name", columnPrefix=false)
*/
private Name $name;
/....../
public functon getName(): Name
{
return $this->name;
}
#############################################
/**
* @ORM\Embeddable()
*/
class Name
{
/**
* @var string
* @ORM\Column(type="string", unique=true)
*/
private string $name;
/**
* @var string
* @ORM\Column(type="string", unique=true, nullable=true)
* @Gedmo\Slug(fields={"name"})
*/
private string $slug;
public function __construct(string $name)
{
Assert::notEmpty($name);
$this->name = $name;
}
public function getValue(): string
{
return $this->name;
}
public function getSlug(): string
{
return $this->slug;
}
}
Unable to guess how to get a Doctrine instance from the request information for parameter "region".
/**
* @param Region $region
* @param RegionFetcher $fetcher
* @return Response
*
* @Route("/region/{slug}", name=".show")
* @ParamConverter("region", options={"mapping": {"slug": "slug"}})
*/
public function show(Region $region, RegionFetcher $fetcher): Response
{
$region = $fetcher->find($region->getId()->getValue());
return $this->render('app/admin/location/regions/show.html.twig', ['region' => $region]);
}
<a href="{{ path('location.regions.show', {'slug': region.slug}) }}">{{ region.name }}</a>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question