Need Help for Unable to found model !

edited December 2010 in News & Announcements
Hello I have create one module it's name is "ads" & in that I have created 2 models it's name is given below
1) ads_categories.php
2) ads_subcategories.php

I have created tables for both in INNODB (and also set relationship between both table)

I can able to do list/add/edit/delete for Categories. But for SubCategories it's giving me below error

======================================================
URL : www.test.com/fuel/ads/subcategories/create

An Error Was Encountered
Unable to locate the model you have specified: ads_categories_model
======================================================

Here is my Directory structure,
=======================
fuel
|modules
|ads
|config
|controllers
|models
- ads_categories.php
- ads_subcategories.php
=======================

Here is my Code for both models
=========================

============ ads_categories.php ===================

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

class Ads_categories_model extends Base_module_model {

public $required = array('title');

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

============ ads_subcategories.php ===================

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

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

class Ads_subcategories_model extends Base_module_model {

public $required = array('title','id_ads_categories');
public $foreign_keys = array('id_ads_categories' => 'ads_categories_model');

function __construct()
{
parent::__construct('ads_subcategories');
}
}
=====================================================


Here is my ads_fuel_modules.php file
============================

<?php
// included in the main config/MY_fuel_modules.php

$config['modules']['ads_categories'] = array(
'module_name' => 'Categories',
'module_uri' => 'ads/categories',
'model_name' => 'ads_categories_model',
'model_location' => 'ads',
'display_field' => 'title',
'preview_path' => 'ads/categories/{id}',
'permission' => 'ads/categories',
'instructions' => 'Manage Ad Categories',
'archivable' => TRUE,
'configuration' => array('ads' => 'ads'),
'nav_selected' => 'ads/categories',
'sanitize_input' => array('template','php')
);

$config['modules']['ads_subcategories'] = array(
'module_name' => 'Sub Categories',
'module_uri' => 'ads/subcategories',
'model_name' => 'ads_subcategories_model',
'model_location' => 'ads',
'display_field' => 'title',
'preview_path' => 'ads/subcategories/{id}',
'permission' => 'ads/subcategories',
'instructions' => 'Manage Ad Sub Categories',
'archivable' => TRUE,
'configuration' => array('ads' => 'ads'),
'nav_selected' => 'ads/subcategories',
'sanitize_input' => array('template','php')
);

Can you please tell me What's wrong here? why it didn't found the model of "ads_categories_model" ? or is there anything I am missing?

Thanks in advance.

Comments

  • edited 6:45AM
    I think the issue is because your file name needs to be ad_categories_model.php not ad_categories.php
  • edited 6:45AM
    ah sorry it's my typo mistake, my file name is already like below, Even though it is same problem.

    ad_categories_model.php
    ad_subcategories_model.php
  • edited 6:45AM
    Should it be ads_subcategories_model.php (with an s)?
  • edited 6:45AM
    Ah, again typo mistake :) Yes, it is including "s", Now I have just copied the file name,

    ads_categories_model.php
    ads_subcategories_model.php
  • edited 6:45AM
    I'm not sure. Can you send me the module code and any SQL to power the models (click on my picture to get to my email address)? Also, which branch are you using 0.9 (master) or the 0.91 branch (CI 2.0)?
  • edited December 2010
    If your model exists in a module directory (which in your case is one called 'ads'), the foreign_keys property value for 'id_ads_categories' needs to be an array with the key being the module folder name like so.

    public $foreign_keys = array('id_ads_categories' => array('ads' => 'ads_categories_model'));
  • edited 6:45AM
    Got it now.. I was thaught that it will detect automatic.. :)
    Thanks a lot for helping..

    one minor thing, by default in the form Label is coming like "Ads_categorie*"
    I want to put custom lable like "Choose Category" then what change is required?
  • edited 6:45AM
    Do something like this:
    $field['my_field']['label'] = 'Choose Category';
  • edited 6:45AM
    thanks a lot, for giving such time.
    I am really thankful of you :)
  • edited 6:45AM
    Sure. Hope your project goes well and that you'll be able to share what you've learned with the community.
  • edited 6:45AM
    definitely, I will share with all. Recently I am preparing to many modules for one classified system of Car (Auto System), and I hope I will do everything perfectly in FUEL... :)
  • edited 6:45AM
    Hi there,

    I have add 3rd level in same module..

    it's like categories >> sub-categories >> products

    So in products form I need 2 Select Option

    1) Choose Category (* Required)
    2) Choose Sub Category (Optional)


    So in ads_products_model.php I have pasted below code for "Foriegn Key"

    in below code, it gives me correct option value in select box for "Category"

    But in Sub Category select option it is showing me "Ids" (Primary key of categories)
    Instead of Sub category title.

    public $foreign_keys = array('id_ads_categories' => array('ads' => 'ads_categories_model'), 'id_ads_subcategories' => array('ads' => 'ads_subcategories_model'));

    My Product table Structure..
    =====================
    id --> int(10)
    id_ads_categories --> smallint(6) [Foriegn Key]
    id_ads_subcategories --> int(10) [Foriegn Key]
    product_name --> varchar(255)
    price --> int(10)
    description --> text
    published enum --> ('yes', 'no')
    create_date --> datetime
    modify_date --> datetime

    NOTE : in sub cateogry option list it is showing list of "id_ads_categories" instead of "subcategory_title"
  • edited 6:45AM
    2nd Question,
    Is it possible to make dependent field? like when I choose some category option and then it should load only related subcategory in Subcategory option box.
  • edited 6:45AM
    You'll have to create custom javascript to do that. The navigation module does that to load the proper parents for the given navigation group. There is a corresponding javascript file in the fuel/modules/fuel/assets/js/fuel/controller/NavigationController.js

    You'll need to specify the javascript jqx Controller file or the simple javascript file in your modules ads/config/ads_fuel_modules.php
  • edited 6:45AM
    ok for 2nd question I got it, I will create the JS.

    but can you please tell me about my 1st question, In select box option I am getting "id_ads_categories" in list, instead of "subcategories_title", Is there anything wrong mentioned in above foreign key code?
  • edited 6:45AM
    Could you email me the ads module again (or a place to download the code) including any SQL.
  • edited 6:45AM
    Here is the DB query for ads_products

    CREATE TABLE `ads_products` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `id_ads_categories` smallint(5) unsigned NOT NULL default '0',
    `id_ads_subcategories` int(10) unsigned NOT NULL default '0',
    `product_name` varchar(255) NOT NULL,
    `price` int(10) unsigned NOT NULL default '0',
    `image` varchar(50) default NULL,
    `description` text,
    `published` enum('yes','no') NOT NULL default 'yes',
    `create_date` datetime default NULL,
    `modify_date` datetime default NULL,
    PRIMARY KEY (`id`),
    KEY `id_ads_categories` (`id_ads_categories`),
    KEY `id_ads_subcategories` (`id_ads_subcategories`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

    --
    -- Constraints for dumped tables
    --

    --
    -- Constraints for table `ads_products`
    --
    ALTER TABLE `ads_products`
    ADD CONSTRAINT `ads_products_ibfk_2` FOREIGN KEY (`id_ads_subcategories`) REFERENCES `ads_subcategories` (`id`) ON DELETE CASCADE,
    ADD CONSTRAINT `ads_products_ibfk_1` FOREIGN KEY (`id_ads_categories`) REFERENCES `ads_categories` (`id`) ON DELETE CASCADE;

    ============================
    Code of ads_products_model.php
    ============================

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class Ads_products_model extends Base_module_model { public $required = array('id_ads_categories','product_name','price'); public $foreign_keys = array('id_ads_categories' => array('ads' => 'ads_categories_model'), 'id_ads_subcategories' => array('ads' => 'ads_subcategories_model')); function __construct() { parent::__construct('ads_products'); } }


    Is there anything else you require?
  • edited 6:45AM
    Mail sent with attachment
  • edited 6:45AM
    I see the issue. The option_list() method is used to create the array used for that form field (around line 1701 on MY_Model). By default, that method will use the second column as the name field if no $val property is passed to the method which is what you are seeing. There are two ways to fix. The first would be to move the title column to be right after the id column. The second way would be to overwrite the option_list method to always use the title column instead of the id_ads_categories column by default. To do that, see below:
    
    function options_list($key = NULL, $val = NULL, $where = array(), $order = TRUE)
    {
    	if (empty($val)) $val = 'title';
    	$options = parent::options_list($key, $val, $where, $order);
    	return $options;
    }
    
  • edited 6:45AM
    Hi,

    Got it now, This kind of points are important to know, I think there should be some document for Rules of Select/Option or Radio etc.. It would be good to learn from it.

    Anyway thank you very much for helping... :)
Sign In or Register to comment.