saveAll() public method

Saves all related items. You can use it to touch items as well: every item being saved causes the modified_by and modified_on fields to be changed automatically, thanks to the DataModel's magic.
public saveAll ( )
Exemplo n.º 1
0
 /**
  * Saves all related items. For many-to-many relations there are two things we have to do:
  * 1. Save all related items; and
  * 2. Overwrite the pivot table data with the new associations
  */
 public function saveAll()
 {
     // Save all related items
     parent::saveAll();
     $this->saveRelations();
 }
Exemplo n.º 2
0
 /**
  * Saves all related items. For many-to-many relations there are two things we have to do:
  * 1. Save all related items; and
  * 2. Overwrite the pivot table data with the new associations
  */
 public function saveAll()
 {
     // Save all related items
     parent::saveAll();
     // Get all the new keys
     $newKeys = array();
     if ($this->data instanceof DataModel\Collection) {
         foreach ($this->data as $item) {
             if ($item instanceof DataModel) {
                 $newKeys[] = $item->getId();
             } elseif (!is_object($item)) {
                 $newKeys[] = $item;
             }
         }
     }
     $newKeys = array_unique($newKeys);
     $db = $this->parentModel->getDbo();
     $localKeyValue = $this->parentModel->getFieldValue($this->localKey);
     // Kill all existing relations in the pivot table
     $query = $db->getQuery(true)->delete($db->qn($this->pivotTable))->where($db->qn($this->pivotLocalKey) . ' = ' . $db->q($localKeyValue));
     $db->setQuery($query);
     $db->execute();
     // Write the new relations to the database
     $protoQuery = $db->getQuery(true)->insert($db->qn($this->pivotTable))->columns(array($db->qn($this->pivotLocalKey), $db->qn($this->pivotForeignKey)));
     $i = 0;
     $query = null;
     foreach ($newKeys as $key) {
         $i++;
         if (is_null($query)) {
             $query = clone $protoQuery;
         }
         $query->values($db->q($localKeyValue) . ', ' . $db->q($key));
         if ($i % 50 == 0) {
             $db->setQuery($query);
             $db->execute();
             $query = null;
         }
     }
     if (!is_null($query)) {
         $db->setQuery($query);
         $db->execute();
     }
 }