Search code examples
phpsymfonyannotationsentityslug

How annotation @groups for the sluggable column


I'm working in symfony 3.3:

I need to add the @Groups annotation to the slug field (created using ORMBehaviors\Sluggable\Sluggable) so I can pass this field through other entities that I will specify in the Groups. How can I do this?

this is the entity 'foo':

/**
 * @var int
 *
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 *
 * @Groups({"advertisingSpace"})
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=4)
 *
 * @Groups({"advertisingSpace"})
 */
private $code;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=255)
 *
 * @Groups({"advertisingSpace"})
 */
private $description;

/**
 * @Vich\UploadableField(mapping="advertisingspacecategory_icon", fileNameProperty="icon")
 *
 * @var File
 */
private $iconFile;

/**
 * @var string
 *
 * @ORM\Column(type="string", length=255)
 *
 * @Groups({"advertisingSpace"})
 */
private $icon;

/**
 * @var array
 *
 * @ORM\OneToMany(
 *     targetEntity="AppBundle\Entity\AdvertisingSpace",
 *     mappedBy="category"
 * )
 */
private $advertisingSpaces;

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

/**
 * @return string
 */
public function getIcon()
{
    return $this->icon;
}

/**
 * @param string $icon
 *
 * @return AdvertisingSpaceCategory
 */
public function setIcon($icon)
{
    $this->icon = $icon;

    return $this;
}

/**
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $iconFile
 *
 * @return AdvertisingSpaceCategory
 */
public function setIconFile(File $iconFile = null)
{
    $this->iconFile = $iconFile;

    if ($iconFile) {
        $this->updatedAt = new \DateTimeImmutable();
    }

    return $this;
}

/**
 * @return File|null
 */
public function getIconFile()
{
    return $this->iconFile;
}

/**
 * @param int $id
 *
 * @return AdvertisingSpaceCategory
 */
public function setId($id)
{
    $this->id = $id;

    return $this;
}

/**
 * @return string
 */
public function getCode()
{
    return $this->code;
}

/**
 * @param string $code
 *
 * @return AdvertisingSpaceCategory
 */
public function setCode($code)
{
    $this->code = $code;

    return $this;
}

/**
 * @return array
 */
public function getAdvertisingSpaces()
{
    return $this->advertisingSpaces;
}

/**
 * @param array $advertisingSpaces
 *
 * @return AdvertisingSpaceCategory
 */
public function setAdvertisingSpaces($advertisingSpaces)
{
    $this->advertisingSpaces = $advertisingSpaces;

    return $this;
}

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

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

    return $this;
}

/**
 * @return array
 */
public function getSluggableFields()
{
    return [ 'slug' ];
}

/**
 * @param string $values
 *
 * @return string
 */
public function generateSlugValue($values)
{
    return implode('-', $values);
}

not having the private slug declaration as I can attribute the @groups annotation to?


Solution

  • I solved it anyway by creating a new string variable (Eg "SlugName"):

    /**
     * @var string
     *
     * @Groups({"advertisingSpace"})
     */
    private $slugName;
    

    where I inserted the Groups annotation and which returns the original 'slug' field from its getter:

    /**
     * @ return string
     */
    public function getSlugName(): string
    {
        return $this->slug;
    }