[dev] [commit] r928 - phplib/models wwwbase/admin

automailer at dexonline.ro automailer at dexonline.ro
Wed Aug 14 17:21:16 EEST 2013


Author: cata
Date: Wed Aug 14 17:21:15 2013
New Revision: 928

Log:
Factor out some common code in various models and put it in BaseObject.

Modified:
   phplib/models/BaseObject.php
   phplib/models/LexemSource.php
   phplib/models/Meaning.php
   phplib/models/MeaningSource.php
   phplib/models/MeaningTagMap.php
   phplib/models/Synonym.php
   wwwbase/admin/lexemEdit.php

Modified: phplib/models/BaseObject.php
==============================================================================
--- phplib/models/BaseObject.php	Wed Aug 14 16:40:58 2013	(r927)
+++ phplib/models/BaseObject.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -35,6 +35,43 @@
     }
     return parent::save();
   }
+
+  /**
+   * Saves a list of objects sharing a common foreign key. Reuses existing table rows:
+   *   - deletes extra rows if the list is shrinking
+   *   - creates extra rows if the list if growing
+   * Example: if a user edits a Lexem and adds/removes LexemSources, then we can save the new list of LexemSources with
+   *   LexemSource::updateList(array('lexemId' => $lexem->id), 'sourceId', $sourceIds);
+   **/
+  static function updateList($filters, $field, $newValues) {
+    // Select the existing rows
+    $query = Model::factory(get_called_class());
+    foreach ($filters as $k => $v) {
+      $query = $query->where($k, $v);
+    };
+    $old = $query->find_many();
+
+    // Create new rows as needed
+    while (count($old) < count($newValues)) {
+      $old[] = Model::factory(get_called_class())->create();
+    }
+
+    // Delete rows we no longer need
+    while (count($old) > count($newValues)) {
+      $dead = array_pop($old);
+      $dead->delete();
+    }
+
+    // Populate data in the remaining rows
+    foreach ($newValues as $i => $newValue) {
+      $old[$i]->$field = $newValue;
+      foreach ($filters as $k => $v) {
+        $old[$i]->$k = $v;
+      }
+      $old[$i]->save();
+    }
+    
+  }
 }
 
 ?>

Modified: phplib/models/LexemSource.php
==============================================================================
--- phplib/models/LexemSource.php	Wed Aug 14 16:40:58 2013	(r927)
+++ phplib/models/LexemSource.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -13,22 +13,6 @@
     return $results;
   }
 
-  public static function update($lexemId, $sourceIds) {
-    $lss = self::get_all_by_lexemId($lexemId);
-    while (count($lss) < count($sourceIds)) {
-      $lss[] = Model::factory('LexemSource')->create();
-    }
-    while (count($lss) > count($sourceIds)) {
-      $deadLs = array_pop($lss);
-      $deadLs->delete();
-    }
-    foreach ($sourceIds as $i => $sourceId) {
-      $lss[$i]->lexemId = $lexemId;
-      $lss[$i]->sourceId = $sourceId;
-      $lss[$i]->save();
-    }
-  }
-
   public static function deleteByLexemId($lexemId) {
     $meanings = self::get_all_by_lexemId($lexemId);
     foreach ($meanings as $m) {

Modified: phplib/models/Meaning.php
==============================================================================
--- phplib/models/Meaning.php	Wed Aug 14 16:40:58 2013	(r927)
+++ phplib/models/Meaning.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -106,13 +106,13 @@
       $meaningStack[$tuple->level] = $m->id;
 
       $sourceIds = StringUtil::explode(',', $tuple->sourceIds);
-      MeaningSource::updateMeaningSources($m->id, $sourceIds);
+      MeaningSource::updateList(array('meaningId' => $m->id), 'sourceId', $sourceIds);
       $meaningTagIds = StringUtil::explode(',', $tuple->meaningTagIds);
-      MeaningTagMap::updateMeaningTags($m->id, $meaningTagIds);
+      MeaningTagMap::updateList(array('meaningId' => $m->id), 'meaningTagId', $meaningTagIds);
       $synonymIds = StringUtil::explode(',', $tuple->synonymIds);
-      Synonym::updateList($m->id, $synonymIds, Synonym::TYPE_SYNONYM);
+      Synonym::updateList(array('meaningId' => $m->id, 'type' => Synonym::TYPE_SYNONYM), 'lexemId', $synonymIds);
       $antonymIds = StringUtil::explode(',', $tuple->antonymIds);
-      Synonym::updateList($m->id, $antonymIds, Synonym::TYPE_ANTONYM);
+      Synonym::updateList(array('meaningId' => $m->id, 'type' => Synonym::TYPE_ANTONYM), 'lexemId', $antonymIds);
       $seenMeaningIds[] = $m->id;
     }
     self::deleteNotInSet($seenMeaningIds, $lexem->id);

Modified: phplib/models/MeaningSource.php
==============================================================================
--- phplib/models/MeaningSource.php	Wed Aug 14 16:40:58 2013	(r927)
+++ phplib/models/MeaningSource.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -10,22 +10,6 @@
       ->where('MeaningSource.meaningId', $meaningId)->find_many();
   }
 
-  static function updateMeaningSources($meaningId, $sourceIds) {
-    $mss = self::get_all_by_meaningId($meaningId);
-    while (count($mss) < count($sourceIds)) {
-      $mss[] = Model::factory('MeaningSource')->create();
-    }
-    while (count($mss) > count($sourceIds)) {
-      $deadMs = array_pop($mss);
-      $deadMs->delete();
-    }
-    foreach ($sourceIds as $i => $sourceId) {
-      $mss[$i]->meaningId = $meaningId;
-      $mss[$i]->sourceId = $sourceId;
-      $mss[$i]->save();
-    }
-  }
-
   public static function deleteByMeaningId($meaningId) {
     $mss = self::get_all_by_meaningId($meaningId);
     foreach ($mss as $ms) {

Modified: phplib/models/MeaningTagMap.php
==============================================================================
--- phplib/models/MeaningTagMap.php	Wed Aug 14 16:40:58 2013	(r927)
+++ phplib/models/MeaningTagMap.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -3,22 +3,6 @@
 class MeaningTagMap extends BaseObject implements DatedObject {
   public static $_table = 'MeaningTagMap';
 
-  static function updateMeaningTags($meaningId, $tagIds) {
-    $mtms = self::get_all_by_meaningId($meaningId);
-    while (count($mtms) < count($tagIds)) {
-      $mtms[] = Model::factory('MeaningTagMap')->create();
-    }
-    while (count($mtms) > count($tagIds)) {
-      $deadMtm = array_pop($mtms);
-      $deadMtm->delete();
-    }
-    foreach ($tagIds as $i => $tagId) {
-      $mtms[$i]->meaningId = $meaningId;
-      $mtms[$i]->meaningTagId = $tagId;
-      $mtms[$i]->save();
-    }
-  }
-
   public static function deleteByMeaningId($meaningId) {
     $mtms = self::get_all_by_meaningId($meaningId);
     foreach ($mtms as $mtm) {

Modified: phplib/models/Synonym.php
==============================================================================
--- phplib/models/Synonym.php	Wed Aug 14 16:40:58 2013	(r927)
+++ phplib/models/Synonym.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -15,23 +15,6 @@
       ->find_many();
   }
 
-  static function updateList($meaningId, $synonymIds, $type) {
-    $synonyms = Model::factory('Synonym')->where('meaningId', $meaningId)->where('type', $type)->find_many();
-    while (count($synonyms) < count($synonymIds)) {
-      $synonyms[] = Model::factory('Synonym')->create();
-    }
-    while (count($synonyms) > count($synonymIds)) {
-      $deadSynonym = array_pop($synonyms);
-      $deadSynonym->delete();
-    }
-    foreach ($synonymIds as $i => $lexemId) {
-      $synonyms[$i]->meaningId = $meaningId;
-      $synonyms[$i]->lexemId = $lexemId;
-      $synonyms[$i]->type = $type;
-      $synonyms[$i]->save();
-    }
-  }
-
   public static function deleteByMeaningId($meaningId) {
     $synonyms = self::get_all_by_meaningId($meaningId);
     foreach ($synonyms as $s) {

Modified: wwwbase/admin/lexemEdit.php
==============================================================================
--- wwwbase/admin/lexemEdit.php	Wed Aug 14 16:40:58 2013	(r927)
+++ wwwbase/admin/lexemEdit.php	Wed Aug 14 17:21:15 2013	(r928)
@@ -129,7 +129,7 @@
         $lexem->deleteLongInfinitive();
       }
       $lexem->save();
-      LexemSource::update($lexem->id, $lexemSourceIds);
+      LexemSource::updateList(array('lexemId' => $lexem->id), 'sourceId', $lexemSourceIds);
       $lexem->regenerateParadigm(); // This generates AND saves the paradigm
 
       log_userLog("Edited lexem {$lexem->id} ({$lexem->form})");


More information about the Dev mailing list