modxcms / revolution

MODX Revolution - Content Management Framework
https://modx.com/
GNU General Public License v2.0
1.36k stars 529 forks source link

modDbRegister->clear doesn't work #12965

Closed arthurkazaryants closed 5 years ago

arthurkazaryants commented 8 years ago

Summary

Register "clear" method doesn't work because of two reasons

package: modx subpackage: registry file: /core/model/modx/registry/moddbregister.class.php class: modDbRegister method: clear (line 72)

/**
 * Clear the register messages.
 *
 * {@inheritdoc}
 */

  public function clear($topic) {
        $result = $this->modx->removeCollection('modDbRegisterMessage', array('topic' => $topic));
        return (bool)$result;
    }

First reason: $this->modx->removeCollection throws error. collection name should be replaced:

$result = $this->modx->removeCollection('registry.db.modDbRegisterMessage', array('topic' => $topic));

Second reason: $topic argument can't be passed to remove collection directly. We have to get topic ID first.

Below is candidate to replace whole method:

/**
     * Clear the register messages.
     *
     *  @param string $topic A topic container in which to broadcast the message.
     */
    public function clear($topic) {
        $result = false;

        if (empty($topic) || $topic[0] != '/') $topic = $this->_currentTopic . $topic;
        $topicIdx = array_search($topic, $this->subscriptions);
        $queueId = $this->_queue->get('id');
        if ($queueId && $topicIdx !== false) {
            if ($topicObj = $this->modx->getObject('registry.db.modDbRegisterTopic', array('queue' => $queueId, 'name' => $topic))) {
                $topicId = $topicObj->get('id');
                $result = $this->modx->removeCollection('registry.db.modDbRegisterMessage', array('topic' => $topicId));
            }
        }

        return (bool)$result;
    }

Environment

MODX Revolution 2.5.4

arthurkazaryants commented 7 years ago

anybody here?

sdrenth commented 7 years ago

Since I'm not familiar with modDbRegister I do not understand why the second reason your giving is an issue? If you just pass the topic id to the clear method it will clear the messages for the given topic ID as long as modDbRegisterMessage has been changed to registry.db.modDbRegisterMessage.

@artimarkov Why can't you just pass a topic ID? Is this method called from somewhere where it will always pass the topic name instead of the ID?

I've done some tests and below clears all messages with topic id 4 (after fixing the object name first):

$modx->registry->food->clear(4);
arthurkazaryants commented 7 years ago

@sdrenth yes, we don't use ID. We always calling methods with topic name. Example:

$modx->registry->cartRegistry->send(
                        '/user_cart/', // <— topic name here
                        array($session_id => $cart_data),   
                        array('ttl' => 259200)
                    );