Extending ControllerPublic

I think yes, you also need to make that field your primary key (but doesn't need AI if you're inserting from another table).

Also, I suppose $testuserid_input's value is correct?

In the template? Yes. The model works as intended and any duplicates display a responseError with zero issues.
What do you mean inserting from another table? I'm getting a visitor instance and just sending the user_id as a parameter to the datawriter.

I set user_id as primary but still giving me the same error :(
 
Last edited:
Nevermind I got it worked out but one more error :(
I'm getting:
The existing data required by the data writer could not be found.

Here is my controllerpublic:
PHP:
    public function actionTestUpdate()
    {
        $visitor = XenForo_Visitor::getInstance();

        // parse input parameters
        $input = $this->getInput();
        $testuserid_input = $input->filterSingle('auth_id', XenForo_Input::STRING);
        $duplicate = $this->_getAccountModel()->verifyUserID($testuserid_input);
        if (empty($duplicate))
        {
            $dwAccount = XenForo_DataWriter::create('Test_DataWriter_Account');
            $dwAccount->setExistingData($testuserid_input);
            $index = array(
                        'user_id' => $visitor['user_id'],
                        'auth_id' => $testuserid_input
            );
            $dwAccount->bulkSet($index);
            $dwAccount->save();
            return $this->responseRedirect(
                XenForo_ControllerResponse_Redirect::SUCCESS,
                XenForo_Link::buildPublicLink('account/Test')
            );
        }
        else
        {
            return $this->responseError("That UserID is currently in use! Please try another!");
        }
    }
and here is my datawriter:
PHP:
<?php
class Test_DataWriter_Account extends XenForo_DataWriter
{
    /**
    * Gets the fields that are defined for the table. See parent for explanation.
    *
    * @return array
    */
    protected function _getFields()
    {
        return array(
            'xf_test' => array(
                'user_id'    => array(
                    'type' => self::TYPE_UINT
                ),
                'auth_id'    => array(
                    'type' => self::TYPE_STRING, 'required' => true
                ),
            )
        );
    }

    /**
    * Gets the actual existing data out of data that was passed in. See parent for explanation.
    *
    * @param mixed
    *
      * @see XenForo_DataWriter::_getExistingData()
      *
      * @return array|false
    */
    protected function _getExistingData($data)
    {
        if (!$id = $this->_getExistingPrimaryKey($data, 'auth_id'))
        {
            return false;
        }

        return array('xf_test' => $this->_getAccountModel()->getTestUserID($id));
    }
    /**
    * Gets SQL condition to update the existing record.
    *
    * @see XenForo_DataWriter::_getUpdateCondition()
    *
    * @return string
    */
    protected function _getUpdateCondition($tableName)
    {
        return 'user_id = ' . $this->_db->quote($this->getExisting('user_id'));
    }
    /**
    * Get the simple text model.
    *
    * @return SimpleText_Model_SimpleText
    */
    protected function _getAccountModel()
    {
        return $this->getModelFromCache('Test_Model_Account');
    }
}
?>
I've looked everywhere but nobody has seemed to have this issue and I dunno what's causing it D:

Re-review your datawriter code. I'd actually step through the calls in the default datawriter class so you can see where the error is being thrown. That'd give you a hint.

I actually see two issues.

The first issue seems to be a logistical issue in your controller. You ensure the user isn't already represented in your table, then you try to grab the existing data from the table. Since you've already proven that the user isn't there, how would he have an existing record?

The second...I guess I'm not sure how your model returns the data that the datawriter is looking for. I'm rusty, but I thought it was supposed to be array($tableName -> array($key -> $value)). And, since it's supposed to be setting the existing data, shouldn't it return the entire record?

The other thing is...your methods don't seem to describe what they do. Test_Model_Account::getTestUserId() returns the auth id (I'm not even sure of that, but I don't want to figure out the logic)? Not a bug, but how do you keep track of what things actually do?
 
Re-review your datawriter code. I'd actually step through the calls in the default datawriter class so you can see where the error is being thrown. That'd give you a hint.

I actually see two issues.

The first issue seems to be a logistical issue in your controller. You ensure the user isn't already represented in your table, then you try to grab the existing data from the table. Since you've already proven that the user isn't there, how would he have an existing record?

The second...I guess I'm not sure how your model returns the data that the datawriter is looking for. I'm rusty, but I thought it was supposed to be array($tableName -> array($key -> $value)). And, since it's supposed to be setting the existing data, shouldn't it return the entire record?

The other thing is...your methods don't seem to describe what they do. Test_Model_Account::getTestUserId() returns the auth id (I'm not even sure of that, but I don't want to figure out the logic)? Not a bug, but how do you keep track of what things actually do?
Sorry, was busy the last few days.

Second one: No, array($table => $rowContent) is correct.
 
Re-review your datawriter code. I'd actually step through the calls in the default datawriter class so you can see where the error is being thrown. That'd give you a hint.

I actually see two issues.

The first issue seems to be a logistical issue in your controller. You ensure the user isn't already represented in your table, then you try to grab the existing data from the table. Since you've already proven that the user isn't there, how would he have an existing record?

The second...I guess I'm not sure how your model returns the data that the datawriter is looking for. I'm rusty, but I thought it was supposed to be array($tableName -> array($key -> $value)). And, since it's supposed to be setting the existing data, shouldn't it return the entire record?

The other thing is...your methods don't seem to describe what they do. Test_Model_Account::getTestUserId() returns the auth id (I'm not even sure of that, but I don't want to figure out the logic)? Not a bug, but how do you keep track of what things actually do?

So basically do something like this?
PHP:
    public function actiontestUpdate()
    {
        $visitor = XenForo_Visitor::getInstance();

        // parse input parameters
        $input = $this->getInput();
        $testuserid_input = $input->filterSingle('auth_id', XenForo_Input::STRING);
        $userid = $this->_getAccountModel()->verifyUserID($testuserid_input);
        $authid = $this->_getAccountModel()->verifyAuthID($testuserid_input);
        if (empty($userid))
        {
      
        }
        if (empty($authid))
        {
            $dwAccount = XenForo_DataWriter::create('test_DataWriter_Account');
            $dwAccount->setExistingData($testuserid_input);
            $index = array(
                        'user_id' => $visitor['user_id'],
                        'auth_id' => $testuserid_input
            );
            $dwAccount->bulkSet($index);
            $dwAccount->save();
            return $this->responseRedirect(
                XenForo_ControllerResponse_Redirect::SUCCESS,
                XenForo_Link::buildPublicLink('account/test')
            );
        }
        else
        {
            return $this->responseError("That UserID is currently in use! Please try another!");
        }
    }
Or should I just modify the datawriter so it updates/adds both the userid and the authid?
 
Ok I did get further, but hit another snag:

The existing data required by the data writer could not be found.
Change Test UserID:
Please enter a value for the required field 'auth_id'.

Controller Public:
PHP:
    public function actionTestUpdate()
    {
        $id = XenForo_Visitor::getUserId();

        // parse input parameters
        $input = $this->getInput();
        $testuserid_input = $input->filterSingle('auth_id', XenForo_Input::STRING);
        $authid = $this->_getAccountModel()->checkAuthID($testuserid_input);
        $userid = $this->_getAccountModel()->checkUserID($id);
        if ($authid)
        {
            $dwAccount = XenForo_DataWriter::create('Test_DataWriter_Account');
            $index = array(
                        'user_id' => $id,
                        'auth_id' => $testuserid_input
            );
            if ($userid)
            {
                $dwAccount->setExistingData($index);
            }
            else
            {
                $dwAccount->bulkSet($index);
            }
            $dwAccount->save();
            return $this->responseRedirect(
                XenForo_ControllerResponse_Redirect::SUCCESS,
                XenForo_Link::buildPublicLink('account/test')
            );
        }
        else
        {
            return $this->responseError("That UserID is currently in use! Please try another!");
        }
    }
Model:
PHP:
<?php

class Test_Model_Account extends XenForo_Model
{
    public function getTestAuthID($user_id)
    {
        $auth_id = $this->_getDb()->fetchRow('SELECT auth_id FROM xf_test WHERE user_id = ?', $user_id);
        if (!empty($auth_id))
        {
            $string = reset($auth_id);
            //if ($string == '0') { $string = 'None'; }
            return $string;
        }
        return 'None';
    }
    public function checkUserID($user_id)
    {
        $user_id = $this->_getDb()->fetchRow('SELECT user_id FROM xf_test WHERE user_id = ?', $user_id);
        if (!empty($user_id))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public function checkAuthID($input)
    {
        $auth_id = $this->_getDb()->fetchRow('SELECT auth_id FROM xf_test WHERE auth_id = ?', $input);
        if (empty($auth_id))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
Also is this a good way to do this? Because it feels like it's a bit sloppy :C
 
Last edited:
Ugh I got it working. I was never checking in the datawriter for an existing primary key of user_id, I was checking the entire row :C
 
You're still attempting to get input at DataWriter. These belongs to the Controller, not the DW.

Ok great progress on my plugin, everything is working so far, just need help on something small.
I'm storing html into a variable that I can display in a template, but when I just put {$html} or whatever, it literally displays the unparsed HTML, so how can I display a variable as HTML?
 
Ok great progress on my plugin, everything is working so far, just need help on something small.
I'm storing html into a variable that I can display in a template, but when I just put {$html} or whatever, it literally displays the unparsed HTML, so how can I display a variable as HTML?

{xen:raw $html}
 
Top Bottom