-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
OctoberCMS repeater: How to store index/id inside the json array #4056
Comments
@nolyboms could you please provide more information on why you need the index to be stored? It used to be stored (at least, that's what I remember) but it was removed because it made it overly complex to store that information in two ways. |
@LukeTowers thank you for responding. |
@nolyboms hmm. If you have a need for a unique ID you should be using unique records with a RelationController, not a Repeater. The reason for this is that the "id" of a repeater is not unique, it's just an integer describing it's position within the array. If you really must have a unique ID then I recommend you look at adding to the data in |
@LukeTowers Since that was possible in a previous OctoberCMS version - do you have an idea on how to get that info and store it in the array? |
That number that you see is just straight CSS, it doesn't exist anywhere else. You'll notice that if you drag and reorder records the numbers stay in the same position even though the item contents are now different. That is why it is a bad idea to use the position of the element as an ID for other purposes. |
@LukeTowers We are getting close. :-) In my controller i am trying to append a value to the json array. But when i add this function to my controller it tries to append the value to a column:
public function formBeforeSave($model)
{
$model->topics= 'x1';
}
According to the docs i would have to define a accessor which i think is not necessary in my case. So the question is now: |
@nolyboms same as you would with any array in general. public function formBeforeSave($model)
{
$model->topics = array_map($model->topics, function ($topic) {
$topic['id'] = uniqid();
});
} |
@LukeTowers Adding this to my controller
throws the error:
And when i adjust the function according to a solution provided on stackoverflow to look like this...
I get the error:
Any idea how to make OctoberCMS happy and not throwing errors anymore? :) |
Whoops, order of arguments was backwards. array_map(callbackFunction, $inputArray) |
My function looks now like this:
The output of var_dump states that the id is been appended but checking the db column and generated API, it seems that the value is not being stored. Very strange. -_- |
@nolyboms try attaching to the model's beforeSave event. |
Nope. Didn't work. but... So here is the strange thing which i can not understand: Repeater Field Name: topics Controller function:
NO TOPIC_ID IS STORED! Now watch this... Repeater Field Name: no change
TOPIC_ID is appended to the column themes! ?????? |
@nolyboms post your model class. |
sidenote: i have changed the column name and all corresponding assets now from "topics" to "timeslots" model class:
controller:
|
is it working now? |
No. :( |
@nolyboms did you try putting the logic in the |
And for anyone in the same situation, here is the solution: public function beforeSave()
{
$this->_FieldName_ = array_map(function ($miau) {
$miau['id'] = uniqid();
return $miau;
}, $this->_FieldName_);
} |
Hello everyone,
after 3 days of researching for a way to accomplish my need i thought its best to ask and hope to get an answer.
In this post
#2772 (comment)
it is exactly described what i am trying to do.
I have a repeater in my backend form and the data gets saved in the database as a json like that:
"topics": [
],
What i would like to achieve is that the repeater items get indexed like that:
"topics": [
],
In another github post #2451 (comment) it is stated that this could only be achieved by manipulating the array with
formBeforeSave()
But the information is already available in the code as
$indexCount:
input type="hidden" name="___index_topics[]" value="2"
Can you please tell me how to get index/id into a repeater field so it gets stored inside the json array?
The text was updated successfully, but these errors were encountered: