4

I am working on a module where I have to implement one to many relationship. I have one table for series with id and name field and one table series_product where I have id, series_id, product_id and I want to make relation in models like we do in MVC. can we implement this on model??

  • You want multiple table relation via single model? – Amit Bera Jul 9 '18 at 9:19
  • I want If I delete record in series table then it should automatically deletes from series_product tables as well. – Daniel_12 Jul 9 '18 at 10:00
2

If you want delete the record in series table then it should automatically deletes from series_product tables as well then you need to work at model class & resource model class of series table.

Add afterDeleteCommitat Model class and add below code:

   public function afterDeleteCommit()
    {
        $this->getResource()->deleteSeries($this->getId())
        parent::afterDeleteCommit();
    }

Then on Resource class of series table define deleteSeries

  public function deleteSeries($series_id)
    {
        $this->getConnection()->delete( $this->getTable('series_product'), ['series_id = ?' => $series_id]);
        return $this;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.