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

[3.x]: EVENT_AFTER_APPLY_DRAFT data in DB is not consistent #13087

Closed
eduardoalonsoalbella opened this issue Apr 17, 2023 · 3 comments
Closed
Assignees

Comments

@eduardoalonsoalbella
Copy link

eduardoalonsoalbella commented Apr 17, 2023

What happened?

Description

In EVENT_BEFORE_APPLY_DRAFT if we try to get a relationship value ex:

$entry->fieldName->one() we get correctly our custom relationship

But in EVENT_AFTER_APPLY_DRAFT if we try to get that relationship

$entry->fieldName->one() we get null

Steps to reproduce

  1. (explained above)

Expected behavior

The code should always return the related object

Actual behavior

In one event the data is returned as expected, in the other one it doesn't work

Craft CMS version

latest 3.8.7

PHP version

8.1

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

  • latest versions of neo blocks, supertables
@eduardoalonsoalbella
Copy link
Author

eduardoalonsoalbella commented Apr 17, 2023

The following query is run in the AFTER event to get the value for that property, if i Had to guess some data has already been updated/changed in the db, but the $event->source object, does not have the updated data, and is unable to retrieve the relationships correctly

If i remember correctly, in the folowing query, the subquery, relations.sourceId = 375 does not exist when the query is performed, that'sthe reason we get a null value

SELECT `elements`.`id`,
       `elements`.`fieldLayoutId`,
       `elements`.`uid`,
       `elements`.`enabled`,
       `elements`.`archived`,
       `elements`.`dateCreated`,
       `elements`.`dateUpdated`,
       `elements_sites`.`id`      AS `siteSettingsId`,
       `elements_sites`.`slug`,
       `elements_sites`.`siteId`,
       `elements_sites`.`uri`,
       `elements_sites`.`enabled` AS `enabledForSite`,
       `elements`.`canonicalId`,
       `elements`.`dateLastMerged`,
       `jira_issues`.`key`
FROM (SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`
      FROM `craft_elements` `elements`
               INNER JOIN `craft_jira_issues` `jira_issues` ON `jira_issues`.`id` = `elements`.`id`
               INNER JOIN `craft_elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
               INNER JOIN `craft_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND
                                                           ((`relations`.`sourceId` = 375) AND (`relations`.`fieldId` = 121)) AND
                                                           ((`relations`.`sourceSiteId` IS NULL) OR
                                                            (`relations`.`sourceSiteId` = 14))
      WHERE (`elements_sites`.`siteId` = 17)
        AND (`elements`.`archived` = FALSE)
        AND (`elements`.`dateDeleted` IS NULL)
        AND (`elements`.`draftId` IS NULL)
        AND (`elements`.`revisionId` IS NULL)
      ORDER BY `relations`.`sortOrder` LIMIT 1) `subquery`
         INNER JOIN `craft_jira_issues` `jira_issues` ON `jira_issues`.`id` = `subquery`.`elementsId`
         INNER JOIN `craft_elements` `elements` ON `elements`.`id` = `subquery`.`elementsId`
         INNER JOIN `craft_elements_sites` `elements_sites` ON `elements_sites`.`id` = `subquery`.`elementsSitesId`
         INNER JOIN `craft_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND
                                                     ((`relations`.`sourceId` = 375) AND (`relations`.`fieldId` = 121)) AND
                                                     ((`relations`.`sourceSiteId` IS NULL) OR
                                                      (`relations`.`sourceSiteId` = 14))
ORDER BY `relations`.`sortOrder`

The subquery

SELECT `elements`.`id` AS `elementsId`, `elements_sites`.`id` AS `elementsSitesId`
      FROM `craft_elements` `elements`
               INNER JOIN `craft_jira_issues` `jira_issues` ON `jira_issues`.`id` = `elements`.`id`
               INNER JOIN `craft_elements_sites` `elements_sites` ON `elements_sites`.`elementId` = `elements`.`id`
               INNER JOIN `craft_relations` `relations` ON (`relations`.`targetId` = `elements`.`id`) AND
                                                           ((`relations`.`sourceId` = 375) AND (`relations`.`fieldId` = 121)) AND
                                                           ((`relations`.`sourceSiteId` IS NULL) OR
                                                            (`relations`.`sourceSiteId` = 14))
      WHERE (`elements_sites`.`siteId` = 17)
        AND (`elements`.`archived` = FALSE)
        AND (`elements`.`dateDeleted` IS NULL)
        AND (`elements`.`draftId` IS NULL)
        AND (`elements`.`revisionId` IS NULL)
      ORDER BY `relations`.`sortOrder` LIMIT 1

@brandonkelly
Copy link
Member

Fixed for the next Craft 3 and 4 releases!

@brandonkelly
Copy link
Member

Craft 3.8.8 and 4.4.8 are out with the fix for this. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants