3

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

[Gedmo\Exception\InvalidMappingException]
无法使用字段 - [用户名] 用于 slug 存储,类型无效并且必须
是类中的“字符串”或“文本” - My\PrivateApplication\Bundle\UserBundle
\Entity\User

--编辑-- 这是我的实体:

<?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 以使用我的新自定义类型?

非常感谢。

4

0 回答 0