DataWriter for Relation-Like Database Table

Lukas W.

Well-known member
Hey,

I've created a relation table for an AddOn of the following form:
table.webp

I'm trying to update the table with my DataWriter, but I end up either creating a duplicate entry or getting the following error:
  • The existing data required by the data writer could not be found.
  • Please enter a value for the required field 'resource_id'.
  • Please enter a value for the required field 'member_id'.

My DataWriter:
PHP:
<?php

class ResourceToRPG_DataWriter_RPGMember extends XenForo_DataWriter {
    protected function _getFields() {
        return array(
            'xf_rtr_members' => array(
                'id'            => array('type' => self::TYPE_UINT, 'autoIncrement' => true),
                'resource_id'   => array('type' => self::TYPE_UINT, 'required' => true),
                'member_id'     => array('type' => self::TYPE_UINT, 'required' => true),
                'status'  => array('type' => self::TYPE_STRING, 'default' => 'Approved',
                    'allowedValues' => array('Approved', 'Unapproved', 'Moderator')
                )
            )
        );
    }
   
    protected function _getExistingData($data)
    {
        if (!is_array($data))
            return false;
        else if (isset($data['member_id'], $data['resource_id']))
        {
            $memberId = $data['member_id'];
            $resourceId = $data['resource_id'];
        }
        else
            return false;

        return array('xf_rtr_members' => $this->_getResourceModel()->getMemberState($memberId, $resourceId));
    }
   
    protected function _getUpdateCondition($tableName) {
        return 'resource_id = '.$this->_db->quote($this->getExisting('resource_id')).
                ' AND member_id = '.$this->_db->quote($this->getExisting('member_id'));
    }
   
    protected function _getResourceModel() {
        return $this->getModelFromCache('XenResource_Model_Resource');
    }
}

The getMemberStatus-Function in the Model:
PHP:
    public function getMemberState($idr,$idu) {
        return $this->_getDb()->fetchRow('
            SELECT *
            FROM xf_rtr_members
            WHERE member_id = ?
            AND resource_id = ?

        ', array(
            $idu,
            $idr
        ));
    }

And the snippet of the Save-Call in the ControllerPublic:
PHP:
        $resource['resource_id'] = $this->_input->filterSingle('rid', XenForo_Input::UINT);
        $users = $this->_input->filterSingle('user_ids', XenForo_Input::ARRAY_SIMPLE);
        $action = $this->_input->filterSingle('do', XenForo_Input::STRING);
       
        $dw = XenForo_DataWriter::create('ResourceToRPG_DataWriter_RPGMember');
        //Define Action
        switch($action) {
            case 'approve':
                foreach($users as $user) {
                    $data = array('resource_id' => $resource['resource_id'], 'member_id' => $user);
                    $dw->setExistingData($data);
                    $dw->set('status','Approved');
                    $dw->save();
                }
                break;

Someone has a hint for me where my mistake lies?
 
Wow, this is so awkward...
I've called "public function getMemberState($idr,$idu)" with the wrong parameter order...
Guess the topic can be closed. :oops:
 
Back
Top Bottom