Reputation: 79
How can I set slug field to JDoe (J-First char of first name, Doe - full last name);
/**
* @var string
*
* @Gedmo\Slug(fields={"firstName", "lastName"}, separator="??")
* @ORM\Column(name="slug", type="string", length=20, unique=true)
*/
Thanks!
Upvotes: 1
Views: 64
Reputation: 17964
I have not tried the following, but it is probably the snippet you are looking for. git it a try and let me know if it works:
class SomeEntity
{
/**
* @var string
*
* @Gedmo\Slug(fields={"getFirstNameSlugged", "lastName"}, separator="??")
* @ORM\Column(name="slug", type="string", length=20, unique=true)
*/
private $slug;
private function getFirstNameFirstLetter()
{
return substr ($this->firstName, 0 , 1);
}
private function getFirstNameSlugged()
{
return ucfirst (getFirstNameFirstLetter());
}
}
in your Entity use a setSlug()
method:
class SomeEntity
{
const SEPERATOR = '??';
/**
* @var string
*
* @Gedmo\Slug(fields={"firstName", "lastName"}, separator="??")
* @ORM\Column(name="slug", type="string", length=20, unique=true)
*/
private $slug;
private function getFirstNameFirstLetter()
{
return substr ($this->firstName, 0 , 1);
}
private function getFirstNameSlugged()
{
return ucfirst (getFirstNameFirstLetter());
}
private function getLastNameSlugged()
{
return ucfirst ($this->getLastName());
}
public function setSlug()
{
$this->slug = $this->getFirstNameSlugged() . self::SEPERATOR . $this->getLastNameSlugged();
return $this->slug;
}
}
and in your Controller:
$entity = new SomeEntity;
$entity->setSlug();
$em->persist($entity);
$em->flush();
in your Entity, create a method and set it in your Controller:
class SomeEntity
{
/**
* @var string
*
* @Gedmo\Slug(fields={"firstName", "lastName"}, separator="??")
* @ORM\Column(name="slug", type="string", length=20, unique=true)
*/
private $slug;
private function getFirstNameFirstLetter()
{
return substr ($this->firstName, 0 , 1);
}
private function getFirstNameSlugged()
{
return ucfirst (getFirstNameFirstLetter());
}
private function getLastNameSlugged()
{
return ucfirst ($this->getLastName());
}
public generateSlug()
{
$slug = $this->getFirstNameSlugged() . self::SEPERATOR . $this->getLastNameSlugged();
return $slug;
}
}
and in your Controller:
$entity = new SomeEntity;
$callable = array('AppBundle\Entity\SomeEntity', 'generateSlug');
$sluggableListener = new \Gedmo\Sluggable\SluggableListener();
$sluggableListener->setTransliterator($callable);
$em->persist($entity);
$em->flush();
Upvotes: 1