Hi,
I can't find why the subscribed
value is not detected as changed by getChangedFields()
.
Here is the relevant code:
$subscription = GkNewsCommentsAccess::findFirst([
'conditions' => 'news_id = ?1 and user_id=?2',
'bind' => [1 => $newsid, 2 => $userid]
]);
$this->logger->debug('isSubscribed: '. $subscription->subscribed);
$this->logger->debug('getSnapshotData before: '. print_r($subscription->getSnapshotData(), TRUE));
$this->logger->debug('getChangedFields before: '. print_r($subscription->getChangedFields(), TRUE));
$subscription->subscribed = $subscribe; // changing value
$subscription->save();
$this->logger->debug('isSubscribed: '. $subscription->subscribed);
$this->logger->debug('getSnapshotData after: '. print_r($subscription->getSnapshotData(), TRUE));
$this->logger->debug('getChangedFields after: '. print_r($subscription->getChangedFields(), TRUE));
The debug output is as:
DEBUG isSubscribed: 1
DEBUG getSnapshotData before: Array ( [news_id] => 177 [user_id] => 26422 [read] => 2017-05-20 02:28:24 [post] => [subscribed] => 1 )
DEBUG getChangedFields before: Array ( )
DEBUG isSubscribed: 0
DEBUG getSnapshotData after: Array ( [news_id] => 177 [user_id] => 26422 [read] => 2017-05-20T02:29:08+02:00 [post] => [subscribed] => 0 )
DEBUG getChangedFields after: Array ( )
I expect the getChangedFields after
to be as Array ( [0] => read, [1] => subscribed )
However, if I change value of user_id
or news_id
this is detected as changed by getChangedFields()
Am I doing something wrong? Reading from model.zep
, I expect it to work.
Thanks for your help.
- PHALCON_VERSION=3.1.2
- I have
$this->keepSnapshots(true);
in Modelinitialize()
My actual function is:
protected function updateSubscription(bool $subscribe) {
$subscription = new GkNewsCommentsAccess();
$subscription->news_id = $this->dispatcher->getParam('news_id');
$subscription->user_id = $this->session->user_id;
$subscription->subscribed = ($subscribe?'1':'0');
if (!$subscription->save()) {
$this->flashSession->warning($this->di->getTranslation()
->_('news_comment_failed_save_subscription'));
foreach ($subscription->getMessages() as $message) {
$this->flashSession->error($message);
}
return;
}
if ($subscription->hasSnapshotData() && !$subscription->hasChanged('subscribed')) {
return;
}
if ($subscribe) {
$this->flashSession->success($this->di->getTranslation()
->_('news_comment_you_are_successfully_subscribed'));
} else {
$this->flashSession->success($this->di->getTranslation()
->_('news_comment_you_are_successfully_un_subscribed'));
}
}