Skip to content
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

[fix] Multiline #1631

Merged
merged 10 commits into from
May 10, 2021
Merged

[fix] Multiline #1631

merged 10 commits into from
May 10, 2021

Conversation

ibelar
Copy link
Contributor

@ibelar ibelar commented May 7, 2021

Align with latest data changes.

BC-BREAK

Multiline->setReferenceModel() method parameters has changed.

You now have to passed the reference name and the model entity to where reference apply:

$multiline->setReferenceModel('Products', $categoryModel->load($id));

Align with latest data changes
@ibelar ibelar requested a review from mvorisek May 7, 2021 15:48
}
}
$id = $row_model->save()->getId();
$id = $entity->save()->getId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save is outside foreach, is that intended?

otherwise feel free to merge it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - the intention is to collect all record values for one row into the for loop and save after.

Copy link
Member

@mvorisek mvorisek May 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but a few lines above there is a load... related with https://github.com/atk4/ui/pull/1631/files#r628487155

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad explanation from me.

The data collect from load contains all rows and each row has one set of record.
The second for loop is too loop for each field and check if the current field is editable prior to set is value.
Once all fields are set within the row, then the record is save.

Hope it is clearer now...

Maybe we could use $entity->save($row) directly but I am not sure how Data will react if trying to pass a field => value set to a non editable field... This is were my knowledge of Data fails 😄 Multiline will display the non editable field as readonly input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAK this relies on :N typecasting from single cell value which... Which at least is not good to be saved n times when looping.

feel free to merge without my review once you are happy with the changes ;-)

if ($fieldName === $row_model->id_field && $value) {
$row_model = $row_model->load($value);
if ($fieldName === $model->id_field && $value) {
$entity = $model->load($value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically the id_field could be anywhere in the array, couldn't it? So it could be that some values are already set to the entity by the rows below ($entity->set($fieldName, $value), and then $entity->load($value) comes after. That could overwrite values already set by $entity->set($fieldName, $value).
I think load() should be the first thing happening, any set() should happen after.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right - I guess it never breaks before probably because id is generally the first field in a row. I will make changes. Good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update

@@ -420,13 +433,15 @@ public function setModel(Model $model, array $fieldNames = []): Model
* Otherwise, form will try to save 'multiline' field value as an array when form is save.
* $multiline = $form->addControl('multiline', [Multiline::class], ['never_persist' => true])
*/
public function setReferenceModel(Model $refModel, string $linkByFieldName, array $fieldNames = []): Model
public function setReferenceModel(string $refModelName, Model $modelEntity = null, array $fieldNames = []): Model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

search setReferenceModel, docs needs to be adjusted.

In general, do we need to pass Model at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc updates. In general, the Model entity will be set using the model set in Form. But there might be cases where you need to pass it with the method.

@mvorisek mvorisek merged commit 6690f7f into develop May 10, 2021
@mvorisek mvorisek deleted the fix/multiline branch May 10, 2021 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants