• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Trouble Storing an Array

Gossamer

Active member
#1
I'm currently working on an add-on that is supposed to take the recently posted post_id and post_date and store it in an array on a particular field of the xf_user table. I'm trying to serialize the array, but I keep getting an error when I try to unserialize it.

Here is what I have so far:
User Model
PHP:
public function getActivityPosts($userId)
{
$posts = $this->_getDb()->fetchOne('
SELECT rp_messages_for_activity AS posts
FROM xf_user
WHERE user_id = ?
', $userId);

return $posts;
}
User Datawriter
PHP:
public function updateTrackedActivityPosts($userId, $postid, $postdate)
{

/* Get current activity posts from database */
$activityPosts = $this->_getUserModel()->getActivityPosts($userId);

/* Unserialize data */
$activityPosts = unserialize($activityPosts);

/* Add new entry to activityPosts */
$activityPosts[$postid] = $postdate;

/* Serialize data */
$activityPosts = serialize($activityPosts);

$this->set('rp_messages_for_activity', $activityPosts);
}
I keep getting this error:
unserialize(): Error at offset 0 of 2 bytes

  1. XenForo_Application::handlePhpError()

  2. unserialize() in Goss/RoleplaySystem/DataWriter/User.php at line 23

  3. Goss_RoleplaySystem_DataWriter_User->updateTrackedActivityPosts() in Goss/RoleplaySystem/DataWriter/DiscussionMessage/Post.php at line 53

  4. Goss_RoleplaySystem_DataWriter_DiscussionMessage_Post->_updateUserPostsForActivity() in Goss/RoleplaySystem/DataWriter/DiscussionMessage/Post.php at line 10

  5. Goss_RoleplaySystem_DataWriter_DiscussionMessage_Post->_messagePostSave() in XenForo/DataWriter/DiscussionMessage.php at line 589

  6. XenForo_DataWriter_DiscussionMessage->_postSave() in XenForo/DataWriter.php at line 1423

  7. XenForo_DataWriter->save() in XenForo/ControllerPublic/Thread.php at line 578

  8. XenForo_ControllerPublic_Thread->actionAddReply() in XenForo/FrontController.php at line 351

  9. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134

  10. XenForo_FrontController->run() in C:/xampp/htdocs/xenforo_DEV/index.php at line 13
Any ideas?
 

Snog

Well-known member
#2
Your serialized data most likely has an invalid length or data in general.

What does a dump of the serialized data look like?
 

Gossamer

Active member
#3
Okay, so I think I figured out that issue. My database still had old info from when I was testing saving with unserialized data. I removed that from the field and it looks like the data isn't saving at all now.

This is my DataWriter
PHP:
class Goss_RoleplaySystem_DataWriter_User extends XFCP_Goss_RoleplaySystem_DataWriter_User
{
protected function _getFields()
{
$fields = parent::_getFields();

$fields['xf_user']['rp_message_count'] = array('type' => self::TYPE_UINT_FORCED, 'default' => 0);
$fields['xf_user']['app_accept_date'] = array('type' => self::TYPE_UINT_FORCED, 'default' => 0);
$fields['xf_user']['rp_messages_for_activity'] = array('type' => self::TYPE_STRING, 'default' => 0);

return $fields;
}

public function updateTrackedActivityPosts($userId, $postid, $postdate)
{

/* Get current activity posts from database */
$activityPosts = $this->_getUserModel()->getActivityPosts($userId);

/* Unserialize data */
$activityPosts = unserialize($activityPosts);

/* Add new entry to activityPosts */
$activityPosts[$postid] = $postdate;

/* Serialize data */
$activityPosts = serialize($activityPosts);

$this->set('rp_messages_for_activity', $activityPosts);

return true;
}

protected function _getUserModel()
{
return $this->getModelFromCache('XenForo_Model_User');
}
}