0

I have question on how do you guys save post_category relationship, when i debug save action, i just saw Post to be saved , how magic underneath the hood

https://github.com/magefan/module-blog

enter image description here

1 Answer 1

1

After saving the post, it calls the post model's _afterSave function, which updates the relationship data for categories and tags:

Model/ResourceModel/Post.php

    protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
    {
        ...

        /* Save category & tag links */
        foreach (['category' => 'categories', 'tag' => 'tags'] as $linkType => $dataKey) {
            $newIds = (array)$object->getData($dataKey);
            foreach ($newIds as $key => $id) {
                if (!$id) { // e.g.: zero
                    unset($newIds[$key]);
                }
            }
            if (is_array($newIds)) {
                $lookup = 'lookup' . ucfirst($linkType) . 'Ids';
                $oldIds = $this->$lookup($object->getId());
                $this->_updateLinks(
                    $object,
                    $newIds,
                    $oldIds,
                    'magefan_blog_post_' . $linkType,
                    $linkType . '_id'
                );
            }
        }
1
  • sure thank you <3
    – ducanh
    Commented Jul 6 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.