Here is the class definition

class MyTable extends JTable{

    public $parent_id = null;
    public $hits = 1;

    function __construct()
        parent::__construct('#__mytable', 'parent_id', JFactory::getDbo());

and here is the problematic code.

$table = new MyTable();
if(!$table->load($id)){ //If the record doe not exist
    $table->parent_id=id; //Setting the id for the new row
    echo "Should be storing new table row...";
    $table->store(true);//New record should be created here
    echo "Record existed existed : ";
    echo "$table->hits hits";

The problem is that $table->store returns true but does not create any records in the database.

If I add records manually, load() works fine. But it fails to INSERT a new row. getErrors() return NULL

2 Answers 2


I believe, you want to bind your data at database level with primary-foreign key, Right? But FYI, Joomla itself is not using their data that way. Tables of Joomla does not use foreign key bindings at database level, but instead Joomla handles it at application(code) level. If you want to go ahead with Joomla standard way, and also if you are developing component for public/commercial usage, you should remove foreign key dependency at database level and then use JTable to accomplish above task.

You will find relative discussion over here: https://joomla.stackexchange.com/a/4541/3869


Apparently store() inserts a row only if the primary key is not set (e.g parent_id is NULL). This doesn't work in my case because the parent_id is a foreign key and I must specify it.

The only solution I came up with is adding the row using a different method (e.g. running a query manually)

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.