Переглянути джерело

Merged in FD3-658 (pull request #22)

FD3-658 control en eliminación de Perfíl de Cablemodems

Approved-by: Guillermo Espinoza <guillermo@interlink.com.ar>
Approved-by: Maximiliano Schvindt <maximiliano@interlink.com.ar>
Maximiliano Schvindt 6 роки тому
батько
коміт
4f6edb10c8

+ 4 - 4
composer.lock

@@ -1458,7 +1458,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/BaseAdmin.git",
-                "reference": "cd1449790213410afbf0cce650997c9c24a481d8"
+                "reference": "a9b57ea652fa0cfe12a32c3135860bb9bcfe0453"
             },
             "type": "library",
             "autoload": {
@@ -1473,7 +1473,7 @@
                 "bootstrap",
                 "sonata"
             ],
-            "time": "2018-08-14T12:58:30+00:00"
+            "time": "2018-09-04T16:04:16+00:00"
         },
         {
             "name": "ik/device-bundle",
@@ -1691,7 +1691,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@gogs.infra.flowdat.com:222/VendorSoftwareFlowdat3/WorkflowBundle.git",
-                "reference": "580409eb57da6656829201d41e8b755e85c626cf"
+                "reference": "a929fd734fdeee5a7b91d4eba944c7e39ccdb384"
             },
             "require": {
                 "php-amqplib/rabbitmq-bundle": "^1.12"
@@ -1721,7 +1721,7 @@
                 "bundle",
                 "workflow"
             ],
-            "time": "2018-08-24T18:47:22+00:00"
+            "time": "2018-09-03T11:59:55+00:00"
         },
         {
             "name": "incenteev/composer-parameter-handler",

+ 2 - 2
src/CablemodemBundle/Entity/Cablemodem.php

@@ -87,7 +87,7 @@ class Cablemodem implements DeviceInterface, TenancyIdTraitInterface, LocationIn
 
     /**
      * @ORM\ManyToOne(targetEntity="Profile", fetch="EXTRA_LAZY")
-     *
+     * @ORM\JoinColumn(name="profile_id", referencedColumnName="id", onDelete="SET NULL")
      * @JMS\MaxDepth(1)
      *
      * @Assert\NotNull
@@ -96,7 +96,7 @@ class Cablemodem implements DeviceInterface, TenancyIdTraitInterface, LocationIn
 
     /**
      * @ORM\ManyToOne(targetEntity="CablemodemModel", fetch="EXTRA_LAZY")
-     *
+     * 
      * @JMS\MaxDepth(1)
      */
     protected $model;

+ 69 - 2
src/CablemodemBundle/Entity/Profile.php

@@ -9,14 +9,17 @@ use ExtraDataBundle\Entity\Traits\ExtraDataTrait;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 use Symfony\Component\Validator\Constraints as Assert;
 use Symfony\Component\Yaml\Yaml;
+use Base\AdminBundle\Interfaces\PreRemoveBlockCascadeInterface;
+use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
+use Sonata\AdminBundle\Exception\ModelManagerException;
 
 /**
  * @ORM\Entity
  * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name","tenancy_id"})})
- *
+ * @ORM\HasLifecycleCallbacks
  * @UniqueEntity(fields={"tenancyId","name"})
  */
-class Profile  implements TenancyIdTraitInterface
+class Profile  implements TenancyIdTraitInterface, PreRemoveBlockCascadeInterface
 {
 
     use TenancyIdTrait;
@@ -88,6 +91,11 @@ class Profile  implements TenancyIdTraitInterface
      * @ORM\Column(type="string", nullable=true)
      */
     protected $template;
+
+    /**
+     * @ORM\OneToMany(targetEntity="Cablemodem", mappedBy="profile", fetch="EXTRA_LAZY")
+     */
+    protected $cablemodems;
     
 
     public function __construct()
@@ -95,6 +103,7 @@ class Profile  implements TenancyIdTraitInterface
         $this->downstream = $this->getDefault('downstream');
         $this->upstream = $this->getDefault('upstream');
         $this->template = $this->getDefault('template');
+        $this->cablemodems = new \Doctrine\Common\Collections\ArrayCollection();
     }
 
     /**
@@ -306,4 +315,62 @@ class Profile  implements TenancyIdTraitInterface
         return $this;
     }
 
+    /**
+     * @param Cablemodem $cablemodem
+     * @return Profile
+     */
+    public function addCablemodem(Cablemodem $cablemodem)
+    {
+        $this->cablemodems[] = $cablemodem;
+
+        return $this;
+    }
+
+    /**
+     * @param Cablemodem $cablemodem
+     * @return Profile
+     */
+    public function removeCablemodem(Cablemodem $cablemodem)
+    {
+        $this->cablemodems->removeElement($cablemodem);
+
+        return $this;
+    }
+
+    /**
+     * @return Doctrine\Common\Collections\Collection
+     */
+    public function getCablemodems()
+    {
+        return $this->cablemodems;
+    }
+
+    /**
+     * @return array
+     */
+    public function getEntitiesForRemove()
+    {
+        $entities = [];
+        if ($this->cablemodems->count() != 0) {
+            $entities['cablemodems'] = $this->cablemodems;
+        }
+
+        return $entities;
+    }
+
+    /**
+     * @ORM\PreRemove
+     */
+    public function preRemove(LifecycleEventArgs $event)
+    {   
+        $em = $event->getEntityManager(); 
+        $entity = $event->getEntity(); 
+        
+        if (count($entity->getCablemodems()) > 0) {
+            $error = "This profile has connected cablemodems, so it can't be removed.";
+            throw new ModelManagerException($error);
+        }
+        
+    }
+
 }