for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Smart\ContentBundle\Entity\Traits;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html
*/
trait ImageTrait
{
* @var string
*
* @ORM\Column(type="text", nullable=true)
protected $image;
* @Vich\UploadableField(mapping="smart_content_image", fileNameProperty="image")
* @Assert\Image
* @Assert\File(maxSize="500k")
* @var File
protected $imageFile;
* @return string
public function getImage()
return $this->image;
}
* @param string $image
* @return $this
public function setImage($image)
$this->image = $image;
return $this;
* @return bool
public function hasImage()
return ($this->image !== null);
* @return File
public function getImageFile()
return $this->imageFile;
* It is required that at least one field changes if you are using Doctrine,
* otherwise the event listeners won't be called and the file is lost
* @param File|UploadedFile $file
public function setImageFile(File $file = null)
$this->imageFile = $file;
if ($file) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime();
updatedAt
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: