Пример #1
0
 /**
  * Public constructor. Initialises the relation.
  *
  * @param   DataModel $parentModel       The data model we are attached to
  * @param   string    $foreignModelName  The name of the foreign key's model in the format "modelName@com_something"
  * @param   string    $localKey          The local table key for this relation, default: parentModel's ID field name
  * @param   string    $foreignKey        The foreign key for this relation, default: parentModel's ID field name
  * @param   string    $pivotTable        IGNORED
  * @param   string    $pivotLocalKey     IGNORED
  * @param   string    $pivotForeignKey   IGNORED
  */
 public function __construct(DataModel $parentModel, $foreignModelName, $localKey = null, $foreignKey = null, $pivotTable = null, $pivotLocalKey = null, $pivotForeignKey = null)
 {
     parent::__construct($parentModel, $foreignModelName, $localKey, $foreignKey, $pivotTable, $pivotLocalKey, $pivotForeignKey);
     if (empty($this->localKey)) {
         $this->localKey = $parentModel->getIdFieldName();
     }
     if (empty($this->foreignKey)) {
         $this->foreignKey = $this->localKey;
     }
 }
Пример #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();
     $this->saveRelations();
 }
Пример #3
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();
     }
 }