eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

Preserve order from CP when saving entries

Open ryanmitchell opened this issue 1 year ago • 13 comments

This goes some of the way to resolving issue #43 in that it preserves the order, however if we're using incrementing IDs then ordering will fail once IDs hit double digits as it will treat it as string ordering rather than numeric ordering (so 1, 10, 2, 3 etc).

I'm not sure how to resolve this in the JSON field with getting into specific code for specific database types.

The best option might be to make an order column in the database for handling this?

ryanmitchell avatar Sep 05 '22 16:09 ryanmitchell

Update: I've tapped into laravel core builder methods to cast the order as a real field which seems to do the trick.

ryanmitchell avatar Sep 05 '22 21:09 ryanmitchell

image Hi , i tried this pr again and i have time format issue while reordering , is it something to do with my mysql configuration ?

enespolat24 avatar Sep 16 '22 14:09 enespolat24

If you can supply a sample repository with clear steps to reproduce the issue then I can look into it further.

On 16 Sep 2022, at 15:28, Enes Polat @.***> wrote:

https://user-images.githubusercontent.com/21336979/190662690-188aa983-f89c-4e1b-b746-115c65c60558.pngHi , i tried this pr again and i have time format issue while reordering , is it something to do with my mysql configuration ? — Reply to this email directly, view it on GitHub https://github.com/statamic/eloquent-driver/pull/57#issuecomment-1249437227, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMVOZ2NLSC3OLZM5JMEPLV6R72RANCNFSM6AAAAAAQFE2MXA. You are receiving this because you authored the thread.

ryanmitchell avatar Sep 16 '22 14:09 ryanmitchell

i'll try to supply a sample tonight but i temporarily eleminated the problem like this in the CollectionRepository image

enespolat24 avatar Sep 16 '22 14:09 enespolat24

That shouldnt be necessary as updated_at should come from the laravel model timestamps.

ryanmitchell avatar Sep 16 '22 16:09 ryanmitchell

@enespolat24 can you create a sample repository so I can look into this further?

ryanmitchell avatar Sep 21 '22 07:09 ryanmitchell

hi , sorry for the latency https://github.com/enespolat24/example-case

enespolat24 avatar Sep 21 '22 15:09 enespolat24

@jasonvarga this is good to merge from our testing and the feedback from others

ryanmitchell avatar Sep 28 '22 08:09 ryanmitchell

@ryanmitchell I tested this with one of my live projects. We use mariaDb instead of mysql. Can we use DOUBLE PRECISION instead of REAL since mariadb doesn't support REALdatatype ?

enespolat24 avatar Sep 28 '22 14:09 enespolat24

What’s the support for that on SQLite, Postgres and MySQL?

ryanmitchell avatar Sep 28 '22 14:09 ryanmitchell

I checked their docs. they all support DOUBLE PRECISION but in the Postgres there's a storage size difference. REAL uses 4 byte but DOUBLE PRECISION uses 8 byte

enespolat24 avatar Sep 28 '22 15:09 enespolat24

Ok. I ran some quick tests and it seems ok to switch to that.

On 28 Sep 2022, at 16:02, Enes Polat @.***> wrote:

I checked their docs. they all support DOUBLE PRECISION but in the Postgres there's a storage size difference. REAL uses 4 byte but DOUBLE PRECISION uses 8 byte

— Reply to this email directly, view it on GitHub https://github.com/statamic/eloquent-driver/pull/57#issuecomment-1261047140, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMVO66USZ3QH2T5QUWKYLWARMY3ANCNFSM6AAAAAAQFE2MXA. You are receiving this because you were mentioned.

ryanmitchell avatar Sep 28 '22 16:09 ryanmitchell

@jasonvarga would be great to get this merged as ordering isn't working in the CP at the moment.

ryanmitchell avatar Oct 13 '22 10:10 ryanmitchell

@enespolat24 Does this PR fix your issue? Last you said on #43 was that you're still having issues.

jasonvarga avatar Oct 26 '22 20:10 jasonvarga

@ryanmitchell What's the reason for using the orderByRaw and CAST stuff? Is it because you don't want to add an order column?

I'm using MariaDB 10.5.8 and it doesn't seem to like that syntax. I get the following error when I visit the reorder entries screen in the CP.

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PRECISION) ASC' at line 1
select
  *
from
  `entries`
where
  `collection` = 'more_pages'
order by
  CAST(
    json_unquote(json_extract(`data`, '$."order"')) AS DOUBLE PRECISION
  ) ASC
limit
  50 offset 0

jasonvarga avatar Oct 26 '22 21:10 jasonvarga

@jasonvarga works like a charm with mysql but i tried this with our Mariadb 10.6.3 and got same error as you do. the walk around i did was changing DOUBLE PRECISION as DOUBLE. Still not optimum but does the job. with this temporary solution i need to click save button more than once

enespolat24 avatar Oct 26 '22 23:10 enespolat24

Yeah I was trying to avoid adding a column but I guess in hindsight it’s just making things more complicated. I’ll refactor it to use a nullable order column

ryanmitchell avatar Oct 27 '22 05:10 ryanmitchell

Two things:

  1. Why are you calling the column sort_order instead of just order?
  2. How can we get that migration going? It's just a stub, how are people supposed to use it once they upgrade?

jasonvarga avatar Oct 27 '22 14:10 jasonvarga

Order is a reserved word in some db engines. I’ve always avoided using it as a result.

Yeah the migrations annoyed me too - I don’t like the approach of publishing then every time but others contributing to the big PR wanted it so I gave in.

To me it should automatically publish but I think we are stuck now?

ryanmitchell avatar Oct 27 '22 14:10 ryanmitchell

Order is a reserved word in some db engines. I’ve always avoided using it as a result.

I would think as long as its quoted appropriately it'll be fine with it being a column name.

ie. select `title`,`order` from `entries` as opposed to just select title,order from entries Laravel adds the quotes.

As for publishing the migrations.... even if you publish the migrations again, it publishes all of them, not just new ones. 🤔

jasonvarga avatar Oct 27 '22 14:10 jasonvarga

Ok I’ll change it over later on.

Yeah I should have really pushed back on the migrations, I knew it wasn’t the right approach - sorry about that.

do we need to do something like the upgrade to logic in core that you use for adding permissions etc?

ryanmitchell avatar Oct 27 '22 14:10 ryanmitchell

Thanks. We'll figure out the migration side of it from here. Leave it with us.

Much appreciated!

jasonvarga avatar Oct 27 '22 14:10 jasonvarga

Sorry that ive left you with a mess to clean up!

ryanmitchell avatar Oct 27 '22 15:10 ryanmitchell