Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set slug using associated entity field

I'm using DoctrineExtensionsto get sluggable behavior for my entities. One one of my entities I would like to use the city field of the related address entity for the slug. However, I don't know how to access it:

/**
 * @var Foo\SiteBundle\Entity\Address
 *
 * @ORM\ManyToOne(targetEntity="Foo\SiteBundle\Entity\Address", cascade={"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="address_id", referencedColumnName="id")
 * })
 */
private $address;

/**
 * @Gedmo\Slug(fields={"address->city", "name"})
 * @ORM\Column(length=128, unique=true)
 */
private $slug;

How can I make this work?

like image 537
Thomas K Avatar asked Mar 31 '26 12:03

Thomas K


1 Answers

You could perform it: https://github.com/l3pp4rd/DoctrineExtensions/issues/86

But here https://github.com/l3pp4rd/DoctrineExtensions:

2012-02-26

Removed slug handlers, this functionality brought complucations which could not be maintained.

Unfortunately this functionnality was removed !

So I think you have to change your logical: for example, you can use a route containing the slug of the address and the current entity and load the entity in response:

@Route("/user/{slugUser}/{slugAddress}.html", requirements={"slugUser"="^[a-z0-9-]+", "slugAddress"="^[a-z0-9-]+"})

Or perhaps a solution, try to set it manually... and automatically with lifecycles, but i'm not sure if it works:

/**
 * @Gedmo\Slug(fields={})
 * @ORM\Column(length=128, unique=true)
 */
private $slug;

/**
 * @ORM\PrePersist
 */
public function updateSlug()
{
    $this->setSlug($this->name.$this->address->getCity());
}
like image 184
Sybio Avatar answered Apr 03 '26 07:04

Sybio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!