DB Problem

Matthew Hawley

Well-known member
For my categories I added an option to add descriptions.

BYMHxP4.png


But when I put something in the description, click save, and go back to editing, it isn't there. I think this is a DB problem, how would I fix this? :)
 
Where is the template for the option bit?

Look for
<xen:textboxunit label="{xen:phrase cf_directory_category_desc}:" name="message" rows="2" inputclass="Elastic">

Code:
<xen:title>{xen:if {$page.page_id}, '{xen:phrase edit_page}: {$page.title}', '{xen:phrase create_new_page}'}</xen:title>

<xen:if is="{$page.page_id}">
    <xen:navigation>
        <xen:breadcrumb href="{xen:adminlink directory, $page}">{$page.title}</xen:breadcrumb>
    </xen:navigation>
</xen:if>

<xen:form action="{xen:adminlink 'directory/save'}"
    class="AutoValidator"
    data-redirect="on">
    <xen:if is="{$page.page_id}">
        <input type="hidden" name="page_id" value="{$page.page_id}" /> 
    </xen:if>

    <xen:textboxunit name="title" value="{$page.title}" label="{xen:phrase title}:">
        <xen:explain>{xen:phrase cf_directory_title_desc}</xen:explain>
    </xen:textboxunit>

    <xen:textboxunit name="route" value="{$page.route}" label="{xen:phrase cf_directory_route}:">
        <xen:explain>{xen:phrase cf_directory_route_desc}</xen:explain>
    </xen:textboxunit>
 
    <xen:textboxunit label="{xen:phrase cf_directory_category_desc}:" name="message" rows="2" inputclass="Elastic">
        <xen:hint>{xen:phrase you_may_use_html}</xen:hint>
        <xen:explain>{xen:phrase cf_directory_category_desc_explain}</xen:explain>
    </xen:textboxunit>
 
    <xen:textboxunit name="display_order" value="{$page.display_order}" label="{xen:phrase display_order}:">
        <xen:explain>{xen:phrase cf_directory_display_desc}</xen:explain>
    </xen:textboxunit>

    <xen:submitunit save="{xen:phrase save_page}">
        <xen:if is="{$page.page_id}">
            <input type="button" value="{xen:phrase cf_directory_delete_category}..." accesskey="d" class="button OverlayTrigger"
                data-href="{xen:adminlink directory/delete-confirm, $page}" />
        </xen:if>
    </xen:submitunit>
</xen:form>
 
See value="{$page.display_order}. That is wrong - check the $page array to see which one is the correct one.
 
Actually I'm pretty sure thats not it. I don't think I have description defined or something.

Here is the code for the ControllerAdmin file.

Code:
<?php



class CF_Directory_ControllerAdmin_Directory extends XenForo_ControllerAdmin_Abstract

{

    protected function _preDispatch($action)

    {

        $this->assertAdminPermission('cfDirectory');

    }



    /**

     *

     * @return    XenForo_ControllerResponse_View

     */

    public function actionIndex()

    {

        $pageModel = $this->_getPageModel();

      

        $viewParams = array(

            'pages'    => $pageModel->getAllPages()

        );



        return $this->responseView('CF_Directory_ViewAdmin_Page_List', 'cf_directory_pages_list', $viewParams);

    }



    /**

     * Presents form for editing a page.

     *

     * @return    XenForo_ControllerResponse_Error|XenForo_ControllerResponse_View

     */

    public function actionEdit()

    {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::UINT);



        $viewParams = array(

                'page' => XenForo_Model::create('CF_Directory_Model_Page')->getPageById($pageId)

            );



        return $this->responseView('CF_Directory_ViewAdmin_Page_Add', 'cf_directory_page_add', $viewParams);

    }



    /**

     * Presents form for adding a new page.

     *

     * @return    XenForo_ControllerResponse_View

     */

    public function actionAdd()

    {



        $viewParams = array(

                'page' => array(

                    'title'                 => '',

                    'description'             => '',

                    'category_id'             => 0,

                    'display_order' => 1,

                    'route'                 => ''

                )

            );

        return $this->responseView('CF_Directory_ViewAdmin_Page_Add', 'cf_directory_page_add', $viewParams);

    }

  

    public function actionSave()

    {

        $this->_assertPostOnly();

      

        $input = $this->_input->filter(array(

            'route' => XenForo_Input::STRING,

            'display_order' => XenForo_Input::UINT
          

        ));

      

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::UINT);

        $pageWriter = XenForo_DataWriter::create('CF_Directory_DataWriter_Page');



        if ($pageId) {

            $pageWriter->setExistingData($pageId);

        }

      

        $pageWriter->bulkSet($input);

        $pageWriter->save();

      

        $lastPage = $pageWriter->getMergedData();

      

        $data = array(

            'title'              => 'cf_directory_page_' . $lastPage['page_id'],

            'language_id'  => 0,

            'global_cache' => 0,

            'addon_id'          => 'CFDirectory'

        );

      

        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Phrase');



        if ($pageId) {

            $phrases = XenForo_Model::create('XenForo_Model_Phrase')->getPhraseIdInLanguagesByTitle($data['title']);

            $writer->setExistingData($phrases[0], true);

        }



        $writer->set('phrase_text', $this->_input->filterSingle('title', XenForo_Input::STRING));



        if (!$pageId) {

            $writer->bulkSet($data);

        }



        $writer->save();

      

        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Template');



        if ($pageId) {

            $template_id = XenForo_Model::create('XenForo_Model_Template')->getTemplateInStyleByTitle('cf_directory_page.' . $lastPage['page_id']);

            $writer->setExistingData($template_id);

        } else {

            $writer->set('title', 'cf_directory_page.' . $lastPage['page_id']);

            $writer->set('style_id', 0);

            $writer->set('addon_id', 'CFDirectory');

        }



        $writer->set('template', $this->_input->filterSingle('template', XenForo_Input::STRING));

        $writer->save();



        return $this->responseRedirect(

            XenForo_ControllerResponse_Redirect::SUCCESS,

            XenForo_Link::buildAdminLink('directory')

        );

    }



    /**

     * Deletes a page.

     *

     * @return    XenForo_ControllerResponse_Redirect|XenForo_ControllerResponse_View

     */

    public function actionDelete()

    {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::INT);



        if ($this->isConfirmedPost()) {

            $pageWriter = XenForo_DataWriter::create('CF_Directory_DataWriter_Page');

            $pageWriter->setExistingData($pageId);

            $pageWriter->delete();



            return $this->responseRedirect(

                XenForo_ControllerResponse_Redirect::SUCCESS,

                XenForo_Link::buildAdminLink('directory')

            );

        } else {

            return $this->responseReroute('CF_Directory_ControllerAdmin_Directory', 'deleteConfirm');

        }

    }



    public function actionDeleteConfirm() {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::INT);



        $viewParams = array(

                'page' => $this->_getPageModel()->getPageById($pageId)

            );



        return $this->responseView('XenForo_ViewAdmin_Page_Delete', 'cf_directory_page_delete', $viewParams);

    }



    /**

     * @return    CF_Directory_Model_Page

     */

    protected function _getPageModel()

    {

        return $this->getModelFromCache('CF_Directory_Model_Page');

    }

}

?>
 
Actually I'm pretty sure thats not it. I don't think I have description defined or something.

Here is the code for the ControllerAdmin file.

Code:
<?php



class CF_Directory_ControllerAdmin_Directory extends XenForo_ControllerAdmin_Abstract

{

    protected function _preDispatch($action)

    {

        $this->assertAdminPermission('cfDirectory');

    }



    /**

     *

     * @return    XenForo_ControllerResponse_View

     */

    public function actionIndex()

    {

        $pageModel = $this->_getPageModel();

     

        $viewParams = array(

            'pages'    => $pageModel->getAllPages()

        );



        return $this->responseView('CF_Directory_ViewAdmin_Page_List', 'cf_directory_pages_list', $viewParams);

    }



    /**

     * Presents form for editing a page.

     *

     * @return    XenForo_ControllerResponse_Error|XenForo_ControllerResponse_View

     */

    public function actionEdit()

    {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::UINT);



        $viewParams = array(

                'page' => XenForo_Model::create('CF_Directory_Model_Page')->getPageById($pageId)

            );



        return $this->responseView('CF_Directory_ViewAdmin_Page_Add', 'cf_directory_page_add', $viewParams);

    }



    /**

     * Presents form for adding a new page.

     *

     * @return    XenForo_ControllerResponse_View

     */

    public function actionAdd()

    {



        $viewParams = array(

                'page' => array(

                    'title'                 => '',

                    'description'             => '',

                    'category_id'             => 0,

                    'display_order' => 1,

                    'route'                 => ''

                )

            );

        return $this->responseView('CF_Directory_ViewAdmin_Page_Add', 'cf_directory_page_add', $viewParams);

    }

 

    public function actionSave()

    {

        $this->_assertPostOnly();

     

        $input = $this->_input->filter(array(

            'route' => XenForo_Input::STRING,

            'display_order' => XenForo_Input::UINT
         

        ));

     

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::UINT);

        $pageWriter = XenForo_DataWriter::create('CF_Directory_DataWriter_Page');



        if ($pageId) {

            $pageWriter->setExistingData($pageId);

        }

     

        $pageWriter->bulkSet($input);

        $pageWriter->save();

     

        $lastPage = $pageWriter->getMergedData();

     

        $data = array(

            'title'              => 'cf_directory_page_' . $lastPage['page_id'],

            'language_id'  => 0,

            'global_cache' => 0,

            'addon_id'          => 'CFDirectory'

        );

     

        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Phrase');



        if ($pageId) {

            $phrases = XenForo_Model::create('XenForo_Model_Phrase')->getPhraseIdInLanguagesByTitle($data['title']);

            $writer->setExistingData($phrases[0], true);

        }



        $writer->set('phrase_text', $this->_input->filterSingle('title', XenForo_Input::STRING));



        if (!$pageId) {

            $writer->bulkSet($data);

        }



        $writer->save();

     

        $writer = XenForo_DataWriter::create('XenForo_DataWriter_Template');



        if ($pageId) {

            $template_id = XenForo_Model::create('XenForo_Model_Template')->getTemplateInStyleByTitle('cf_directory_page.' . $lastPage['page_id']);

            $writer->setExistingData($template_id);

        } else {

            $writer->set('title', 'cf_directory_page.' . $lastPage['page_id']);

            $writer->set('style_id', 0);

            $writer->set('addon_id', 'CFDirectory');

        }



        $writer->set('template', $this->_input->filterSingle('template', XenForo_Input::STRING));

        $writer->save();



        return $this->responseRedirect(

            XenForo_ControllerResponse_Redirect::SUCCESS,

            XenForo_Link::buildAdminLink('directory')

        );

    }



    /**

     * Deletes a page.

     *

     * @return    XenForo_ControllerResponse_Redirect|XenForo_ControllerResponse_View

     */

    public function actionDelete()

    {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::INT);



        if ($this->isConfirmedPost()) {

            $pageWriter = XenForo_DataWriter::create('CF_Directory_DataWriter_Page');

            $pageWriter->setExistingData($pageId);

            $pageWriter->delete();



            return $this->responseRedirect(

                XenForo_ControllerResponse_Redirect::SUCCESS,

                XenForo_Link::buildAdminLink('directory')

            );

        } else {

            return $this->responseReroute('CF_Directory_ControllerAdmin_Directory', 'deleteConfirm');

        }

    }



    public function actionDeleteConfirm() {

        $pageId = $this->_input->filterSingle('page_id', XenForo_Input::INT);



        $viewParams = array(

                'page' => $this->_getPageModel()->getPageById($pageId)

            );



        return $this->responseView('XenForo_ViewAdmin_Page_Delete', 'cf_directory_page_delete', $viewParams);

    }



    /**

     * @return    CF_Directory_Model_Page

     */

    protected function _getPageModel()

    {

        return $this->getModelFromCache('CF_Directory_Model_Page');

    }

}

?>

What about the DataWriter code?
 
This is the data writer code.

Code:
<?php



/**

* Data writer for Pages.

*/

class CF_Directory_DataWriter_Page extends XenForo_DataWriter

{

    protected function _getFields()

    {

        return array(

            'xf_cf_directory_pages'    => array(

                'page_id'                => array(

                    'type'                    => self::TYPE_UINT,

                    'autoIncrement'    => true

                ),

                'route'                    => array(

                    'type'                    => self::TYPE_STRING,

                    'required'            => true,

                    'requiredError'    => 'please_enter_valid_title',

                    'maxLength'            => 25

                ),

                'display_order'    => array(

                    'type'            => self::TYPE_UINT,

                    'default'        => 1

                )

            )

        );

    }



    protected function _getExistingData($data)

    {

        if(!$pageId = $this->_getExistingPrimaryKey($data, 'page_id'))

        {

            return false;

        }



        return array('xf_cf_directory_pages' => $this->_getPageModel()->getPageById($pageId));

    }



    protected function _getUpdateCondition($tableName)

    {

        return 'page_id = ' . $this->_db->quote($this->getExisting('page_id'));

    }



    protected function _postDelete() {

        $page = $this->getMergedData();

        $template = $this->getModelFromCache('XenForo_Model_Template')->getTemplateInStyleByTitle('cf_directory_page.' . $page['page_id']);

        if ($template)

        {

            $dw = XenForo_DataWriter::create('XenForo_DataWriter_Template', XenForo_DataWriter::ERROR_SILENT);

            $dw->setExistingData($template, true);

            $dw->delete();

        }



        $phrases = XenForo_Model::create('XenForo_Model_Phrase')->getPhraseIdInLanguagesByTitle('cf_directory_page_' . $page['page_id']);

        $dw = XenForo_DataWriter::create('XenForo_DataWriter_Phrase', XenForo_DataWriter::ERROR_SILENT);

        $dw->setExistingData($phrases[0], true);

        $dw->delete();



        parent::_postDelete();

    }



    /**

     * @return    CF_Directory_Model_Category

     */

    protected function _getPageModel()

    {

        return $this->getModelFromCache('CF_Directory_Model_Page');

    }

}

?>
 
This is the data writer code.

Code:
<?php



/**

* Data writer for Pages.

*/

class CF_Directory_DataWriter_Page extends XenForo_DataWriter

{

    protected function _getFields()

    {

        return array(

            'xf_cf_directory_pages'    => array(

                'page_id'                => array(

                    'type'                    => self::TYPE_UINT,

                    'autoIncrement'    => true

                ),

                'route'                    => array(

                    'type'                    => self::TYPE_STRING,

                    'required'            => true,

                    'requiredError'    => 'please_enter_valid_title',

                    'maxLength'            => 25

                ),

                'display_order'    => array(

                    'type'            => self::TYPE_UINT,

                    'default'        => 1

                )

            )

        );

    }



    protected function _getExistingData($data)

    {

        if(!$pageId = $this->_getExistingPrimaryKey($data, 'page_id'))

        {

            return false;

        }



        return array('xf_cf_directory_pages' => $this->_getPageModel()->getPageById($pageId));

    }



    protected function _getUpdateCondition($tableName)

    {

        return 'page_id = ' . $this->_db->quote($this->getExisting('page_id'));

    }



    protected function _postDelete() {

        $page = $this->getMergedData();

        $template = $this->getModelFromCache('XenForo_Model_Template')->getTemplateInStyleByTitle('cf_directory_page.' . $page['page_id']);

        if ($template)

        {

            $dw = XenForo_DataWriter::create('XenForo_DataWriter_Template', XenForo_DataWriter::ERROR_SILENT);

            $dw->setExistingData($template, true);

            $dw->delete();

        }



        $phrases = XenForo_Model::create('XenForo_Model_Phrase')->getPhraseIdInLanguagesByTitle('cf_directory_page_' . $page['page_id']);

        $dw = XenForo_DataWriter::create('XenForo_DataWriter_Phrase', XenForo_DataWriter::ERROR_SILENT);

        $dw->setExistingData($phrases[0], true);

        $dw->delete();



        parent::_postDelete();

    }



    /**

     * @return    CF_Directory_Model_Category

     */

    protected function _getPageModel()

    {

        return $this->getModelFromCache('CF_Directory_Model_Page');

    }

}

?>
I think in the _getFields method you are missing the description and probably some other column definitions. So the DataWriter is not aware of those fields, that's probably why they are not saved.
 
The DataWriter doesn't need to know about the description if he's structured the data storage based on Help Manager, which, judging my the code, he has.
 
The DataWriter doesn't need to know about the description if he's structured the data storage based on Help Manager, which, judging my the code, he has.

I though that if the a column is not defined in the _getFields it will not be saved. Or is that method used for validation purposes only?
 
You are correct, but he isn't storing the description within the table (because I'm not storing the same field in the table).
 
You are correct, but he isn't storing the description within the table (because I'm not storing the same field in the table).
Got it. Yeah i see that the ControllerAdmin is setting the values. His description field name is 'message' yet it's never set in the actionSave method. He has reference to a template field

PHP:
$writer->set('template', $this->_input->filterSingle('template', XenForo_Input::STRING));

which never gets set since that field is not present in this form. If he'll change that to

PHP:
$writer->set('message', $this->_input->filterSingle('message', XenForo_Input::STRING));

the description will probably save.

Not sure what he is doing but that makes more sense now, to me.
 
Back
Top Bottom