与索纳塔管理员的独特领域 - SYMFONY

时间:2017-09-21 09:40:20

标签: unique sonata

在symfony 3中,使用sonata admin。我做了一些实体和 我有一个字段"名称" 的实体,我希望是唯一的。

将理论与YML结合使用。我试用验证约束但没有成功! 我试过这个,但注意到了

我的对象名称

this.mediaPlugin

我的学说档案:

    <?php

namespace AdminBundle\Entity;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;

    /**
     * Organisation
     */
class Organisation
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string(200)
     */
    private $name;

    /**
     * @var int
     */
    private $organisationtype=null;

     /**
     * @var string
     */
    private $description=null;

    /**
     * @var string(100)
     */
    private $address1=null;

    /**
     * @var string(100)
     */
    private $address2=null;

    /**
     * @var string(11)
     */
    private $zipcode=null;

    /**
     * @var string(50)
     */
    private $city=null;

    /**
     * @var int
     */
    private $country=null;

    /**
     * @var string(60)
     */
    private $phone=null;

    /**
     * @var string(100)
     */
    private $URL=null;

    /**
     * @var string(50)
     */
    private $email=null;


   /**
     * @var string(50)
     */
    private $comments=null;

    /**
     * @var \DateTime
     */
    private $created;

    /**
     * @var \DateTime
     */
    private $updated;


    public function setCreatedAtValue()
    {
      $this->created = new \DateTime();
    }

    public function setUpdatedAtValue()
    {
        $this->updated = new \DateTime();
    }


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }


    public function __toString(){
    return $this->name;    
}

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Organisation
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set organisationtype
     *
     * @param $organisationtype
     *
     * @return Organisation
     */
    public function setOrganisationtype($organisationtype)
    {
        $this->organisationtype = $organisationtype;

        return $this;
    }

    /**
     * Get organisationtype
     *
     * @return int
     */
    public function getOrganisationtype()
    {
        return $this->organisationtype;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Organisation
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set address1
     *
     * @param string $address1
     *
     * @return Organisation
     */
    public function setAddress1($address1)
    {
        $this->address1 = $address1;

        return $this;
    }

    /**
     * Get address1
     *
     * @return string
     */
    public function getAddress1()
    {
        return $this->address1;
    }


    /**
     * Set address2
     *
     * @param string $address2
     *
     * @return Organisation
     */
    public function setAddress2($address2)
    {
        $this->address2 = $address2;

        return $this;
    }

    /**
     * Get address2
     *
     * @return string
     */
    public function getAddress2()
    {
        return $this->address2;
    }

    /**
     * Set zipcode
     *
     * @param int $zipcode
     *
     * @return Organisation
     */
    public function setZipcode($zipcode)
    {
        $this->zipcode = $zipcode;

        return $this;
    }

    /**
     * Get zipcode
     *
     * @return string
     */
    public function getZipcode()
    {
        return $this->zipcode;
    }


    /**
     * Set city
     *
     * @param string $city
     *
     * @return Organisation
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return string
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set country
     *
     * @param $country
     *
     * @return Organisation
     */
    public function setCountry($country)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return int
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set phone
     *
     * @param int $phone
     *
     * @return Organisation
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Get phone
     *
     * @return string
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Set URL
     *
     * @param string $URL
     *
     * @return Organisation
     */
    public function setURL($URL)
    {
        $this->URL = $URL;

        return $this;
    }

    /**
     * Get URL
     *
     * @return string
     */
    public function getURL()
    {
        return $this->URL;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Organisation
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set comments
     *
     * @param string $comments
     *
     * @return Organisation
     */
    public function setComments($comments)
    {
        $this->comments = $comments;

        return $this;
    }

    /**
     * Get comments
     *
     * @return string
     */
    public function getComments()
    {
        return $this->comments;
    }


    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Contact
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Contact
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }



}

我的validation.orm.yml

AdminBundle\Entity\Organisation:
type: entity
table: null
repositoryClass: AdminBundle\Repository\OrganisationRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 200
        unique: true
    description:
        type: text
        nullable: TRUE
    address1:
        type: string
        length: 100
        nullable: TRUE
    address2:
        type: string
        length: 100
        nullable: TRUE
    zipcode:
        type: string
        length: 11
        nullable: TRUE
    city:
        type: string
        length: 50
        nullable: TRUE
    phone:
        type: string
        length: 60
        nullable: TRUE
    URL:
        type: string
        length: 100
        nullable: TRUE
    email:
        type: string
        length: 50
        nullable: TRUE
    comments:
        type: string
        length: 255
        nullable: TRUE
    created:
        type: datetime
        nullable: true
        gedmo:
          timestampable:
            on: create
    updated:
        type: datetime
        nullable: true
        gedmo:
          timestampable:
            on: update
uniqueConstraints:
search_idx:
  columns: [ name ]
lifecycleCallbacks:
  prePersist: [ setCreatedAtValue,setUpdatedAtValue ]
  preUpdate: [ setUpdatedAtValue ]
manyToOne:
    organisationtype:
        targetEntity: AdminBundle\Entity\Organisation_type
        joinColumn:
            name: organisationtype
            referencedColumnName: id
        nullable: TRUE
    country:
        targetEntity: AdminBundle\Entity\Country
        joinColumn:
            name: country
            referencedColumnName: id
        nullable: TRUE

和我的配置文件

AdminBundle\Entity\Organisation:
constraints:
    - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
        fields: name
        message: 'Ce nom est dèjà utilisé'

我不知道我错了什么

1 个答案:

答案 0 :(得分:0)

只有“独特”部分不起作用吗?

当您提交超过50个字符的“电子邮件”时,会发生什么?