Multi type Form field Problem

edited October 2013 in Modules
I am trying to make simple user group module. I want to assign more then one user for a single group and also want to assign more then one groups for a single user. but groups to users data is not storing in database. All Files given Bellow
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
CREATE TABLE IF NOT EXISTS `fuel_users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`language` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'english',
`reset_key` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`super_admin` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'no',
`active` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

INSERT INTO `fuel_users` (`id`, `user_name`, `password`, `email`, `first_name`, `last_name`, `language`, `reset_key`, `super_admin`, `active`) VALUES
(1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', '', 'Admin', '', 'english', '', 'yes', 'yes'),
(2, 'aslam', 'e10adc3949ba59abbe56e057f20f883e', 'aslampatwary@gmail.com', 'Md. Aslam', 'Patwary', 'english', '', 'no', 'yes'),
(3, 'administrator', '123456', 'administrator@admin.com', 'Administrator', 'person', 'english', '', 'no', 'yes'),
..................................................................................
CREATE TABLE IF NOT EXISTS `ufcm_groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`active` enum('yes','no') NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

INSERT INTO `ufcm_groups` (`id`, `title`, `active`) VALUES
(1, 'php', 'yes'),
(2, 'ASP', 'yes'),
(3, 'Science & technology', 'yes');
..................................................................................
CREATE TABLE IF NOT EXISTS `ufcm_groups_to_users` (
`group_id` int(10) NOT NULL,
`user_id` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
.................................................................................
ufcm_users_model.php
----------------------------------
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once(FUEL_PATH.'models/base_module_model.php');
require_once(MODULES_PATH.'/ufcm/config/ufcm_constants.php');

class Ufcm_users_model extends Base_module_model {

function __construct()
{
parent::__construct('fuel_users', UFCM_FOLDER); // table name
}

function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc')
{
$data = parent::list_items($limit, $offset, $col, $order);
return $data;
}
}

class Ufcm_user_model extends Base_module_record{

}

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

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

class Ufcm_groups_model extends Base_module_model {

function __construct()
{
parent::__construct('ufcm_groups', UFCM_FOLDER); // table name
}

function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc')
{
$data = parent::list_items($limit, $offset, $col, $order);
return $data;
}

function form_fields($values = array()){

$fields = parent::form_fields();
$CI =& get_instance();
$CI->load->module_model(UFCM_FOLDER, 'ufcm_users_model');
$CI->load->module_model(UFCM_FOLDER, 'ufcm_groups_model');
$CI->load->module_model(UFCM_FOLDER, 'ufcm_groups_to_users_model');
$ufcm_config = $CI->config->item('ufcm');

$user_options = $CI->ufcm_users_model->options_list('id', 'user_name', array('active' => 'yes'), 'user_name');

$user_values = (!empty($values['id'])) ? array_keys($CI->ufcm_groups_to_users_model->find_all_array_assoc('user_id', array('group_id' => $values['id'], 'fuel_users'.'.active' => 'yes'))) : array();

print_r($user_values);

$fields['users'] = array('label' => 'Users', 'type' => 'array', 'options' => $user_options, 'class' => 'add_edit ufcm/users combo', 'value' => $user_values, 'mode' => 'multi');

return $fields;
}
}

class Ufcm_group_model extends Base_module_record{

}

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

class Ufcm_groups_to_users_model extends Base_module_model {

public $required = array();
public $record_class = 'Ufcm_group_to_user';
public $key_field = array('group_id', 'user_id');
public $_tables = array();

function __construct(){
$CI =& get_instance();
$CI->config->module_load(UFCM_FOLDER, UFCM_FOLDER);
parent::__construct('ufcm_groups_to_users',UFCM_FOLDER);
}
}

class Ufcm_group_to_user_model extends Base_module_model {

}

======================AND OTHERS CONFIG FILE [NO LIBRARY FILE CREATED]=================


So please View my All Files and Sql Statment Provide a Solution. I am a Student. IT is Important for my graduation program.

Comments

  • edited October 2013
    NOw It is showing an eRROR when I try to create or edit Group
    ------------------------------------
    A Database Error Occurred

    Error Number: 1054

    Unknown column 'fuel_users.active' in 'where clause'

    SELECT `ufcm_groups_to_users`.* FROM (`ufcm_groups_to_users`) WHERE `ufcm_groups_to_users`.`group_id` = '4' AND `fuel_users`.`active` = 'yes'

    Filename: D:\xampp\htdocs\ufcm\fuel\codeigniter\database\DB_driver.php

    Line Number: 330
  • edited October 2013
    .
  • edited 2:52AM
    In the fuel/modules/fuel/config/fuel.php file, there is an $config['tables'] array that maps key values to table names. You can overwrite those values in your MY_fuel.php file. It also may be helpful to debug the base_module_model::__construct method which assembles an array of tables. Line 165 it has the following code that creates that list of tables:
    self::$tables = array_merge(self::$tables, $config_tables, $module_tables, $fuel_tables);
  • Dear Admin
    ------------config/ufcm.php-------------------
    -----------------------------------
    <?php
    $config['nav']['ufcm'] = array(
    'ufcm/users' => 'User',
    'ufcm/groups' =>'Groups'
    );


    // tables for UFCM
    $config['tables']['ufcm_users'] = 'fuel_users';
    $config['tables']['ufcm_groups_to_users'] = 'ufcm_groups_to_users';
    $config['tables']['ufcm_groups'] = 'ufcm_groups';
    --------------------------------------------
    ------------config/ufcm_constant.php-------------------
    -----------------------------------
    <?php
    define('UFCM_VERSION', '1.0');
    define('UFCM_FOLDER', 'ufcm');
    define('UFCM_PATH', MODULES_PATH.UFCM_FOLDER.'/');
    --------------------------------------------
    ------------config/ufcm_fuel_module.php-------------------
    -----------------------------------
    <?php
    $config['modules']['ufcm_users'] = array(
    'module_name' => 'Users',
    'module_uri' => 'ufcm/users',
    'model_name' => 'ufcm_users_model',
    'model_location' => 'ufcm',
    'table_headers' => array(
    'id',
    'first_name',
    'user_name',
    'email',
    'active',
    ),

    'display_field' => 'id',
    'preview_path' => 'ufcm/users/{id}',
    'permission' => 'ufcm/users',
    'instructions' => lang('module_instructions_default', 'ufcm users'),
    'archivable' => TRUE,
    'configuration' => array('ufcm' => 'ufcm'),
    'nav_selected' => 'ufcm/users',
    // 'language' => array('ufcm' => 'ufcm')
    );

    $config['modules']['ufcm_groups'] = array(
    'module_name' => 'Groups',
    'module_uri' => 'ufcm/groups',
    'model_name' => 'ufcm_groups_model',
    'model_location' => 'ufcm',
    'table_headers' => array(
    'id',
    'title',
    'active',
    ),

    'display_field' => 'id',
    'preview_path' => 'ufcm/groups/{id}',
    'permission' => 'ufcm/groups',
    'instructions' => lang('module_instructions_default', 'ufcm groups'),
    'archivable' => TRUE,
    'configuration' => array('ufcm' => 'ufcm'),
    'nav_selected' => 'ufcm/groups',
    // 'language' => array('ufcm' => 'ufcm')
    );

    --------------------------------------------
    ------------config/ufcm_fuel_module.php-------------------
    -----------------------------------
    <?php
    $ufcm_controllers = array('users', 'groups');

    foreach($ufcm_controllers as $c)
    {
    $route[FUEL_ROUTE.'ufcm/'.$c] = FUEL_FOLDER.'/module';
    $route[FUEL_ROUTE.'ufcm/'.$c.'/(.*)'] = FUEL_FOLDER.'/module/$1';
    }

    $route[FUEL_ROUTE.'ufcm/settings'] = UFCM_FOLDER.'/settings';

    -------------------------------------------------------------------
    Above is my Config files. Please I identify the problem. I did not understand your recent comment.
  • edited October 2013
    Try changing your your constructor to pass "ufcm_users":
    function __construct() { parent::__construct('ufcm_users', UFCM_FOLDER); // table name }

    Also, this following line you are using the fuel_users.active in the where clause and by default that column doesn't exist for that model since it is a model for the ufcm_groups_to_users table:
    $user_values = (!empty($values['id'])) ? array_keys($CI->ufcm_groups_to_users_model->find_all_array_assoc('user_id', array('group_id' => $values['id'], 'fuel_users'.'.active' => 'yes'))) : array();

    To include the fuel_users table fields you can add a _common_query() method to your model to include that join and select on all model "find" methods:
    function _common_query() { parent::_common_query(); $this->db->join('fuel_users', 'fuel_users.id = ufcm_groups_to_users.user_id', 'left'); $this->db->select('ufcm_groups_to_users.*, fuel_users.active'); }
  • Hello Admin Thanks for help. I solved the problem. I have not used on_after_save() hook function. Now it works what i want.
  • edited 2:52AM
    Great... glad you got it working.
Sign In or Register to comment.