Good afternoon! Help please. I have a (add) function in cakephp web application to add a project, that is, another line is added to the database in the table, the problem is that when filling in the form for a new project, empty fields are entered as an empty value in the database, although and the condition was set:

if(empty($this->request->data['Offer_Faktura_LBU']['kommentar'])) { $this->request->data['Offer_Faktura_LBU']['kommentar'] = null; } $this->Offer_Faktura_LBU->save($this->request->data); 

The paradox is that when I added a new project, and I want to change it (the edit function), then when I save this time, the empty fields change to null. Where the error lies is not clear, maybe in the CAKE library in the save function?

  • one
    It is possible that save has a function that converts logical zeros to empty strings. Some other languages ​​do not always display logical values. Or maybe the generating function save (and why not add?) Ignores empty fields, and you have disabled warnings (for example, the array key is missing) in this case, nothing will be written anywhere. If you really want to propose to write: foreach ($ this-> request-> data ['Offer_Faktura_LBU'] as $ key => $ val) {if (empty ($ val)) $ this-> request-> data ['Offer_Faktura_LBU' ] [$ key] = 'NULL'; } (Actually, escaping the value as a string and not a logical variable.) - Rijen
  • Thanks for the answer, above I described the add function. The fact is that there is also an edit function that changes the entered values, and when this function is called, empty fields (empty in add) are converted to NULL. That is, the SAVE function still outputs logical zeros, but for some reason only in EDIT and not in ADD. Maybe here the SAVE function reads NULL only when the table field is rewritten ? Although both functions are almost identical - EvgeniaSamoylova

2 answers 2

Try not to specify:

 if(empty($this->request->data['Offer_Faktura_LBU']['kommentar'])) { $this->request->data['Offer_Faktura_LBU']['kommentar'] = null; } $this->Offer_Faktura_LBU->save($this->request->data); 

And just do it (the logic will remain the same):

  $this->Offer_Faktura_LBU->save($this->request->data); 

    I, for example, use this plugin for cakephp 2.x
    CakePHP-NullableBehavior
    I also looked for a solution to this problem and came across this solution, now instead of empty lines, I still have NULL