1

我的问题 - 如果我在我的字段 slug 集中创建用户 - 名字+姓氏。如果我更新字段 firstname 和 lastname slug 没有改变,为什么?我需要为这个 trable 检查做什么?因为我在路由中使用 slug 字段(用户名),所以我有:

class SUser implements UserInterface
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @Gedmo\Slug(fields={"firstName", "lastName"}, separator="-")
 * @ORM\Column(name="name", type="string", length=255, nullable=false)
 * @Assert\Length(min=3, max=255)
 */
protected $username;

我的表格:

class DeveloperPersonalInformationType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstname', null, array('label' => 'First Name', 'max_length' => 255, 'required' => false))
        ->add('lastname', null, array('label' => 'Last Name', 'max_length' => 255, 'required' => false))

现在我使用自定义变体,首先在用户名中设置名字和姓氏结尾,但我想使用@Gedmo\Slug,怎么做?我的行动:

    public function submitPersonalInformationAction($username)
    {
    $em = $this->getDoctrine()->getManager();
    $request = $this->get('request');
    $profileRepository = $this->get('artel.profile.developer.repository');
    $developer = $profileRepository->findOneByUsername($username);


    $form = $this->createForm(new DeveloperPersonalInformationType(), $developer);
    $form->handleRequest($request);

    if ($form->isValid()) 
    {
        $name = $developer->getFirstName().'-'.$developer->getLastName();
        $developer->setUsername($name);
        $em->flush();

        return $this->redirect($this->generateUrl('artel_profile_homepage', array('username' => $name)) .'#personal-information');
    }

我的轮换:

artel_profile_submit_personal_information:
path:     /profile/{username}/personal_information/submit
defaults: { _controller: ArtelProfileBundle:DeveloperProfile:submitPersonalInformation }
requirements: { _method: post }

我的配置:

stof_doctrine_extensions:
default_locale: "%locale%"
orm:
    default:
        timestampable: true
        sluggable:     true
4

2 回答 2

0

大概是这样的:

if ($form->isValid()) 
{
    $developer->setUsername(Null);
    $em->flush($developer);

    return $this->redirect($this->generateUrl('artel_profile_homepage', array('username' => $name)) .'#personal-information');
}

但我已经注意到测试了它。

于 2015-07-12T22:03:43.673 回答
0

您已将 slug 字段设置为 Null 以使其更新

https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sluggable.md#regenerating-slug

于 2015-07-12T12:24:08.770 回答