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?

3
  • Do you need to bind to the instance? Can you not just add properties to the object like this? forum.joomla.org/viewtopic.php?t=592653 can you var_dump($table)? This looks perhaps related forum.joomla.org/viewtopic.php?t=554960 or maybe this: forum.joomla.org/viewtopic.php?t=247046 Commented Sep 4, 2019 at 8:14
  • @mickmackusa one of your link solves my problem. $table->moveByReference(0, 'last-child', $table->id); is the solution. Commented Sep 4, 2019 at 10:55
  • I will invite you to post a complete and educational solution as an answer (then eventually accept your own answer). I am happy that you discovered the resolution so quickly. Please share your findings generously. Commented Sep 4, 2019 at 11:31

1 Answer 1

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 and acknowledge you have read our privacy policy.

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