需要 slug 字段值作为 JDoe(对于 John Doe)在教义中



如何将 slug 字段设置为 JDoe(J-名字的第一个字符,Doe - 全名(;

/**
* @var string
*
* @GedmoSlug(fields={"firstName", "lastName"}, separator="??")
* @ORMColumn(name="slug", type="string", length=20, unique=true)
*/

谢谢!

方法 1

我没有尝试过以下内容,但它可能是您正在寻找的代码片段。 尝试一下,让我知道它是否有效:

class SomeEntity
{
/**
* @var string
*
* @GedmoSlug(fields={"getFirstNameSlugged", "lastName"}, separator="??")
* @ORMColumn(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());
}
}

方法 2

在您的实体中使用setSlug()方法:

class SomeEntity
{
const SEPERATOR = '??';
/**
* @var string
*
* @GedmoSlug(fields={"firstName", "lastName"}, separator="??")
* @ORMColumn(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;
}
}

并在您的控制器中:

$entity = new SomeEntity;
$entity->setSlug();
$em->persist($entity);
$em->flush();

方法 3

实体中,创建一个方法并在控制器中设置它:

class SomeEntity
{
/**
* @var string
*
* @GedmoSlug(fields={"firstName", "lastName"}, separator="??")
* @ORMColumn(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;
} 
}

并在您的控制器中:

$entity = new SomeEntity;
$callable = array('AppBundleEntitySomeEntity', 'generateSlug');
$sluggableListener = new GedmoSluggableSluggableListener();
$sluggableListener->setTransliterator($callable);
$em->persist($entity);
$em->flush();

相关内容

  • 没有找到相关文章

最新更新