admad / cakephp-sequence Goto Github PK
View Code? Open in Web Editor NEWCakePHP plugin for maintaining a contiguous sequence of records
License: MIT License
CakePHP plugin for maintaining a contiguous sequence of records
License: MIT License
I've started using this plugin, when users already had order
field in place within qobo/cakephp-survey
plugin.
As a result, some of the entities had following order sequence within defined survey questions scope:
[
'question_1' => 1,
'question_2' => 2,
'question_3' => 5,
];
The UI has a wrapper for moveUp()
and moveDown()
methods of the behaviour, and everything works fine, but when the user has to swap question_2
and question_3
, the first one has to be clicked multiple times to reach 4 <-> 5
swap.
Moving question_3
up, the user still has to click multiple times.
I could look into slightly changing _movePosition()
method to look for closest neighbour entity (depending whether its up/down direction) and re-calculate its position
fields, to get rid off possible gaps.
For visibility!
I selected my ordering column to be order and got mysql errors.
when I changed to 'order
' it worked.
Is there possible to restart sequence
13,15,14,16 => 1,3,2,4
Or ideally keep the sequence to always start with start value
start, start+1, start+2, ...
I detected that setOrder method fails hard with protected fields ($_accesible Entity definition). With some validation rules, saving entity will fail. I added this line to overcome this issue, but I'm not sure if this is really safe for all types of models (in my app works OK):
$entities = $table->newEntities($data, ['accessibleFields' => ['*' => true]]);
This method could also accept an array of entities, so we don't need to convert our entities to an array of data to use setOrder.
Another suggestion. We can add another method to fix list order numbers (CakeDC List has some method like that) based in passed where conditions. For example, if you have positions 1,2,3,4 and move down 1, the result list will be 0,2,3,4 and I prefer it to be 1,2,3,4. This happen with list upper limit too. Right now I'm using setOrder for this, but I need to get entities, convert to array, pass to setOrder...
I can PR some of this changes if you want.
The readme says to run composer require admad/sequence:dev-master
to install via composer but you actually need to run composer require admad/cakephp-sequence:dev-master
If you use a reserved word as the order
field the the generated SQL does not get properly escaped when quoteIdentifiers
is set to true.
Not sure if this is a bug with Cake or not.
Example
$this->addBehavior('Sequence.Sequence', [
'order' => 'order',
'start' => 0,
'scope' => 'course_id'
]);
Then the following SQL is created:
UPDATE course_modules SET `order` = (order - 1) WHERE (`order` > :c0 AND `course_id` = :c1)
You can see the order is not getting escaped. It's caused by these lines
$this->_table->query()->newExpr()->add("$orderField + 1")
Main issue is here as i think this is cake related.
The work around seems to be to comment out the beforeFind
in the Behavior and and add this to clause to the 'sort' option when creating your association.
$this->belongsToMany('Modules', [
'through' => 'course_modules',
'sort' => ['position' => 'ASC']
]);
Until the union problem is fixed this is the only way I've found to use the Behavior with it's intended behavior ( no pun intended )
I encountered an problem using virtual fields when editing data:
The method "_getOldValue" adds virtual fields to the $values-array (called in line 175). These are later on passed to the database which results in an error - fields not found...
I created an patch which solves this bug for me.
When running the test the following warning occurs.
Option "idField" is deprecated, use "keyField" instead.
PR is here: #4
Hi,
After using successfuly Behavior on a Table having belongsToMany relation and grouping with 'scope' field, I currently trying to use the Behavior on a Table having hasMnay relation. In this case, I don't have to use 'scope' field, I simply want to handle position of each table's record.
I get an SQL error :
"Error: SQLSTATE[HY000]: General error: 1364 Field 'position' doesn't have a default value"
"INSERT INTO gallerycategories (name, slug) VALUES (:c0, :c1)"
Of course, 'position' field has any default value in my SQL settings.
I don't need it assuming Behavior handles 'position' property in Entity.
I debugged Entity in beforeSave method within Behavior and Entity's got 'position' property.
So, I assumed the Behavior was working as expecded... Then, I tryed to set a default value to my 'position' field in SQL Database and I figured out that 'position' field wasn't set into SQL Database, but 'position' default value was setting in instead.
This is running me crazy, because I successfuly used this great Behavior once. By my side, I checked all I do without any success. The only one difference that I got between my successful use case and now, is not having use 'scope' Behavior setting.
Have you got some ideas ?
Sometimes you don't really care to reorder the entire list but just one or two entities, and having a helper method for this would be useful.
Hey, thanks for your work on this
I have a case where I wanted to keep two columns storing the order.
Is there anyway to accomplish that with the plugin? maybe a workaround?
The case is like a voting system with two phases. I have an order for the first phase and an order for the second phase, they can both be edited.
There seems to be some discrepancy between the Admad/Sequence
namespace used in the readme and the plain Sequence
one the plugin is actually using. Which PR do you prefer?
Deprecated: As of 4.5.0 using query() is deprecated. Instead use insertQuery()
, deleteQuery()
, selectQuery()
or updateQuery()
in vendor/admad/cakephp-sequence/src/Model/Behavior/SequenceBehavior.php, line: 555
It looks like this behavior is just supporting generating a list on insert but there is no way to modify the list order or insert at a certain position?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.