2015-10-23 120 views
0

我有3種型號:MODELA,ModelB和ModelC。如何刪除通過關係創建的相關項目?

模型 「A」 具有通模式 「C」 的關係(HasManyThrough)與模型 「B」。

如何刪除相關的項目?

module.exports = function(ModelA) { 
    ModelA.beforeRemote('deleteById', function(context, remoteMethodOutput, next) { 

     //Remove relationships 

     next(); 
    }); 
}; 
+0

你想先刪除相關的項目?這是你的問題嗎? –

+0

@VishalKumar是的。 – Filipe

回答

0

一些考慮:

  1. 調試上下文真正找到哪裏是ID與請求一起發送。我使用通用示例來獲取modelA的id。

  2. 要刪除相關的modelB實例,modelAInstance.modelB.destroyAll()其中modelB是關係的名稱,「modelA hasMany modelB」。

    ModelA.beforeRemote('deleteById', function(context, remoteMethodOutput, next) { 
    
        //get the id of ModelA from context object. 
        var id = context.req.id; 
    
        //find the model instance to delete. 
        ModelA.findById(id, function(err, modelAInstance) { 
         if (err) throw err; 
    
         //destroy all ModelB instance related to modelAInstance. 
         modelAInstance.modelB.destroyAll({}, function(err, info) { 
          if (err) throw err; 
          console.log(info); 
         }); 
        }); 
        next(); 
    });