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

Extending Xenforo_DataWriter to other directories

Garani

Active member
#1
I am going forward with my module and I getting stuck on the dataDriver. It is a given that I could just low level on SQL and raw db calls, but I wanted to keep things within the framewrk.

What I am trying to achieve:
I want to gather some more data when submitting a thread and save this data in a table.

Where is the data going:
To a table newly made. This is because I do not want to touch and screw the xf_ tables in the DB

How I am doing it:
I have make a DataWriter directory within my module and there I put a Myaddon.php file containing the following:

PHP:
/**
* Data writer for Myaddon.
*
* @package IIO_DataWriter_Myaddon
*/
class IIO_DataWriter_Myaddon extends XenForo_DataWriter
{
    const DATA_REBUILD_CACHES = 'rebuildCaches';

    /**
     * Title of the phrase that will be created when a call to set the
     * existing data fails (when the data doesn't exist).
     *
     * @var string
     */
    protected $_existingDataErrorPhrase = 'iio_requested_myaddon_not_found';

    /**
    * Gets the fields that are defined for the table. See parent for explanation.
    *
    * @return array
    */
    protected function _getFields()
    {
        return array(
            'iio_myaddon' => array(
                'myaddon_id'
                    => array('type' => self::TYPE_UINT, 'autoIncrement' => true, 'verification' => array('IIO_DataWriter_Helper_Deal', 'verifyDealId')),
                'image_url'
                    => array('type' => self::TYPE_STRING, 'required' => true, 'maxLength' => 250),
                'thread_id'
                    => array('type' => self::TYPE_UINT, 'default' => 0),
                'first_post_id'
                    => array('type' => self::TYPE_UINT, 'default' => 0),
            ),
        );
    }
}
When I call the Xenforo_DataWriter::create('IIO_DataWriter_Myaddon') it does not get autoloaded and I get the follong error: Invalid data writer 'IIO_DataWriter_Myaddon' specified

This means that I am not able to correctly autoload the file. What is the best practice in extending the DataDriver for Addons?
 

Shadab

Well-known member
#2
Make sure the file path is: /library/IIO/DataWriter/Myaddon.php
And that you implement all the abstract methods specified in the base XenForo_DataWriter class.

At the very least your datawriter needs to have these 3 methods:
  • protected function _getFields();
  • protected function _getExistingData($data);
  • protected function _getUpdateCondition($tableName);