Re: how to add category programmatic?

323 views
Skip to first unread message

b2z

unread,
Jul 26, 2012, 2:24:17 AM7/26/12
to joomla-de...@googlegroups.com
Hi Daniel!

Seems to me that is something wrong with the include path. Try to add:
[code]JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'tables');[/code]
just before:
[code]require_once  JPATH_ADMINISTRATOR.DS.'components'.DS.'com_categories'.DS.'models'.DS.'category.php';[/code]

Hope this helps!

Best regards,
Dmitry

среда, 25 июля 2012 г., 3:54:50 UTC+3 пользователь Daniel написал:
Hi, 

i have another blog which was developed by DNN.
now, i want to transfer it Joomla.

i need to add category and article programmatic, but i also got an error when i try to add category:

i have created the test component, in the component entry file:
Code:
defined('_JEXEC') or die;

// Include dependancies
jimport('joomla.application.component.controller');
//echo JPATH_ADMINISTRATOR;
require_once  JPATH_ADMINISTRATOR.DS.'components'.DS.'com_categories'.DS.'models'.DS.'category.php';

// Execute the task.
/* $controller   = JController::getInstance('Helloworld');
 $controller->execute(JRequest::getVar('task',''));
$controller->redirect(); */

//echo "Hello world";

//get categories
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id, name');
$query->from('#__import_categories');
$db->setQuery($query, 0, 20);
$rows = (array) $db->loadObjectList();

$cateModel = new CategoriesModelCategory();

foreach($rows as $row){
    $data = array (
            'id' => 0,
            'parent_id' => '1',
            'extension' => 'com_content',
            'title' => $row->name,
            'alias' => '',
            'note' => '',
            'description' => '',
            'published' => '1',
            'access' => '1',
            'metadesc' => '',
            'metakey' => '',
            'created_user_id' => '0',
            'language' => '*',
            'rules' =>
            array (
                    'core.create' =>
                    array (
                    ),
                    'core.delete' =>
                    array (
                    ),
                    'core.edit' =>
                    array (
                    ),
                    'core.edit.state' =>
                    array (
                    ),
                    'core.edit.own' =>
                    array (
                    ),
            ),
            'params' =>
            array (
                    'category_layout' => '',
                    'image' => '',
            ),
            'metadata' =>
            array (
                    'author' => '',
                    'robots' => '',
            ),
    );
    
    $cateModel->save($data);
    
    echo $row->name . '<br />';
}


error is:
Quote:
PHP Fatal error: Call to a member function setLocation() on a non-object in D:\CANADA\joomla-learning.testing.our-projects.info\administrator\components\com_categories\models\category.php on line 364
PHP Stack trace:
PHP 1. {main}() D:\CANADA\joomla-learning.testing.our-projects.info\index.php:0
PHP 2. JSite->dispatch() D:\CANADA\joomla-learning.testing.our-projects.info\index.php:42
PHP 3. JComponentHelper::renderComponent() D:\CANADA\joomla-learning.testing.our-projects.info\includes\application.php:197
PHP 4. JComponentHelper::executeComponent() D:\CANADA\joomla-learning.testing.our-projects.info\libraries\joomla\application\component\helper.php:351
PHP 5. require_once() D:\CANADA\joomla-learning.testing.our-projects.info\libraries\joomla\application\component\helper.php:383
PHP 6. CategoriesModelCategory->save() D:\CANADA\joomla-learning.testing.our-projects.info\components\com_helloworld\helloworld.php:116


the error is in file: 
\administrator\components\com_categories\models\category.php
the error code is method "save": 
Code:
$table = $this->getTable();


the variable "$table" is false.

so , the problem is there: 
Code:
public function getTable($type = 'Category', $prefix = 'CategoriesTable', $config = array()) {
        $table = JTable::getInstance($type, $prefix, $config);
        //var_dump($table);
        return $table;
    }


in the JTable::getInstance method: 
the $tableClass is : CategoriesTableCategory

but loaded file is : \libraries\joomla\database\table\category.php, in this file the Class name is : JTableCategory, but not CategoriesTableCategory. 

the class "CategoriesTableCategory" is in file: /administrator/com_categories/tables/category.php.

can anyone help me where was wrong? 

any ideas will be appreciated. 

elin

unread,
Jul 26, 2012, 8:45:04 PM7/26/12
to joomla-de...@googlegroups.com
We're really discouraging use of DS over the long term so you may just want to use slashes.

Elin

b2z

unread,
Jul 27, 2012, 1:16:52 AM7/27/12
to joomla-de...@googlegroups.com
Oops, thank you elin. Forgot it... :)
Reply all
Reply to author
Forward
0 new messages