Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Potential issue saving a reorder when the model relies on a different database outside of the set default #5656

Closed
SilverFocus opened this issue Sep 11, 2024 · 4 comments · Fixed by #5660
Assignees

Comments

@SilverFocus
Copy link

Bug report

What I did

I clicked to reorder items, then clicked save on the reorder page.

What I expected to happen

The reorder to save properly.

What happened

Upon saving:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'franchise_local.staff_bios' doesn't exist (Connection: mysql, SQL: UPDATE staff_bios SET parent_id = CASE WHEN id = 97 THEN ? WHEN id = 39 THEN ? WHEN id = 37 THEN ? ELSE parent_id END WHERE id IN (37,39,97))

I have 2 databases:

  • a global one, we will call it franchise_global
  • a local one, we will call it franchise_local (with 50+ variations of this same table to denote different franchise locations, it is the default database for each franchise)

The reorder is attempting to save to the wrong database and table entirely, it should be franchise_global.staff_bios - this was not an issue for me at all until I migrated over to Backpack 6.

I have a connection property set on my staff_bios model that specifies the global database and any other CRUD actions work perfectly fine on this model. It seems to be ignoring the connection property completely when saving the reorder, which is reflected by how it attempts to save it to franchise_local.staff_bios - which is a table that does not exist in that local database. I have another model for promotions, also uses reorder, that is also having this same exact issue.

Additionally, I have another model with rorder for nav menus that exist in these local databases, and the reorder works 100% fine here.

I do not use github a ton, so I apologize if the format is messy in anyway or this does not make sense.

CRUD:

class StaffBioCrudController extends CrudController
{
    use ListOperation;
    use CreateOperation;
    use UpdateOperation;
    use DeleteOperation;
    use ReorderOperation;
    use ShowOperation;

    public function setup()
    {
        $this->crud->setModel(StaffBio::class);
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/staffbio');
        $this->crud->setEntityNameStrings('Staff Bio', 'Staff Bios');
        $this->crud->addClause('where', 'control_number', '=', config('local.control_number'));
        $this->crud->orderBy('lft', 'ASC');

        $this->crud->operation('list', function () {
            (...)
        });

        $this->crud->operation(['create', 'update'], function () {
            (...)
        });

        $this->crud->operation('show', function () {
            (...)
        });

        $this->crud->operation('reorder', function () {
            $this->crud->set('reorder.label', 'full_name_and_team');
            $this->crud->set('reorder.max_level', 1);
        });
    }
}

Model:

class StaffBio extends Model
{
    use CrudTrait;
    use SoftDeletes;
    use RevisionableTrait;

    protected $connection = 'globals';

    protected $table = 'staff_bios';

    protected $primaryKey = 'id';

    protected $fakeColumns = ['extras'];

    protected $fillable = [
        'control_number',
        'first_name',
        'last_name',
        'team',
        'job_title',
        'image',
        'bio',
        'approval',
        'updated_by',
        'parent_id',
        'lft',
        'rgt',
        'depth',
        'extras',
    ];

    protected $revisionEnabled = true;

    protected $revisionCreationsEnabled = true;
    
    protected $casts = [
        'approval' => 'boolean',
    ];

    /*
    |--------------------------------------------------------------------------
    | ACCESSORS
    |--------------------------------------------------------------------------
    */
    protected $revisionFormattedFields = [
        'approval'     => 'boolean:No|Yes',
        'created_at'   => 'datetime:m/d/Y g:i A',
        'updated_at'   => 'datetime:m/d/Y g:i A',
        'deleted_at' => 'isEmpty:Active|Deleted'
    ];

    public function getFullNameAndTeamAttribute(): string
    {
        $string = $this->first_name . ' ' . $this->last_name;
        if ($this->team) {
            $string .= ' (' . $this->team . ')';
        }
        return $string;
    }
}

What I've already tried to fix it

If there is an issue, I think it might reside in updateTreeOrder() in backpack/crud/src/app/Library/CrudPanel/Traits/Reorder.php where $query is being set with $this->model->getTable()

Ultimately, I want to know if it's a 'me' issue in forgetting to change something when migrating from backpack 5 to 6 , because I'm stumped at this point.

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Yes, this issue started happening after I moved from Backpack 5 to Backpack 6.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

8.2.22

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, readline, Reflection, SPL, session, standard, bcmath, calendar, ctype, curl, dom, fileinfo, ftp, gd, iconv, imap, mbstring, mysqlnd, PDO, Phar, posix, SimpleXML, sockets, sqlite3, tokenizer, xml, xmlwriter, xsl, zip, mysqli, pdo_mysql, pdo_sqlite, xmlreader

LARAVEL VERSION:

11.22.0.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.6
backpack/crud: 6.7.34
backpack/filemanager: 3.0.8
backpack/generators: v4.0.6
backpack/menucrud: v4.0.2
backpack/newscrud: 5.1.0
backpack/pagemanager: 3.3.1
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.15
backpack/revise-operation: 2.0.0
backpack/theme-coreuiv2: 1.2.5

Copy link

welcome bot commented Sep 11, 2024

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use GitHub Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

  • Bug Reports, Feature Requests - GitHub Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@karandatwani92
Copy link
Contributor

Hey @SilverFocus

Sorry for the inconvenience. I'm assigning the issue to my colleague to fix it. Thanks for reporting.

@pxpm What I did to test:

  • I added protected $connection = 'demo_db2'; to the model.
  • It loads the list from the 2nd DB, but the recorder doesn't save and shows me a "Success" notification.
  • I did not receive any error as reported.

@SilverFocus
Copy link
Author

Hello - a follow-up here!

I noticed maybe one other potential issue some people might hit. Casting the Primary Key (PK) as an int in Reorder.php will also cause the reorder to fail if the PK is not actually a INT for that database column. I say this because a PK on one of my reorderable models is actually a varchar and the reorder was still breaking.

Changing line 32 from: $item[$primaryKey] = (int) $item['item_id']; to $item[$primaryKey] = $item['item_id']; has fixed this for me.

@pxpm
Copy link
Contributor

pxpm commented Sep 18, 2024

Thanks @SilverFocus for the heads up 👍

I've just merged the fix and will be available in the next patch release. 🙏

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants