Custom Model refusing to save into db

rl2rl2
edited October 2017 in Share
Hi,

I have a custom model which appears in the CMS and is displaying within fieldsets as required however when I click save the data is not being written into the database. Something does not tally up?

SQL:

CREATE DATABASE /*!32312 IF NOT EXISTS*/`fuel` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `fuel`;

/*Table structure for table `kbarticles` */

DROP TABLE IF EXISTS `kbarticles`;

CREATE TABLE `kbarticles` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`subtitle` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`section_content1` text COLLATE utf8_unicode_ci NOT NULL,
`section_image1` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`section_content2` text COLLATE utf8_unicode_ci NOT NULL,
`section_image2` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`section_content3` text COLLATE utf8_unicode_ci NOT NULL,
`section_image3` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`section_content4` text COLLATE utf8_unicode_ci NOT NULL,
`section_image4` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`published` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Model:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once(FUEL_PATH.'models/Base_module_model.php');

class Knowledge_Base_Articles_model extends Base_module_model {

function __construct()
{
parent::__construct('kbarticles');
}

function _common_query($display_unpublished_if_logged_in = FALSE)
{
parent::_common_query(); // to do active and published
$this->db->order_by('date desc');
}

function form_fields($values = array(), $related = array() ) {
$fields['published'] = array('type' => 'enum', 'mode' => 'radios', 'options' => array( 'yes' => 'yes', 'no' => 'no') );
$fields['title'] = array('type' => '', 'required' => TRUE);
$fields['subtitle'] = array('type' => '', 'required' => TRUE);
$fields['slug'] = array('type' => 'slug', 'linked_to' => 'title');
$fields['section1'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 1');
$fields['section_content1'] = array('type' => 'text', 'label' => 'Content');
$fields['section_image1'] = array('type' => 'asset', 'label' => 'Image');
$fields['section2'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 2');
$fields['section_content2'] = array('type' => 'text', 'label' => 'Content');
$fields['section_image2'] = array('type' => 'asset', 'label' => 'Image');
$fields['section3'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 3');
$fields['section_content3'] = array('type' => 'text', 'label' => 'Content');
$fields['section_image3'] = array('type' => 'asset', 'label' => 'Image');
$fields['section4'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 4');
$fields['section_content4'] = array('type' => 'text', 'label' => 'Content');
$fields['section_image4'] = array('type' => 'asset', 'label' => 'Image');

return $fields;
}

}

class Knowledge_Base_Article_model extends Data_record {

public function get_url()
{
return site_url('kbarticles/'.$this->slug);
}

}

Comments

  • rl2rl2
    edited 4:50AM
    When the page is reloaded it doesn't warn me about required fields being empty either.
  • Anything in the browser console log after saving? There should be some POST/GET traffic you can browse...

    If you manually add data directly into the table, can you see it using your module?
  • rl2rl2
    edited 4:50AM
    Nothing in console.

    I was able to add data directly into the table and it to appear in the list, I've since added a Date column into the table as Fuel was complaining I needed one.

    However, when I go to Save through the CMS it is still refusing to update the DB.
  • edited 4:50AM
    You can check in the on_after_save hook on the model if there are any errors using the get_errors() method:
    function on_after_save($values) { print_r($this->get_errors()); exit(); }
  • rl2rl2
    edited 4:50AM
    I tried inserting that table class hook above into Knowledge_Base_Articles_model, but the hook didn't seem to launch - tried several hooks - nothing being observed.
  • edited 4:50AM
    Is it getting to at least the on_before_validate hook?
  • rl2rl2
    edited 4:50AM
    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    require_once(FUEL_PATH.'models/Base_module_model.php');

    class Knowledge_Base_Articles_model extends Base_module_model {

    function __construct()
    {
    parent::__construct('kbarticles');
    }

    function _common_query($display_unpublished_if_logged_in = FALSE)
    {
    parent::_common_query(); // to do active and published
    $this->db->order_by('date desc');
    }

    function form_fields($values = array(), $related = array() ) {
    $fields['published'] = array('type' => 'enum', 'mode' => 'radios', 'options' => array( 'yes' => 'yes', 'no' => 'no') );
    $fields['title'] = array('type' => '', 'required' => TRUE);
    $fields['subtitle'] = array('type' => '', 'required' => TRUE);
    $fields['slug'] = array('type' => 'slug', 'linked_to' => 'title', 'description' => 'If left empty Title will be used');
    $fields['section1'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 1');
    $fields['section_content1'] = array('type' => 'text', 'label' => 'Content');
    $fields['section_image1'] = array('type' => 'asset', 'label' => 'Image');
    $fields['section2'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 2');
    $fields['section_content2'] = array('type' => 'text', 'label' => 'Content');
    $fields['section_image2'] = array('type' => 'asset', 'label' => 'Image');
    $fields['section3'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 3');
    $fields['section_content3'] = array('type' => 'text', 'label' => 'Content');
    $fields['section_image3'] = array('type' => 'asset', 'label' => 'Image');
    $fields['section4'] = array('type' => 'fieldset', 'class' => 'tab', 'label' => 'Section 4');
    $fields['section_content4'] = array('type' => 'text', 'label' => 'Content');
    $fields['section_image4'] = array('type' => 'asset', 'label' => 'Image');

    return $fields;
    }

    function on_before_validate($values)
    {
    echo "test";
    die();
    }

    }

    class Knowledge_Base_Article_model extends Data_record {

    public function get_url()
    {
    return site_url('kbarticles/'.$this->slug);
    }
    }

    -----

    Nope.
  • edited 4:50AM
    I think it's because you don't have a hidden field for the id. If you call the following at the top of your form_fields method it should take care of that:
    $fields = parent::form_fields($values, $related);
  • rl2rl2
    edited 4:50AM
    Okay that sort of works its saving the data. =) Thank you.

    However the fields which were previously in the field sets are no longer in there.
  • edited 4:50AM
    Calling parent gives you the default form and then you would need to manipulate those array variables. If you are starting fresh, you will need to create a hidden "id" field and use the 'value' => $values['id'] to set the value:
    $fields['id'] = array('type' => 'hidden', 'value' => (!empty($values['id']) ? $values['id'] : ''));
  • rl2rl2
    edited 4:50AM
    Great that works for me. Thank you.

    This is the second model I've had to create, with the previous one I just created the relevant columns in sql just marked the fields that were required in the model itself.

    This particular model I needed the fieldsets so I had to set out different from the start.

    Thanks again.
Sign In or Register to comment.