src/Charcoal/Admin/Script/Object/Table/AlterPrimaryKeyScript.php
protected function syncRelatedFields(
        IdProperty $newProp,
        PropertyField $newField,
        IdProperty $oldProp,
        PropertyField $oldField
    ) {
        unset($newProp, $oldProp, $oldField);

        $cli = $this->climate();
        if (!$this->quiet()) {
            $cli->br();
            $cli->comment('Syncing new IDs to related tables.');
        }

        $related = $cli->arguments->get('related_model');
        if (!$related) {
            $cli->br();
            $input = $cli->confirm('Are there any model(s) related to the target?');
            if (!$input->confirmed()) {
                return $this;
            }

            $related = $this->argOrInput('related_model');
        }
        $this->setRelatedModels($related);

        $target = $this->targetModel();
        $table  = $target->source()->table();
        foreach ($this->relatedModels() as $model) {
            $src = $model->source();
            $tbl = $src->table();
            $dbh = $src->db();

            $dbh->query(
                strtr(
                    'LOCK TABLES
                        `%relatedTable` AS a WRITE,
                        `%sourceTable` AS b WRITE',
                    [
                        '%relatedTable' => $tbl,
                        '%sourceTable'  => $table,
                    ]
                )
            );

            $sql = strtr(
                'UPDATE `%relatedTable` AS a '.
                    'JOIN `%sourceTable` AS b ON a.`%prop` = b.`%oldKey` '.
                    'SET a.`%prop` = b.`%newKey`',
                [
                    '%relatedTable' => $tbl,
                    '%prop'         => $this->relatedProperties[$model->objType()],
                    '%sourceTable'  => $table,
                    '%newKey'       => $newField->ident(),
                    '%oldKey'       => $target->key(),
                ]
            );
            $dbh->query($sql);

            $dbh->query('UNLOCK TABLES');
        }

        return $this;
    }
protected function syncRelatedFields(
        IdProperty $newProp,
        PropertyField $newField,
        IdProperty $oldProp,
        PropertyField $oldField
    ) {
        unset($newProp, $oldProp, $oldField);

        $cli = $this->climate();
        if (!$this->quiet()) {
            $cli->br();
            $cli->comment('Syncing new IDs to related tables.');
        }

        $related = $cli->arguments->get('related_model');
        if (!$related) {
            $cli->br();
            $input = $cli->confirm('Are there any model(s) related to the target?');
            if (!$input->confirmed()) {
                return $this;
            }

            $related = $this->argOrInput('related_model');
        }
        $this->setRelatedModels($related);

        $target = $this->targetModel();
        $table  = $target->source()->table();
        foreach ($this->relatedModels() as $model) {
            $src = $model->source();
            $tbl = $src->table();
            $dbh = $src->db();

            $dbh->query(
                strtr(
                    'LOCK TABLES
                        `%relatedTable` AS a WRITE,
                        `%sourceTable` AS b WRITE',
                    [
                        '%relatedTable' => $tbl,
                        '%sourceTable'  => $table,
                    ]
                )
            );

            $sql = strtr(
                'UPDATE `%relatedTable` AS a '.
                    'JOIN `%sourceTable` AS b ON a.`%prop` = b.`%oldKey` '.
                    'SET a.`%prop` = b.`%newKey`',
                [
                    '%relatedTable' => $tbl,
                    '%prop'         => $this->relatedProperties[$model->objType()],
                    '%sourceTable'  => $table,
                    '%newKey'       => $newField->ident(),
                    '%oldKey'       => $target->key(),
                ]
            );
            $dbh->query($sql);

            $dbh->query('UNLOCK TABLES');
        }

        return $this;
    }
protected function syncRelatedFields ( IdProperty $ newProp , PropertyField $ newField , IdProperty $ oldProp , PropertyField $ oldField ) { unset ( $ newProp , $ oldProp , $ oldField ) ; $ cli = $ this -> climate ( ) ; if ( ! $ this -> quiet ( ) ) { $ cli -> br ( ) ; $ cli -> comment ( 'Syncing new IDs to related tables.' ) ; } $ related = $ cli -> arguments -> get ( 'related_model' ) ; if ( ! $ related ) { $ cli -> br ( ) ; $ input = $ cli -> confirm ( 'Are there any model(s) related to the target?' ) ; if ( ! $ input -> confirmed ( ) ) { return $ this ; } $ related = $ this -> argOrInput ( 'related_model' ) ; } $ this -> setRelatedModels ( $ related ) ; $ target = $ this -> targetModel ( ) ; $ table = $ target -> source ( ) -> table ( ) ; foreach ( $ this -> relatedModels ( ) as $ model ) { $ src = $ model -> source ( ) ; $ tbl = $ src -> table ( ) ; $ dbh = $ src -> db ( ) ; $ dbh -> query ( strtr ( 'LOCK TABLES
                        `%relatedTable` AS a WRITE,
                        `%sourceTable` AS b WRITE' , [ '%relatedTable' => $ tbl , '%sourceTable' => $ table , ] ) ) ; $ sql = strtr ( 'UPDATE `%relatedTable` AS a ' . 'JOIN `%sourceTable` AS b ON a.`%prop` = b.`%oldKey` ' . 'SET a.`%prop` = b.`%newKey`' , [ '%relatedTable' => $ tbl , '%prop' => $ this -> relatedProperties [ $ model -> objType ( ) ] , '%sourceTable' => $ table , '%newKey' => $ newField -> ident ( ) , '%oldKey' => $ target -> key ( ) , ] ) ; $ dbh -> query ( $ sql ) ; $ dbh -> query ( 'UNLOCK TABLES' ) ; } return $ this ; }
Sync the new primary keys to related models.

@param  IdProperty    $newProp  The new ID property.
@param  PropertyField $newField The new ID field.
@param  IdProperty    $oldProp  The previous ID property.
@param  PropertyField $oldField The previous ID field.
@throws InvalidArgumentException If the new property does not implement the proper mode.
@return self
Sync the new primary keys to related models .