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

DB Problem

Discussion in 'XenForo Development Discussions' started by Matthew Hawley, Jan 15, 2014.

  1. Matthew Hawley

    Matthew Hawley Well-Known Member

    For my categories I added an option to add descriptions.

    [​IMG]

    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? :)
     
  2. tyteen4a03

    tyteen4a03 Well-Known Member

    Where is the template for the option bit?
     
  3. Matthew Hawley

    Matthew Hawley Well-Known Member

    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>
     
  4. tyteen4a03

    tyteen4a03 Well-Known Member

    See value="{$page.display_order}. That is wrong - check the $page array to see which one is the correct one.
     
  5. Matthew Hawley

    Matthew Hawley Well-Known Member

    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');
    
        }
    
    }
    
    ?>
     
  6. Vincent Gabriel

    Vincent Gabriel Active Member

    What about the DataWriter code?
     
  7. Matthew Hawley

    Matthew Hawley Well-Known Member

    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');
    
        }
    
    }
    
    ?>
     
  8. Jeremy

    Jeremy XenForo Moderator Staff Member

    Not using someone else's code pretty much word for word would do wonders for helping you learn.
     
  9. Vincent Gabriel

    Vincent Gabriel Active Member

    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.
     
  10. Jeremy

    Jeremy XenForo Moderator Staff Member

    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.
     
  11. Vincent Gabriel

    Vincent Gabriel Active Member

    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?
     
  12. Jeremy

    Jeremy XenForo Moderator Staff Member

    You are correct, but he isn't storing the description within the table (because I'm not storing the same field in the table).
     
  13. Vincent Gabriel

    Vincent Gabriel Active Member

    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.
     

Share This Page