[XF1] ID Problems - Broken Model?

Igneous

Participant
Joined
Dec 4, 2016
Messages
64
I'm working on a custom addon and I'm coming across issues with editing & deleting. Its only returning information about the first row in the table no matter which row I click on.

All of the rows have different IDs (15, 16, 17 etc as an example) but whenever I click on any of them, it only returns ID 15s row information (or whatever row is first if I delete that row.)

When looking at the page through inspect element, the hidden input definitely confirms that it has the ID of the first row, not the URL ID.

I'm 95% sure that my Model is the problem so here it is -

PHP:
<?php
class UFPGaming_ServerList_Model_ServerList extends XenForo_Model
{
    
    public function getAllServers()
    {
        return $this->fetchAllKeyed('
            SELECT *
            FROM ufp_serverlist
            ORDER BY server_id
        ', 'server_id');
    }

    public function getServerById($id, array $fetchOptions = array())
    {
        $data = $this->getAllServers(array('server_id' => $id), $fetchOptions);

        return reset($data);
    }

}
Here's the code for ControllerAdmin as well.

PHP:
<?php

class UFPGaming_ServerList_ControllerAdmin_ServerList extends XenForo_ControllerAdmin_Abstract
{
protected function _preDispatch($action)
{
$this->assertAdminPermission('manageServerList');
}

public function actionIndex()
{
$model = $this->_getAllServers();
$allServers = $model->getAllServers();

$viewParams = array('allServers' => $allServers);

return $this->responseView('UFPGaming_ServerList_ViewAdmin_ServerList', 'serverlist_serverslist', $viewParams);
}


public function actionAdd()
{
$viewParams = array('server' => array(),);

return $this->responseView('UFPGaming_ServerList_ViewAdmin_ServerList_Edit', 'serverlist_edit', $viewParams);
}

public function actionEdit()
{
$id = $this->_input->filterSingle('server_id', XenForo_Input::UINT);
$server = $this->_getServerOrError($id);

$viewParams = array('server' => $server,);

return $this->responseView('UFPGaming_ServerList_ViewAdmin_ServerList_Edit', 'serverlist_edit', $viewParams);
}

public function actionSave()
{
$this->_assertPostOnly();

$id = $this->_input->filterSingle('server_id', XenForo_Input::UINT);

$dwInput = $this->_input->filter(array(
'server_name' => 'string',
'server_ip' => 'string',
'server_prefix' => 'string',
'server_image' => 'string'
));

$dw = XenForo_DataWriter::create('UFPGaming_ServerList_DataWriter_ServerList');
if ($id) {
$dw->setExistingData($id);
}
$dw->bulkSet($dwInput);
$dw->save();

return $this->responseRedirect(
XenForo_ControllerResponse_Redirect::SUCCESS,
XenForo_Link::buildAdminLink('servers')
);
}


public function actionDelete()
{
$id = $this->_input->filterSingle('server_id', XenForo_Input::UINT);
$server = $this->_getServerOrError($id);

if ($this->isConfirmedPost()) {
$dw = XenForo_DataWriter::create('UFPGaming_ServerList_DataWriter_ServerList');
$dw->setExistingData($id);
$dw->delete();

return $this->responseRedirect(
XenForo_ControllerResponse_Redirect::SUCCESS,
XenForo_Link::buildAdminLink('servers')
);
} else {
$viewParams = array('server' => $server);

return $this->responseView('UFPGaming_ServerList_ViewAdmin_ServerList_Delete', 'serverlist_delete', $viewParams);
}
}




protected function _getServerOrError($id, array $fetchOptions = array())
{
$info = $this->_getAllServers()->getServerById($id, $fetchOptions);

if (empty($info)) {
throw $this->responseException($this->responseError(new XenForo_Phrase('server_list_no_servers_admin'), 404));
}

return $info;
}


/**
* @return UFPGaming_ServerList_Model_ServerList
*/
protected function _getAllServers()
{
/** @noinspection PhpIncompatibleReturnTypeInspection */
return $this->getModelFromCache('UFPGaming_ServerList_Model_ServerList');
}
}
I've been trying to fix this for two days now so if anybody has any information on what I've done wrong, please let me know.
 

Boothby

Enthusiast
Joined
Sep 25, 2012
Messages
132
I have not looked much deep into your code, but at a short view I noticed that the method getServerById() calls getAllServers() with parameters that are not defined there. You could change that with some logic to differienciate between all servers and server by id or you could change the the method getServerById() like:

PHP:
    public function getServerById($id) // removed the $fetchOptions part
    {
        return $this->_getDb()->fetchRow('
            SELECT *
            FROM ufp_serverlist
            WHERE server_id = ?
        ', $id);
    }
 

Fillip H.

Developer
Joined
Mar 13, 2006
Messages
208
I have not looked much deep into your code, but at a short view I noticed that the method getServerById() calls getAllServers() with parameters that are not defined there. You could change that with some logic to differienciate between all servers and server by id or you could change the the method getServerById() like:

PHP:
    public function getServerById($id) // removed the $fetchOptions part
    {
        return $this->_getDb()->fetchRow('
            SELECT *
            FROM ufp_serverlist
            WHERE server_id = ?
        ', $id);
    }
Removing fetchOptions may not be a good idea as it will limit functionality for extensions to this code. If the project won't be released to the public, that isn't an issue, but if it will be, it should be written with 3rd party developers in mind.


Fillip
 
Top