eT` Posted February 10, 2012 Report Share Posted February 10, 2012 Sveiki, Vai ir veids, kā var atrast visas saistītās ( bērnu ) tabulas, pēc padotā tabulas nosaukuma, un ja tādas ir, tad izdzēst no tām visus ierakstus, kam atslēgas lauka vērtība sakrīt ar padoto $id? Piemērs: Galerijas->Bildes->Komentāri Izdzēsot Galeriju, izdzēšas arī bildes un komentāri ( reversā secībā ). Ja izdomātu f-ju, tad rekursiju gan jau uztaisītu. Bet tā lai f-ja ir forši OOP ( katram jaunajam modulim nav jātaisa klāt esošajā f-jā kaut kas ) Pašreizējā f-jas: <?php //Photo klase public function destroy() { $q = $this->delete(); if($q == true) { $target_path = SITE_ROOT.DS.'public'.DS.$this->image_path(); $target_path_sm = SITE_ROOT.DS.'public'.DS.$this->image_path(TRUE); if(file_exists($target_path) && file_exists($target_path_sm)) { if(unlink($target_path_sm)) { return unlink($target_path) ? true : false; } else { return false; } } else { return false; } } else { return false; } } //Model klase public function delete() { if($this->id) { $q = Database::delete(static::TABLE, $this->id); if($q == true) { return true; } else { return false; } } return false; } //Database klase public static function delete($table,$id) { //te vajadzētu pārbaudīt vai ir saistītās tabulas, un izdzēst bērnu ierakstus if(self::query("DELETE FROM ".$table." WHERE id={$id}")) { return true; } else { return false; } } ?> Quote Link to comment Share on other sites More sharing options...
Maris-S Posted February 10, 2012 Report Share Posted February 10, 2012 (edited) To jādara datubāzes pusē. Ja db sastāv no InnoDB tabulām, tad izmanto relationships, kuriem on delete uzlikts cascade. Ja Tev ir MyIssam tabulas, tad taisi ar Triggeriem. Edited February 10, 2012 by Maris-S Quote Link to comment Share on other sites More sharing options...
codez Posted February 10, 2012 Report Share Posted February 10, 2012 (edited) http://dev.mysql.com...onstraints.html http://dev.mysql.com...ger-syntax.html Edited February 10, 2012 by codez Quote Link to comment Share on other sites More sharing options...
eT` Posted February 10, 2012 Author Report Share Posted February 10, 2012 Jā, ir InnoDB. Skaidrs. Paldies. Man relācijām bija ON DELETE NO ACTION Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.