如何使用Sluggable与自定义映射类型?

时间:2014-03-19 12:51:23

标签: symfony encryption mapping slug doctrine-extensions

在Symfony2项目中,我创建了一个自定义映射字段,以便按照this StackOverflow question中的建议加密数据库中的字符串。 我想使用Gedmo \ Sluggable Doctrine扩展来强化其中一个数据库字段。但显然我收到以下错误消息,因为“encrypted_string”不是允许的类型:

  

[Gedmo \异常\ InvalidMappingException]
    不能使用字段 - [username]进行slug存储,类型无效且必须为
    在课堂上是'字符串'或'文字' - 我的\ PrivateApplication \ Bundle \ UserBundle
    \实体\用户

- 编辑 - 这是我的实体:

<?php
namespace My\PrivateApplication\Bundle\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo,
    Gedmo\Translatable\Translatable;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * My\PrivateApplication\Bundle\UserBundle\Entity\User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="UserRepository")
 */
class User implements AdvancedUserInterface, \Serializable {
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="encrypted_string", length=255)
     */
    private $surname;

    /**
     * @var string
     *
     * @ORM\Column(type="encrypted_string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(type="encrypted_string", length=255, unique=true)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(type="encrypted_string", length=255)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(type="string", length=255, unique=true)
     * @Gedmo\Slug(fields={"username"})
     */
    private $slug;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=40)
     */
    private $salt;

    /**
     * Whether the account is active or not
     *
     * @var boolean
     *
     * @ORM\Column(name="is_active", type="boolean")
     */
    private $isActive;

    /**
     * Whether the account is locked or not
     *
     * @var boolean
     *
     * @ORM\Column(name="is_locked", type="boolean")
     */
    private $isLocked;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     * @Gedmo\Timestampable(on="create")
     *
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     * @Gedmo\Timestampable(on="update")
     */
    private $updatedAt;

    /**
     * @var string
     *
     * @Gedmo\Locale
     */
    private $locale;

    /**
     * Set the locale for the translatable behavior
     *
     * @param string $locale
     */
    public function setTranslatableLocale($locale) {
        $this->locale = $locale;
    }

    public function eraseCredentials()
    {
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getRoles()
    {
        return array((string)$this->getRole());
    }

    public function getSalt()
    {
        return $this->salt;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function isAccountNonExpired()
    {
        return true;
    }

    public function isAccountNonLocked()
    {
        return ($this->isLocked()==0)? false : true;
    }

    public function isCredentialsNonExpired()
    {
        return true;
    }

    public function isEnabled()
    {
        return ($this->isActive==0) ? false : true;
    }

    /**
     * Serialize the User object
     * @see Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array($this->id, $this->username, $this->password, $this->salt));
    }

    /**
     * Unserialize the User object
     * @see Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list($this->id, $this->username, $this->password, $this->salt) = unserialize($serialized);
    }

    //other accessor methods
}

类Gedmo \ Sluggable \ Mapping \ Driver \ Annotation的属性$ validTypes似乎定义了sluggable的有效类型。如何修改SluggableListener以使用我的新自定义类型?

非常感谢。

0 个答案:

没有答案