src/Charcoal/Admin/Script/Object/Table/AlterPrimaryKeyScript.php
protected function prepareProperties($oldKey, $newKey, &$oldProp = null, &$newProp = null)
    {
        $model  = $this->targetModel();
        $source = $model->source();

        /**
         * Either:
         * - TRUE if the $oldKey exists in the model's datasource.
         * - FALSE if the $oldKey does NOT exist in the model's datasource.
         * - NULL if the $oldKey does NOT exist in the model's datasource and is different from $newKey.
         *
         * @var boolean|null
         */
        $oldKeyExists = null;

        if ($this->isPrimaryKeyDifferent()) {
            $newProp = $model->property($newKey)->setAllowNull(false);
            $oldProp = clone $newProp;
            $oldProp->setIdent($oldKey);
        } else {
            $oldKeyExists = false;

            $oldProp = $model->property($oldKey)->setAllowNull(false);
            $newProp = clone $oldProp;
            $newProp->setIdent($newKey);
        }

        $sql  = strtr(
            'SHOW COLUMNS FROM `%table`',
            [
                '%table' => $source->table(),
            ]
        );
        $cols = $source->db()->query($sql, PDO::FETCH_ASSOC);
        foreach ($cols as $col) {
            if ($col['Field'] !== $oldKey) {
                continue;
            }

            $oldKeyExists = true;

            if (!$this->quiet()) {
                $this->climate()->comment(
                    sprintf('Evaluating the current `%s` column.', $oldKey)
                );
            }

            if (preg_match('~\bINT\(?(?:$|\b)~i', $col['Type'])) {
                $oldProp->setMode(IdProperty::MODE_AUTO_INCREMENT);
            } elseif (preg_match('~(?:^|\b)(?:VAR)?CHAR\(13\)(?:$|\b)~i', $col['Type'])) {
                $oldProp->setMode(IdProperty::MODE_UNIQID);
            } elseif (preg_match('~(?:^|\b)(?:VAR)?CHAR\(36\)(?:$|\b)~i', $col['Type'])) {
                $oldProp->setMode(IdProperty::MODE_UUID);
            } else {
                $oldProp->setMode(IdProperty::MODE_CUSTOM);
            }

            break;
        }

        if (!$oldKeyExists) {
            throw new RuntimeException(
                sprintf(
                    'The model [%1$s] does not have the target field [%2$s]',
                    get_class($model),
                    $oldKey
                )
            );
        }

        return [
            'old' => $oldProp,
            'new' => $newProp,
        ];
    }
protected function prepareProperties($oldKey, $newKey, &$oldProp = null, &$newProp = null)
    {
        $model  = $this->targetModel();
        $source = $model->source();

        /**
         * Either:
         * - TRUE if the $oldKey exists in the model's datasource.
         * - FALSE if the $oldKey does NOT exist in the model's datasource.
         * - NULL if the $oldKey does NOT exist in the model's datasource and is different from $newKey.
         *
         * @var boolean|null
         */
        $oldKeyExists = null;

        if ($this->isPrimaryKeyDifferent()) {
            $newProp = $model->property($newKey)->setAllowNull(false);
            $oldProp = clone $newProp;
            $oldProp->setIdent($oldKey);
        } else {
            $oldKeyExists = false;

            $oldProp = $model->property($oldKey)->setAllowNull(false);
            $newProp = clone $oldProp;
            $newProp->setIdent($newKey);
        }

        $sql  = strtr(
            'SHOW COLUMNS FROM `%table`',
            [
                '%table' => $source->table(),
            ]
        );
        $cols = $source->db()->query($sql, PDO::FETCH_ASSOC);
        foreach ($cols as $col) {
            if ($col['Field'] !== $oldKey) {
                continue;
            }

            $oldKeyExists = true;

            if (!$this->quiet()) {
                $this->climate()->comment(
                    sprintf('Evaluating the current `%s` column.', $oldKey)
                );
            }

            if (preg_match('~\bINT\(?(?:$|\b)~i', $col['Type'])) {
                $oldProp->setMode(IdProperty::MODE_AUTO_INCREMENT);
            } elseif (preg_match('~(?:^|\b)(?:VAR)?CHAR\(13\)(?:$|\b)~i', $col['Type'])) {
                $oldProp->setMode(IdProperty::MODE_UNIQID);
            } elseif (preg_match('~(?:^|\b)(?:VAR)?CHAR\(36\)(?:$|\b)~i', $col['Type'])) {
                $oldProp->setMode(IdProperty::MODE_UUID);
            } else {
                $oldProp->setMode(IdProperty::MODE_CUSTOM);
            }

            break;
        }

        if (!$oldKeyExists) {
            throw new RuntimeException(
                sprintf(
                    'The model [%1$s] does not have the target field [%2$s]',
                    get_class($model),
                    $oldKey
                )
            );
        }

        return [
            'old' => $oldProp,
            'new' => $newProp,
        ];
    }
protected function prepareProperties ( $ oldKey , $ newKey , & $ oldProp = null , & $ newProp = null ) { $ model = $ this -> targetModel ( ) ; $ source = $ model -> source ( ) ; /**
         * Either:
         * - TRUE if the $oldKey exists in the model's datasource.
         * - FALSE if the $oldKey does NOT exist in the model's datasource.
         * - NULL if the $oldKey does NOT exist in the model's datasource and is different from $newKey.
         *
         * @var boolean|null
         */ $ oldKeyExists = null ; if ( $ this -> isPrimaryKeyDifferent ( ) ) { $ newProp = $ model -> property ( $ newKey ) -> setAllowNull ( false ) ; $ oldProp = clone $ newProp ; $ oldProp -> setIdent ( $ oldKey ) ; } else { $ oldKeyExists = false ; $ oldProp = $ model -> property ( $ oldKey ) -> setAllowNull ( false ) ; $ newProp = clone $ oldProp ; $ newProp -> setIdent ( $ newKey ) ; } $ sql = strtr ( 'SHOW COLUMNS FROM `%table`' , [ '%table' => $ source -> table ( ) , ] ) ; $ cols = $ source -> db ( ) -> query ( $ sql , PDO :: FETCH_ASSOC ) ; foreach ( $ cols as $ col ) { if ( $ col [ 'Field' ] !== $ oldKey ) { continue ; } $ oldKeyExists = true ; if ( ! $ this -> quiet ( ) ) { $ this -> climate ( ) -> comment ( sprintf ( 'Evaluating the current `%s` column.' , $ oldKey ) ) ; } if ( preg_match ( '~\bINT\(?(?:$|\b)~i' , $ col [ 'Type' ] ) ) { $ oldProp -> setMode ( IdProperty :: MODE_AUTO_INCREMENT ) ; } elseif ( preg_match ( '~(?:^|\b)(?:VAR)?CHAR\(13\)(?:$|\b)~i' , $ col [ 'Type' ] ) ) { $ oldProp -> setMode ( IdProperty :: MODE_UNIQID ) ; } elseif ( preg_match ( '~(?:^|\b)(?:VAR)?CHAR\(36\)(?:$|\b)~i' , $ col [ 'Type' ] ) ) { $ oldProp -> setMode ( IdProperty :: MODE_UUID ) ; } else { $ oldProp -> setMode ( IdProperty :: MODE_CUSTOM ) ; } break ; } if ( ! $ oldKeyExists ) { throw new RuntimeException ( sprintf ( 'The model [%1$s] does not have the target field [%2$s]' , get_class ( $ model ) , $ oldKey ) ) ; } return [ 'old' => $ oldProp , 'new' => $ newProp , ] ; }
Retrieve the old and new ID properties.

@param  string          $oldKey  The previous key.
@param  string          $newKey  The new key.
@param  IdProperty|null $oldProp If provided, then it is filled with an instance of IdProperty.
@param  IdProperty|null $newProp If provided, then it is filled with an instance of IdProperty.
@throws RuntimeException If the $oldKey does not exist.
@return IdProperty[]
Retrieve the old and new ID properties .