1

I want to store a category record from controller by using JTable. My working code is-

$table = JTable::getInstance('Category');
$data = array('id' => 0, 'parent_id' = 1, 'path' => 'demo', 'level' => 1, 'title' => 'demo');

if ($table->bind($data)) {
    $table->store(true);
}

This code section store the record into #__categories table but problem is it stores parent_id = 0 and level = 0 always. How can I solve this problem?

1

I get the solution by the help of @mickmackusa and using this forum link https://forum.joomla.org/viewtopic.php?t=592653.

My final code is now

$table = JTable::getInstance('Category');
$data = array('id' => 0, 'parent_id' = 1, 'path' => 'demo', 'level' => 1, 'title' => 'demo');

if ($table->bind($data)) {
    if ($table->store(true)) {
        $table->moveByReference(0, 'last-child', $table->id); // this method generates the original parent_id and level for me.
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.