CRUD icon indicating copy to clipboard operation
CRUD copied to clipboard

[Bug] Crud add Clause Bug in combination with join

Open mintUI9976 opened this issue 3 years ago • 1 comments

Bug report

What I did

add clause with join in other table

$this->crud->addClause(
            'join',
            'suppliers',
            function ($query) {
                $query->on('supplier-lists.supplier_id', '=', 'suppliers.id')
                    ->where('suppliers.trade', '=', TradeCalculationEnum::EK);
            }
        );

What I expected to happen

That the table view is referenced to another db table view

What happened

[2022-09-21 17:12:23] local.ERROR: SQLSTATE[42703]: Undefined column: 7 ERROR: column "supplier" does not exist LINE 1: ...liers"."id" and "suppliers"."trade" = $1 order by supplier-l... ^ HINT: Perhaps you meant to reference the column "supplier-lists.supplier_id". (SQL: select "supplier-lists".* from "supplier-lists" inner join "suppliers" on "supplier-lists"."supplier_id" = "suppliers"."id" and "suppliers"."trade" = EK order by supplier-lists.id DESC limit 25) {"userId":1,"exception":"[object] (Illuminate\\Database\\QueryException(code: 42703): SQLSTATE[42703]: Undefined column: 7 ERROR: column \"supplier\" does not exist LINE 1: ...liers\".\"id\" and \"suppliers\".\"trade\" = $1 order by supplier-l... ^ HINT: Perhaps you meant to reference the column \"supplier-lists.supplier_id\". (SQL: select \"supplier-lists\".* from \"supplier-lists\" inner join \"suppliers\" on \"supplier-lists\".\"supplier_id\" = \"suppliers\".\"id\" and \"suppliers\".\"trade\" = EK order by supplier-lists.id DESC limit 25) at /var/www/html/OutlayReckoning/vendor/laravel/framework/src/Illuminate/Database/Connection.php:671)

In the php code stored above, I am trying to access the reference 'suppliers.id' within a join, but as you can see in the stored Laravel log, the function is trying to access an unstored db column called 'supplier' and therefore throws an error. Can anyone tell me if this is a bug or a problem in my code. I can hardly find any content on stackoverflow.

What I've already tried to fix it

I had already tried string escaping, possibly thought this was an escape problem. primary key in model is also set.

Backpack, Laravel, PHP, DB version

PHP VERSION:

PHP 7.4.3 (cli) (built: Mar 2 2022 15:36:52) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

LARAVEL VERSION:

v7.30.6@ecdafad1dda3c790af186a6d18479ea4757ef9ee

BACKPACK VERSION:

4.1.65@e7ab6b798262d847647238f91f88992b457927d9

mintUI9976 avatar Sep 21 '22 15:09 mintUI9976

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

welcome[bot] avatar Sep 21 '22 15:09 welcome[bot]

Hello @mintUI9976 Thanks for the report ... I am just wondering what you want to achieve ?

You can reference "related columns" by using dot.notation in your Column definition. Just make sure that your relation is eager loaded with ->with() (alias for join), or join, does not matter. $this->crud->query->with('supplier')

But that will load the relationship in the query, if you use any column like smt_id from the joined table backpack would not know and try to get it from the "curren model table, not the joined one". If you have a supplier relation, you can probably get that value with supplier.supplier_id (this will access the suplier relation that you eager loaded and get the supplier_id field.)

Am I missing something here ?

Cheers

pxpm avatar Sep 22 '22 13:09 pxpm

Thanks for answering my bug request, but I think I figured out the underscore is removed "supplier_id" once the query sends it to the database.

mintUI9976 avatar Sep 22 '22 14:09 mintUI9976

Okay i found an other solution, thanks for helping me.

Cheers

mintUI9976 avatar Sep 22 '22 14:09 mintUI9976