This topic contains 1 reply, has 2 voices, and was last updated by Mike Kudelya 7 years, 8 months ago.
- Topic
Hi,
I’am trying to use LifeCycleCallbacks for upload file to an entity.
I’am not sure but it seems that the callbacks methods define in entity with @PrePersist or @PreUpdate are not working.I supposed first that it was because my entity no necessary change and then events aren’t triggered but still not work even if I had a field updatedAt (which necessary change).
I would like to not put all the logic in controller…
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227<?phpnamespace Sinabs\MonitoringBundle\Entity;use Doctrine\ORM\Mapping as ORM;use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;use Symfony\Component\Validator\Constraints as Assert;use Symfony\Component\Validator\Context\ExecutionContextInterface;use Symfony\Component\HttpFoundation\File\UploadedFile;use Sinabs\MonitoringBundle\Entity\Monitor;use Sinabs\MonitoringBundle\Model\Mask;use Sinabs\MonitoringBundle\Interfaces\MonitorInterface;/*** PingMonitor** @ORM\Table(name="ping_monitor")* @ORM\Entity(repositoryClass="Sinabs\MonitoringBundle\Entity\PingMonitorRepository")* @ORM\HasLifecycleCallbacks* @Config(* defaultValues={* "entity"={* "label"="PingMonitor",* "plural_label"="PingMonitors"* },* "security"={* "type"="ACL",* "permissions"="All"* },* "ownership"={* "owner_type"="BUSINESS_UNIT",* "owner_field_name"="owner",* "owner_column_name"="business_unit_owner_id",* "organization_field_name"="organization",* "organization_column_name"="organization_id"* }* }* )*/class PingMonitor extends Monitor implements MonitorInterface {/*** @var string** @ORM\Column(name="url", type="string", length=255)*/private $url;/*** @ORM\Column(name="path", type="string", length=255, nullable=true)*/private $path;/*** updated Time/Date** @var \DateTime** @ORM\Column(name="updated_at", type="datetime", nullable=false)*/protected $updatedAt;private $file;private $targetDir;private $tempFilename;/*** Constructor*/public function __construct() {parent::__construct();$this->targetDir = $this->getParameter('kernel.root_dir') . '/Resources/public/casperjs';}/*** Set url* @param string $url* @return PingMonitor*/public function setUrl($url) {$this->url = $url;return $this;}/*** Get url* @return string*/public function getUrl() {return $this->url;}/*** Set path** @param string $path** @return PingMonitor*/public function setPath($path) {$this->casperjs = $path;return $this;}/*** Get path** @return string*/public function getPath() {return $this->path;}public function getFile() {return $this->file;}public function setFile(UploadedFile $file = null) {$this->file = $file;// On vérifie si on avait déjà un fichier pour cette entitéif (!is_null($this->casperjs)) {// On sauvegarde l'extension du fichier pour le supprimer plus tard$this->tempFilename = $this->casperjs;// On réinitialise les valeurs des attributs url et alt$this->casperjs = null;}}/*** Set updatedAt** @ORM\PreUpdate* @ORM\PrePersist*/public function setUpdatedAt() {$this->updatedAt = new \DateTime();}/*** Get updatedAt** @return \DateTime*/public function getUpdatedAt() {return $this->updatedAt;}/*** @ORM\PrePersist()* @ORM\PreUpdate()*/public function preUpload() {// Si jamais il n'y a pas de fichier (champ facultatif)if (is_null($this->file)) {return;}$this->casperjs = $this->file->getClientOriginalName();}/*** @ORM\PostPersist()* @ORM\PostUpdate()*/public function upload() {// Si jamais il n'y a pas de fichier (champ facultatif)if (is_null($this->file)) {return;}// Si on avait un ancien fichier, on le supprimeif (!is_null($this->tempFilename)) {$oldFile = $this->targetDir . '/' . $this->tempFilename;if (file_exists($oldFile)) {unlink($oldFile);}}// On déplace le fichier envoyé dans le répertoire de notre choix$this->file->move($this->targetDir, // Le répertoire de destination$this->casperjs // Le nom du fichier à créer);}/*** @ORM\PreRemove()*/public function preRemoveUpload() {// On sauvegarde temporairement le nom du fichier, car il dépend de l'id$this->tempFilename = $this->targetDir . '/' . $this->casperjs;}/*** @ORM\PostRemove()*/public function removeUpload() {// En PostRemove, on n'a pas accès à l'id, on utilise notre nom sauvegardéif (file_exists($this->tempFilename)) {// On supprime le fichierunlink($this->tempFilename);}}/*** @Assert\Callback*/public function isUrlExist(ExecutionContextInterface $context) {$ch = curl_init($this->webSite->getUrl() . $this->url);curl_exec($ch);$intReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);curl_close($ch);if ($intReturnCode != 200 && $intReturnCode != 304) {// La règle est violée, on définit l'erreur$context->buildViolation('url not exist') // message->atPath('url') // attribut de l'objet qui est violé->addViolation() // ceci déclenche l'erreur, ne l'oubliez pas;}}}Thanks to oroTeam
The forum ‘OroCRM – Installation/Technical Issues or Problems’ is closed to new topics and replies.