protected function pruneRelationships()
    {
        $cli   = $this->climate();
        $ask   = $this->interactive();
        $dry   = $this->dryRun();
        $verb  = $this->verbose();
        $mucho = ($dry || $verb);

        $attach = $this->modelFactory()->get(Attachment::class);
        $pivot  = $this->modelFactory()->get(Join::class);
        $source = $attach->source();
        $db     = $source->db();

        $defaultBinds = [
            '%pivotTable' => $pivot->source()->table(),
            '%sourceType' => 'object_type',
            '%sourceId'   => 'object_id',
        ];

        $sql = 'SELECT DISTINCT `%sourceType` FROM `%pivotTable`;';
        $rows = $db->query(strtr($sql, $binds), PDO::FETCH_ASSOC);
        if ($rows->rowCount()) {
            error_log(get_called_class().'::'.__FUNCTION__);

            /** @todo Confirm each distinct source type */

            foreach ($rows as $row) {
                try {
                    $model = $this->modelFactory()->get($row['object_type']);
                } catch (Exception $e) {
                    unset($e);
                    $model = $row['object_type'];
                }

                if ($model instanceof ModelInterface) {
                    $sql = 'SELECT p.* FROM `%pivotTable` AS p
                            WHERE p.`%sourceType` = :objType AND p.`%sourceId` NOT IN (
                                SELECT o.`%objectKey` FROM `%objectTable` AS o
                            );';
                    $binds = array_merge($defaultBinds, [
                        '%objectTable' => $model->source()->table(),
                        '%objectKey'   => $model->key(),
                    ]);
                    $rows = $source->dbQuery(
                        strtr($sql, $binds),
                        [ 'objType' => $row['object_type'] ]
                    );

                    /** @todo Show found rows, confirm deletion of dead relationships */

                    /*
                    $sql = 'DELETE FROM `%pivotTable` AS p
                            WHERE p.`%sourceType` = :objType AND p.`%sourceId` NOT IN (
                                SELECT o.`%objectKey` FROM `%objectTable` AS o
                            );';
                    error_log('-- Delete Dead Objects: '.var_export($sql, true));
                    $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );
                    */
                } elseif (is_string($model)) {
                    $sql  = 'SELECT p.* FROM `%pivotTable` AS p WHERE p.`%sourceType` = :objType;';
                    $rows = $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );

                    /** @todo Explain missing model, confirm deletion of dead relationships */

                    /*
                    $sql = 'DELETE FROM `%pivotTable` WHERE `%sourceType` = :objType;';
                    error_log('-- Delete Dead Model: '.var_export($sql, true));
                    $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );
                    */
                }
            }

            /*
            if (!$this->describeCount(
                $this->total,
                '%d dead objects were found.',
                '%d dead object was found.',
                'All objects are associated!'
            )) {
                return $this;
            }
            */

            $this->conclude();
        }

        return $this;
    }
protected function pruneRelationships()
    {
        $cli   = $this->climate();
        $ask   = $this->interactive();
        $dry   = $this->dryRun();
        $verb  = $this->verbose();
        $mucho = ($dry || $verb);

        $attach = $this->modelFactory()->get(Attachment::class);
        $pivot  = $this->modelFactory()->get(Join::class);
        $source = $attach->source();
        $db     = $source->db();

        $defaultBinds = [
            '%pivotTable' => $pivot->source()->table(),
            '%sourceType' => 'object_type',
            '%sourceId'   => 'object_id',
        ];

        $sql = 'SELECT DISTINCT `%sourceType` FROM `%pivotTable`;';
        $rows = $db->query(strtr($sql, $binds), PDO::FETCH_ASSOC);
        if ($rows->rowCount()) {
            error_log(get_called_class().'::'.__FUNCTION__);

            /** @todo Confirm each distinct source type */

            foreach ($rows as $row) {
                try {
                    $model = $this->modelFactory()->get($row['object_type']);
                } catch (Exception $e) {
                    unset($e);
                    $model = $row['object_type'];
                }

                if ($model instanceof ModelInterface) {
                    $sql = 'SELECT p.* FROM `%pivotTable` AS p
                            WHERE p.`%sourceType` = :objType AND p.`%sourceId` NOT IN (
                                SELECT o.`%objectKey` FROM `%objectTable` AS o
                            );';
                    $binds = array_merge($defaultBinds, [
                        '%objectTable' => $model->source()->table(),
                        '%objectKey'   => $model->key(),
                    ]);
                    $rows = $source->dbQuery(
                        strtr($sql, $binds),
                        [ 'objType' => $row['object_type'] ]
                    );

                    /** @todo Show found rows, confirm deletion of dead relationships */

                    /*
                    $sql = 'DELETE FROM `%pivotTable` AS p
                            WHERE p.`%sourceType` = :objType AND p.`%sourceId` NOT IN (
                                SELECT o.`%objectKey` FROM `%objectTable` AS o
                            );';
                    error_log('-- Delete Dead Objects: '.var_export($sql, true));
                    $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );
                    */
                } elseif (is_string($model)) {
                    $sql  = 'SELECT p.* FROM `%pivotTable` AS p WHERE p.`%sourceType` = :objType;';
                    $rows = $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );

                    /** @todo Explain missing model, confirm deletion of dead relationships */

                    /*
                    $sql = 'DELETE FROM `%pivotTable` WHERE `%sourceType` = :objType;';
                    error_log('-- Delete Dead Model: '.var_export($sql, true));
                    $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );
                    */
                }
            }

            /*
            if (!$this->describeCount(
                $this->total,
                '%d dead objects were found.',
                '%d dead object was found.',
                'All objects are associated!'
            )) {
                return $this;
            }
            */

            $this->conclude();
        }

        return $this;
    }
protected function pruneRelationships ( ) { $ cli = $ this -> climate ( ) ; $ ask = $ this -> interactive ( ) ; $ dry = $ this -> dryRun ( ) ; $ verb = $ this -> verbose ( ) ; $ mucho = ( $ dry || $ verb ) ; $ attach = $ this -> modelFactory ( ) -> get ( Attachment :: class ) ; $ pivot = $ this -> modelFactory ( ) -> get ( Join :: class ) ; $ source = $ attach -> source ( ) ; $ db = $ source -> db ( ) ; $ defaultBinds = [ '%pivotTable' => $ pivot -> source ( ) -> table ( ) , '%sourceType' => 'object_type' , '%sourceId' => 'object_id' , ] ; $ sql = 'SELECT DISTINCT `%sourceType` FROM `%pivotTable`;' ; $ rows = $ db -> query ( strtr ( $ sql , $ binds ) , PDO :: FETCH_ASSOC ) ; if ( $ rows -> rowCount ( ) ) { error_log ( get_called_class ( ) . '::' . __FUNCTION__ ) ; /** @todo Confirm each distinct source type */ foreach ( $ rows as $ row ) { try { $ model = $ this -> modelFactory ( ) -> get ( $ row [ 'object_type' ] ) ; } catch ( Exception $ e ) { unset ( $ e ) ; $ model = $ row [ 'object_type' ] ; } if ( $ model instanceof ModelInterface ) { $ sql = 'SELECT p.* FROM `%pivotTable` AS p
                            WHERE p.`%sourceType` = :objType AND p.`%sourceId` NOT IN (
                                SELECT o.`%objectKey` FROM `%objectTable` AS o
                            );' ; $ binds = array_merge ( $ defaultBinds , [ '%objectTable' => $ model -> source ( ) -> table ( ) , '%objectKey' => $ model -> key ( ) , ] ) ; $ rows = $ source -> dbQuery ( strtr ( $ sql , $ binds ) , [ 'objType' => $ row [ 'object_type' ] ] ) ; /** @todo Show found rows, confirm deletion of dead relationships */ /*
                    $sql = 'DELETE FROM `%pivotTable` AS p
                            WHERE p.`%sourceType` = :objType AND p.`%sourceId` NOT IN (
                                SELECT o.`%objectKey` FROM `%objectTable` AS o
                            );';
                    error_log('-- Delete Dead Objects: '.var_export($sql, true));
                    $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );
                    */ } elseif ( is_string ( $ model ) ) { $ sql = 'SELECT p.* FROM `%pivotTable` AS p WHERE p.`%sourceType` = :objType;' ; $ rows = $ source -> dbQuery ( strtr ( $ sql , $ defaultBinds ) , [ 'objType' => $ model ] ) ; /** @todo Explain missing model, confirm deletion of dead relationships */ /*
                    $sql = 'DELETE FROM `%pivotTable` WHERE `%sourceType` = :objType;';
                    error_log('-- Delete Dead Model: '.var_export($sql, true));
                    $source->dbQuery(
                        strtr($sql, $defaultBinds),
                        [ 'objType' => $model ]
                    );
                    */ } } /*
            if (!$this->describeCount(
                $this->total,
                '%d dead objects were found.',
                '%d dead object was found.',
                'All objects are associated!'
            )) {
                return $this;
            }
            */ $ this -> conclude ( ) ; } return $ this ; }
Prune relationships of dead objects.

@return self
Prune relationships of dead objects .