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
If you manually add data directly into the table, can you see it using your module?
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.
function on_after_save($values) { print_r($this->get_errors()); exit(); }
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.
$fields = parent::form_fields($values, $related);
However the fields which were previously in the field sets are no longer in there.
$fields['id'] = array('type' => 'hidden', 'value' => (!empty($values['id']) ? $values['id'] : ''));
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.