Commit Graph

26 Commits

Author SHA1 Message Date
Jinzhu 77eb925ea0 Refactor preloading many2many for auto preload 2018-02-10 00:07:16 +08:00
Geoff Baskwill ec72a4cb6b Call Query callback chain when preloading many2many ()
When using `Preload` on a `many2many` association, the
`Query` callback chain was not being called. This made
it difficult to write a plugin that could reliably get
called regardless of how objects were being queried.

Now `handleManyToManyPreload` will call the `Query`
callback chain for each object that is retrieved by
following the association.

Since the data has already been read by the
`handleManyToManyPreload` method, a new scope setting
called `gorm:skip_queryCallback` is set to `true` before
calling the callbacks. Callbacks can check for the
presence of this setting if they should not be run; the
default `queryCallback` is an example of this case.

Fixes .
2018-02-09 23:22:53 +08:00
Kyle Spraggs 6f64b8610d Update callback_query_preload.go () 2017-08-02 07:05:11 +08:00
Rob Rodriguez e470b44fa8 adding gorm:auto_preload option and related tests 2017-04-27 15:53:39 -07:00
Jinzhu 66d5b42ee9 Add error if exists after parse raw query results, fix 2017-03-24 09:28:06 +08:00
Jinzhu ab703afe97 Merge pull request from jugglinmike/empty-associations
Return empty slice for associations with 0 results
2016-10-06 21:23:04 +08:00
Jinzhu afaadc3942 Refactor named value support for PolymorphicType 2016-10-06 20:33:48 +08:00
slockij 1413e55339 Add named value for PolymorphicType (to replace scope.TableName) 2016-09-28 22:44:43 +02:00
Mike Pennisi f06d6412de Return empty slice for associations with 0 results
When using `Preload` to include the results of a "has many"
relationship, Gorm previously returned an uninitialized slice for any
such relations that bore zero records. This distinction was most
apparent when the results were marshalled to a JSON representation--a
record with zero related records would be represented with `null`.

For example, consider the following schema:

    id | name
    ---|------
    1  | Lorin
    2  | Sue

    id | p_id | value
    ---|------|-------------------
    1  | 1    | lorin@example.com
    2  | 1    | lorin2@example.com

Querying with:

    db.Preload("Email").Find(&people)

And marshalling the resulting value of `people` to JSON would yield the
following string:

    [
      {
        "name": "Lorin",
        "email": [
          "lorin@example.com",
          "lorin2@example.com"
        ]
      },
      {
        "name": "Sue",
        "email": null
      }
    ]

Beyond being inconsistent, the value `null` in this response differs
semantically from the actual state of the database. The database
actually has zero related records for the second user, so a JSON value
of `[]` is appropriate.

Update the callback that processes "has many" relationships to
communicate empty query results with an empty slice.
2016-09-27 17:09:57 -04:00
Jinzhu caa792644c Include model value when preloading many2many relations to enable row query callbacks 2016-06-17 18:18:46 +08:00
Jinzhu 6ef2a539bd Fix test 2016-06-16 18:18:00 +08:00
Jinzhu 61f50a429a Fix preload has one relations 2016-06-16 17:58:25 +08:00
Jinzhu 277b4e93b6 Merge pull request from girokon/m2m_preload_issue
fix m2m nested preload
2016-06-16 16:41:34 +08:00
Jinzhu dca5e54f8b Merge pull request from RichardKnop/bugfix/preload-panic
Fixed an occurrence of a panic while preloading non existent fields.
2016-06-01 23:10:28 +08:00
Björn fcf8dd4e23 Add support for polymorphic preload
Fixes 
2016-06-01 13:46:45 +02:00
Richard Knop 8dfcab0f7f Fixed an occurence of a panic while preloading non existent fields. 2016-05-24 00:54:53 +08:00
Igor Noskov 13ac0839c4 fix m2m nested preload 2016-05-12 14:48:08 +06:00
Jinzhu 0fd2fd66d5 Refactor handleHasManyPreload 2016-05-09 22:42:07 +08:00
Dirk-Jan d02c2a37ea Improved preload m2m performance 2016-05-09 14:24:53 +02:00
Jinzhu 846a2d401a Fix scan columns with same name 2016-03-10 17:13:48 +08:00
Jinzhu 3055bad1e8 scope.Fields() return slice of *Field 2016-03-07 14:25:41 +08:00
Jinzhu 5883c70478 Support custom preloading SQL, close , , 2016-02-15 21:29:47 +08:00
Jinzhu 421979cfc2 Order results when preload many2many relations 2016-02-14 17:21:40 +08:00
Jinzhu 896ee534e2 Add indirect method 2016-01-18 12:20:27 +08:00
Jinzhu 07773cc367 Keep refactoring on callbacks 2016-01-17 20:51:11 +08:00
Jinzhu de73d30503 Refactor callbacks 2016-01-17 15:30:42 +08:00