ImportModel::_assignIDs PHP Method

_assignIDs() protected method

protected _assignIDs ( $TableName, null $PrimaryKey = null, null $SecondaryKey = null ) : boolean | void
$TableName
$PrimaryKey null
$SecondaryKey null
return boolean | void
    protected function _assignIDs($TableName, $PrimaryKey = null, $SecondaryKey = null)
    {
        if (!array_key_exists($TableName, $this->Tables())) {
            return;
        }
        if (!$PrimaryKey) {
            $PrimaryKey = $TableName . 'ID';
        }
        // Assign existing IDs.
        if ($SecondaryKey) {
            $Sql = "update :_z{$TableName} i\n            join :_{$TableName} t\n              on t.{$SecondaryKey} = i.{$SecondaryKey}\n            set i._NewID = t.{$PrimaryKey}, i._Action = 'Update'";
            $this->query($Sql);
        }
        // Get new IDs.
        $MaxID = $this->query("select max({$PrimaryKey}) as MaxID from :_{$TableName}")->value('MaxID', 0);
        $MinID = $this->query("select min({$PrimaryKey}) as MinID from :_z{$TableName} where _NewID is null")->value('MinID', null);
        if (is_null($MinID)) {
            //$this->Timer->Split('No more IDs to update');
            // No more IDs to update.
            return true;
        }
        if ($MaxID == 0) {
            $IDInc = 0;
        } else {
            $IDInc = $MaxID - $MinID + self::ID_PADDING;
        }
        $Sql = "update :_z{$TableName} i\n         set i._NewID = i.{$PrimaryKey} + {$IDInc}, i._Action = 'Insert'\n         where i._NewID is null";
        $this->query($Sql);
    }