Question

I have two models

  1. Gallery
  2. Image

While deleting Gallery I want to delete the associated Image records ans images in directory and then delete Gallery object and directory .

The best choice seems to be onBeforeContentDelete trigger, for which I have to write a plugin. But do not want to go through all plugin creation steps.

Is it possible to delete this way ?

class SomePrefixModelGallery extends JModelAdmin
{
    public function __construct($config=array())
    {
        parent::__construct($config);
    }

    public function onBeforeGalleryDelete($context,$table){
        if( $this->deleteAllImages()){
           $this->removeDir($this->folder);
          return true;
         }
        else
         return false;
    }

    public function deleteAllImages(){
       // get Image model and delete all images 
    }
}

Going through JModelAdmin core class I can see that in config array event name could be passed e.g

 `array('event_before_delete'=>'onXYZEventName')`

The default event set is onBeforeContentDelete

Was it helpful?

Solution

If it's your own component you can just override the delete method in your model, you don't need a plugin.

JModelAdmin has a delete() method, so in your Model:

class SomePrefixModelGallery extends JModelAdmin
{
      /* all the standard methods */

     /**
      * Method to delete one or more records.
      *
      * @param   array  &$pks  An array of record primary keys.
      *
      * @return  boolean  True if successful, false if an error occurs.
      *
      * @since   11.1
      */
     public function delete(&$pks)
     {
        // Code to delete your image records

        // Code to delete your image files

        // Call the parent delete to remove the Gallery entry
        parent::delete(&$pks);
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top