public function saveColl($data, $extraData = array(), $sort = false)
    {
        if (!is_array($data)) {
            return $this;
        }

        // 1. Uses primary key as data index
        $newData = array();
        foreach ($this->data as $key => $record) {
            unset($this->data[$key]);
            // Ignore default data
            if ($record instanceof $this) {
                $newData[$record[$this->primaryKey]] = $record;
            }
        }
        $this->data = $newData;

        // 2. Removes empty rows from data
        foreach ($data as $index => $row) {
            if (!array_filter($row)) {
                unset($data[$index]);
            }
        }

        // 3. Removes missing rows
        $existIds = array();
        foreach ($data as $row) {
            if (isset($row[$this->primaryKey]) && $row[$this->primaryKey] !== null) {
                $existIds[] = $row[$this->primaryKey];
            }
        }
        /** @var $record Record */
        foreach ($this->data as $key => $record) {
            if (!in_array($record[$this->primaryKey], $existIds)) {
                $record->destroy();
                unset($this->data[$key]);
            }
        }

        // 4. Merges existing rows or create new rows
        foreach ($data as $index => $row) {
            if ($sort) {
                $row[$sort] = $index;
            }
            if (isset($row[$this->primaryKey]) && isset($this->data[$row[$this->primaryKey]])) {
                $this->data[$row[$this->primaryKey]]->fromArray($row);
            } else {
                $this[] = $this->db($this->table)->fromArray($extraData + $row);
            }
        }

        // 5. Save and return
        return $this->save();
    }
public function saveColl($data, $extraData = array(), $sort = false)
    {
        if (!is_array($data)) {
            return $this;
        }

        // 1. Uses primary key as data index
        $newData = array();
        foreach ($this->data as $key => $record) {
            unset($this->data[$key]);
            // Ignore default data
            if ($record instanceof $this) {
                $newData[$record[$this->primaryKey]] = $record;
            }
        }
        $this->data = $newData;

        // 2. Removes empty rows from data
        foreach ($data as $index => $row) {
            if (!array_filter($row)) {
                unset($data[$index]);
            }
        }

        // 3. Removes missing rows
        $existIds = array();
        foreach ($data as $row) {
            if (isset($row[$this->primaryKey]) && $row[$this->primaryKey] !== null) {
                $existIds[] = $row[$this->primaryKey];
            }
        }
        /** @var $record Record */
        foreach ($this->data as $key => $record) {
            if (!in_array($record[$this->primaryKey], $existIds)) {
                $record->destroy();
                unset($this->data[$key]);
            }
        }

        // 4. Merges existing rows or create new rows
        foreach ($data as $index => $row) {
            if ($sort) {
                $row[$sort] = $index;
            }
            if (isset($row[$this->primaryKey]) && isset($this->data[$row[$this->primaryKey]])) {
                $this->data[$row[$this->primaryKey]]->fromArray($row);
            } else {
                $this[] = $this->db($this->table)->fromArray($extraData + $row);
            }
        }

        // 5. Save and return
        return $this->save();
    }
public function saveColl ( $ data , $ extraData = array ( ) , $ sort = false ) { if ( ! is_array ( $ data ) ) { return $ this ; } // 1. Uses primary key as data index $ newData = array ( ) ; foreach ( $ this -> data as $ key => $ record ) { unset ( $ this -> data [ $ key ] ) ; // Ignore default data if ( $ record instanceof $ this ) { $ newData [ $ record [ $ this -> primaryKey ] ] = $ record ; } } $ this -> data = $ newData ; // 2. Removes empty rows from data foreach ( $ data as $ index => $ row ) { if ( ! array_filter ( $ row ) ) { unset ( $ data [ $ index ] ) ; } } // 3. Removes missing rows $ existIds = array ( ) ; foreach ( $ data as $ row ) { if ( isset ( $ row [ $ this -> primaryKey ] ) && $ row [ $ this -> primaryKey ] !== null ) { $ existIds [ ] = $ row [ $ this -> primaryKey ] ; } } /** @var $record Record */ foreach ( $ this -> data as $ key => $ record ) { if ( ! in_array ( $ record [ $ this -> primaryKey ] , $ existIds ) ) { $ record -> destroy ( ) ; unset ( $ this -> data [ $ key ] ) ; } } // 4. Merges existing rows or create new rows foreach ( $ data as $ index => $ row ) { if ( $ sort ) { $ row [ $ sort ] = $ index ; } if ( isset ( $ row [ $ this -> primaryKey ] ) && isset ( $ this -> data [ $ row [ $ this -> primaryKey ] ] ) ) { $ this -> data [ $ row [ $ this -> primaryKey ] ] -> fromArray ( $ row ) ; } else { $ this [ ] = $ this -> db ( $ this -> table ) -> fromArray ( $ extraData + $ row ) ; } } // 5. Save and return return $ this -> save ( ) ; }
Merges data into collection and save to database, including insert, update and delete

@param array $data A two-dimensional array
@param array $extraData The extra data for new rows
@param bool $sort
@return $this
Merges data into collection and save to database including insert update and delete