Array to string conversion

Naz

XenForo developer
Staff member
I have some code and it's meant to save the contents of a form in to the db. It's slightly modified from another modification (where it works perfectly). However, in it's current state I get an error about array to string conversion.
Code:
ErrorException: Array to string conversion - library\Zend\Db\Statement\Mysqli.php:208
Generated By: NixFifty, 36 minutes ago
Stack Trace

#0 [internal function]: XenForo_Application::handlePhpError(8, 'Array to string...', 'F:\xampp\htdocs...', 208, Array)
#1 F:\xampp\htdocs\*****\library\Zend\Db\Statement\Mysqli.php(208): mysqli_stmt->execute()
#2 F:\xampp\htdocs\*****\library\Zend\Db\Statement.php(317): Zend_Db_Statement_Mysqli->_execute(Array)
#3 F:\xampp\htdocs\*****\library\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array)
#4 F:\xampp\htdocs\*****\library\Zend\Db\Adapter\Abstract.php(574): Zend_Db_Adapter_Abstract->query('INSERT INTO `ni...', Array)
#5 F:\xampp\htdocs\*****\library\XenForo\DataWriter.php(1612): Zend_Db_Adapter_Abstract->insert('nixfifty_coupon...', Array)
#6 F:\xampp\htdocs\*****\library\XenForo\DataWriter.php(1601): XenForo_DataWriter->_insert()
#7 F:\xampp\htdocs\*****\library\XenForo\DataWriter.php(1393): XenForo_DataWriter->_save()
#8 F:\xampp\htdocs\*****\library\NixFifty\UpgradesPlus\Model\Coupons.php(42): XenForo_DataWriter->save()
#9 F:\xampp\htdocs\*****\library\NixFifty\UpgradesPlus\ControllerAdmin\Coupons.php(88): NixFifty_UpgradesPlus_Model_Coupons->saveCoupon(Array)
#10 F:\xampp\htdocs\*****\library\XenForo\FrontController.php(347): NixFifty_UpgradesPlus_ControllerAdmin_Coupons->actionSave()
#11 F:\xampp\htdocs\*****\library\XenForo\FrontController.php(134): XenForo_FrontController->dispatch(Object(XenForo_RouteMatch))
#12 F:\xampp\htdocs\*****\admin.php(13): XenForo_FrontController->run()
#13 {main}

Here's my DataWriter:

Code:
<?php

class NixFifty_UpgradesPlus_DataWriter_Coupons extends XenForo_DataWriter
{
    protected $_existingDataErrorPhrase = 'requested_coupon_could_not_be_found';

    protected function _getFields()
    {
        return array(
            'nixfifty_coupons' => array(
                'coupon_id'      => array('type' => self::TYPE_UINT,    'autoIncrement' => true),
                'title'                => array('type' => self::TYPE_STRING,  'required' => true, 'maxLength' => 50,
                        'requiredError' => 'please_enter_valid_title'
                ),
                'description'          => array('type' => self::TYPE_STRING,  'default' => ''),
                'code'      => array('type' => self::TYPE_STRING,  'required' => true, 'maxLength' => 50,
                        'requiredError' => 'please_enter_valid_unique_code',
                        'verification' => array('NixFifty_UpgradesPlus_DataWriter_Helper_Coupon', 'verifyCouponCode')
                ),
                'percentage_off'      => array('type' => self::TYPE_UINT, 'required' => true, 'maxLength' => 2,
                        'requiredError' => 'please_enter_percentage_between_1_99',
                        'verification' => array('$this', '_verifyPercentageAmount')
                ),
                'active'         => array('type' => self::TYPE_BOOLEAN, 'default' => 1),
                'eligible_upgrades'      => array('type' => self::TYPE_UNKNOWN, 'default' => ''),
                'eligible_gifts'      => array('type' => self::TYPE_UNKNOWN, 'default' => ''),
                'eligible_groups'      => array('type' => self::TYPE_UNKNOWN, 'default' => '',
                        'verification' => array('XenForo_DataWriter_Helper_User', 'verifyExtraUserGroupIds')),
                'end_date'          => array('type' => self::TYPE_UINT, 'required' => true),
            )
        );
    }

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

        return array('nixfifty_coupons' => $this->_getCouponModel()->getCouponById($id));
    }

    protected function _getUpdateCondition($tableName)
    {
        return 'coupon_id = ' . $this->_db->quote($this->getExisting('coupon_id'));
    }

    protected function _getCouponModel()
    {
        return $this->getModelFromCache('NixFifty_UpgradesPlus_Model_Coupons');
    }
   
    protected function _verifyPercentageAmount(&$percentage)
    {
        if ($percentage <= 0)
        {
            $this->error(new XenForo_Phrase('please_enter_a_percentage_amount_greater_than_zero'), 'percentage_off');
            return false;
        }
        else
        {
            return true;
        }
    }
   
}

The ControllerAdmin method that handles saving.
Code:
public function actionSave()
    {
        $input = $this->_input->filter(array(
                'coupon_id' => XenForo_Input::UINT,
                'title' => XenForo_Input::STRING,
                'description' => XenForo_Input::STRING,
                'code' => XenForo_Input::STRING,
                'percentage_off' => XenForo_Input::UINT,
                'active' => XenForo_Input::UINT,
                'eligible_upgrades' => array(XenForo_Input::UINT, 'array' => true),
                'eligible_gifts' => array(XenForo_Input::UINT, 'array' => true),
                'eligible_groups' => array(XenForo_Input::UINT, 'array' => true),
                'end_date' => XenForo_Input::DATE_TIME
        ));
       
        if (!$this->getModelFromCache('NixFifty_UpgradesPlus_Model_Coupons')->saveCoupon($input))
        {
            return $this->responseError(new XenForo_Phrase('failed_to_save_coupon'));
        }
       
        return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, XenForo_Link::buildAdminLink('coupons'));
    }

And my model:

Code:
class NixFifty_UpgradesPlus_Model_Coupons extends XenForo_Model
{
    /**
     * Gets a specific upgrade by it's ID.
     *
     * @param integer id
     *
     * @return array
     */
    public function getCouponById($id)
    {
        $db = $this->_getDb();
        return $db->fetchRow('SELECT * FROM nixfifty_coupons WHERE coupon_id = ?', $id);
    }
   
    /**
     * Handles saving of a coupon.
     *
     * @param array $coupon
     * @return boolean
     */
    public function saveCoupon($coupon)
    {
        $dw = XenForo_DataWriter::create('NixFifty_UpgradesPlus_DataWriter_Coupons');
        if (isset($coupon['coupon_id']) && $coupon['coupon_id'] >= 1)
        {
            $dw->setExistingData($coupon['coupon_id']);
        }
        $dw->bulkSet($coupon);
        if($dw->save())
        {
            return true;
        }
        return false;
    }
}

Could someone point out my mistake? I can't figure it out for the life of me. Thanks.
 
Are these BLOBS: eligible_upgrades, eligible_gifts ,and eligible_groups? If they are, those three in your ControllerAdmin should read in the values like this, for example: 'eligible_gifts' => XenForo_Input::ARRAY_SIMPLE

The datawriter should be 'eligible_gifts' => array('type' => self::TYPE_SERIALIZED, 'default' => '')
 
  • Like
Reactions: Naz
Are these BLOBS: eligible_upgrades, eligible_gifts ,and eligible_groups? If they are, those three in your ControllerAdmin should read in the values like this, for example: 'eligible_gifts' => XenForo_Input::ARRAY_SIMPLE

The datawriter should be 'eligible_gifts' => array('type' => self::TYPE_SERIALIZED, 'default' => '')
Hi Lawrence. Thanks for the reply. Initially they were VARCHARs, however I went ahead and changed the datatype to BLOB and did as you instructed and it worked. Thank you :)
 
Back
Top Bottom