Due duplicate entry in the database, field 'created_by' in the $data is missing?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Wed Mar 08, 2006 7:20 am

Due duplicate entry in the database, field 'created_by' in the $data is missing?

Post by nkamp » Sat Feb 20, 2021 8:42 pm

Hello ,

I have already made a viewtopic.php?f=727&t=983181 a while ago about the 'publishing data' is not saved by default. I have this working and the fields created_by, created_time, modified_by, and modified_time are saved when a new record is saved and when update a record.
I have added two lines here for which is doing the job:

Code: Select all

echo JLayoutHelper::render('joomla.edit.global', $this);
echo JLayoutHelper::render('joomla.edit.publishingdata', $this);
I have also a requirement: when an record is created, than I will pre-fill the 'created_by' field.
So in my loadformdata() method I have added:

Code: Select all

$user = JFactory::getUser();
echo "Data: ".var_dump($data)." user: ".$user->id;      //Temporarily
$data->set('created_by', $user->get('id'), 'int');
When a new record is created and save it, or when update an existing record than it works all perfectly, without any issue. It is all saved in the database!

It goes wrong when I try to save a new record or try to update a record with a value which already exist in the dababase, because I have defined an unique index for the column 'canr' in the database

I know now more or less why it is going wrong, but not how to solve it!

Here my var_dump($data) of the data array at the time of loadformData () when everything went ok:

Code: Select all

object(Joomla\CMS\Object\CMSObject)[465]
  protected '_errors' => 
    array (size=0)
      empty
  public 'id' => string '61' (length=2)
  public 'created_by' => string '150' (length=3)
  public 'created_time' => string '2021-02-14 17:29:46' (length=19)
  public 'modified_by' => string '150' (length=3)
  public 'modified_time' => string '2021-02-15 21:19:11' (length=19)
  public 'canr' => string '88' (length=2)
  public 'catc' => string 'NN' (length=2)
  public 'catn' => string 'Test' (length=4)
  public 'info' => string 's' (length=1)
  public 'cmin' => string '0' (length=1)
  public 'cmax' => string '0' (length=1)
  public 'checked_out' => string '150' (length=3)
  public 'checked_out_time' => string '2021-02-15 21:19:11' (length=19)
  public 'state' => string '1' (length=1)
  public 'ordering' => string '0' (length=1)
As you can see the field 'created_by' is in the $data array. This is when I add a new record or update the record.

Now if I change the value of the field canr to 5 instead off 88, than I get the error: "Saving fails due to the following error: Duplicate entry '5' for key 'idx_canr' " The error is what I expect, because I have unique index on this field.

Here is the var_dump($data) again at the time of loadFormData().
First of all it is not a object(Joomla\CMS\Object\CMSObject)[465] anymore!
The field 'created_by' is missing in the $data array!!!???

Code: Select all

array (size=100)
  'id' => int 61
  'canr' => string '5' (length=1)
  'catc' => string 'NN' (length=2)
  'catn' => string 'Test' (length=4)
  'cmin' => int 0
  'cmax' => string '0' (length=1)
  'info' => string 's' (length=1)
  'state' => int 1
  'created_time' => string '2021-02-14 17:29:46' (length=19)
  'modified_by' => string '150' (length=3)
  'modified_time' => string '2021-02-15 21:19:11' (length=19)
  'tags' => null
Does anybody know:
  • Why is there difference if the saving action it is successful it is CMSObject, and if it fails due a database validation it is an array
  • When the saving action fails, why is the behavior different, I mean the created_by field is missing, so than handling must be different.
  • Has anybody an idea, how to solve this?

Locked

Return to “Joomla! 3.x Coding”