How do I add an input field in the Admin CP and create table

I'm not understanding how to make the following code into a DataWriter.

PHP:
    // get new_post_date from overlay     
     $newSmilieCount = $this->_input->filterSingle('new_smilie_count', XenForo_Input::STRING);
     
     // save $newSmilieCount
     $db = XenForo_Application::getDb();   
     $db->query("
       UPDATE andy_smilie_count
       SET max_smilie = '$newSmilieCount'
       WHERE max_smilie <> ''
     ");

I would love to get specific instruction like:

1) Where to create a new folder and file
2) The contents of the new file
3) Show the DataWriter code that calls this new file

Thank you.
 
1) /Andy/DataWriter/Smilie.php
2) I'm not writing the file for you, you have to look at the abstract class or other datawriters that XenForo wrote and figure it out. Not trying to be mean.
3)
PHP:
XenForo_DataWriter::create('DATAWRITERCLASS');

Replace DATAWRITERCLASS with the name of your data writer class (if you use my file name that I gave you it'd be Andy_DataWriter_Smilie)
 
Hi Daniel,

I appreciate your help and I understand that if I spend time figuring things out it will be better for me.

1) library/Andy/SmilieCount/DataWriter/Smilie.php

So I create this file, but I'm not at all sure what is suppose to go in there. I take it it's not the query I wrote. Is it part of the query I wrote?
 
No; You actually don't need to write a query.

You need to look for and modify the following functions: "_getFields(), _getUpdateCondition($tableName), _getExistingData($data)"
 
It means create those functions with the content that needs to be in them. Find out what needs to be in them by looking at other data writers.
 
So would Smilie.php look something like this?

PHP:
<?php

class Andy_SmilieCount_DataWriter_Smilie extends XenForo_DataWriter
{
   public function _getFields()
   {     

   }
   
   public function _getUpdateCondition($tableName)
   {     

   }
   
   public function _getExistingData($data)
   {     

   }
}

?>
 
Yes, but with actual content obviously. You need to extend the DataWriter to apply to your context.
 
SmilieCount.php

Am I on the right track?

PHP:
<?php

class Andy_SmilieCount_DataWriter_SmilieCount extends XenForo_DataWriter
{
  
   public function _getFields()
   {
     $_fields = array('max_smilie');
   }
  
   public function _getUpdateCondition($tableName)
   {    
     $tableName = 'andy_smilie_count';
     return $tableName;
   }
  
   public function _getExistingData($data)
   {
     $existing = $this->_getExistingData($data);
   }
}

?>
 
Last edited:
Still not working. Hopefully I can get a little help.

Tools.php

PHP:
    $dw = XenForo_DataWriter::create('Andy_SmilieCount_DataWriter_SmilieCount');
     if ($update)
     {
       $dw->setExistingData($existingData);
     }
     $dw->set('max_smilie', $newSmilieCount);
     $dw->save();
 
Last edited:
When I try to run this code I get the following error:

pic001.webp

For some reason the following line is incorrect:

$dw->set('max_smilie', $newSmilieCount);
 
Sorry, you have to populate those functions with your data. Since you're not extending a datawriter (even though you're extending the core datawriter) you do not need to both calling $this->function() like that (which parent::function() is the correct way in any case).

You need to read other datawriters and see what they did in the functions I listed. You need to have similar content in those functions. Obviously changing database fields to be yours, table names to be yours, etc...
 
The problem is I don't know how to test if the DataWriter is working at all. The error message seams to indicate the SmilieCount.php file never even gets loaded.
 
PHP:
$dw->setExistingData($existingData);

$existingData should be an integer value (it's the most easiest way). You have to tell your custom DataWriter how to get the existing value, so you have to create a function in your custom model like ->getXxxId($xxxId);

You changed an existing data record in your Date Change Addon. Existing Data was $postId. If you take a look at DataWriter_DiscussionMessage_Post, I bet you see a function that fetches the Post_Id. So If you write

PHP:
$dw->setExistingData($existingData);

... the DataWriter will execute this model function which is used in the Datawriter for Posts, maybe something like ->getPostId($postId);

I think you have to modify public function _getExistingData($data). I would take a look at how other addons do this, as well as the current functions like DataWriter_Model_Post is doing it. They also extend the DataWriter like you do, and so all DataWriters have to add a function to fetch the existing data.
 
This is an excerpt from a DataWriter to show you how your DataWriter could look like:
PHP:
<?php

class Shop_DataWriter_Cart extends XenForo_DataWriter
{
   protected function _getFields()
   {
     return array(
       'shop_cart' => array(
         'cart_id'  => array('type' => self::TYPE_STRING, 'autoIncrement' => true),
         'date'  => array('type' => self::TYPE_STRING, 'required' => true,  'default' => XenForo_Application::$time),
       )
     );
   }

   protected function _getExistingData($data)
   {
     if (!$id = $this->_getExistingPrimaryKey($data, 'cart_id'))
     {
       return false;
     }

     return array('shop_cart' => $this->getModelFromCache('Shop_Model_Cart')->getCartById($id));
   }

   protected function _getUpdateCondition($tableName)
   {
     return 'cart_id = ' . $this->_db->quote($this->getExisting('cart_id'));
   }
}
 
I created the SimpleText add-on (see post #54) and now how much better understanding of how to use the DataWriter.
 
This is an excerpt from a DataWriter to show you how your DataWriter could look like:
PHP:
<?php

class Shop_DataWriter_Cart extends XenForo_DataWriter
{
   protected function _getFields()
   {
     return array(
       'shop_cart' => array(
         'cart_id'  => array('type' => self::TYPE_STRING, 'autoIncrement' => true),
         'date'  => array('type' => self::TYPE_STRING, 'required' => true,  'default' => XenForo_Application::$time),
       )
     );
   }

   ...

Maybe you, Andy or someone else can help me answer this question. Why does _getFields() return an arrays of arrays? And further, why are we self::TYPE_STRING'ng them? In other words, how does this return statement relate to the 'shop_cart' table? Obviously the 'cart-id' and 'date' are fields, yes - but why are we setting them to an array value?

Thanks
 
Top Bottom