[XF1] ID Problems - Broken Model?

Discussion in 'XenForo' started by Igneous, Apr 3, 2018.

  1. Igneous

    Igneous Participant

    64
    13
    +15
    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.
     
  2. Boothby

    Boothby Enthusiast

    132
    43
    +100
    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);
        }
     
  3. Fillip H.

    Fillip H. Developer

    208
    90
    +288
    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
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.