[Vtigercrm-developers] Cant restore records from custom modules
Rubén A. Estrada Orozco
rulotec1 at gmail.com
Thu Oct 10 01:40:19 GMT 2019
Thanks for your help Lajeesh, I could finally find spot the bug and fix it
without changing vtiger's code.
In vtiger's modules you will find a class with the name of the module. For
example, there is the class Accounts.php
Accounts has a constructor that looks like this:
[image: image.png]
Accounts also extends CRMEntity.php
So, the $this->db you see in the restore() function inside CRMEntity.php
gets intitialized in the Accounts constructor using
PearDatabase::getInstance()
In the case of custom modules, you usually extend Vtiger_CRMEntity and you
don't create a constructor. You inherit the one provided by
Vtiger_CRMEntity which is this one:
[image: image.png]
There you can see that you are creating an entirely new database object
with "new PearDatabase()" instead of getting it via
PearDatabase::getInstance(), and that is the problem.
To fix it, just create a constructor like this one in your module's main
class like this:
[image: image.png]
I think Vtiger should fix that by making PearDatabase constructor private
(implement the singleton pattern correctly) and replacing all the new
PearDatabase() occurrences.
Saludos
Rubén
On Wed, Oct 9, 2019 at 2:13 AM lajeesh k <lajeeshk at gmail.com> wrote:
> Hi Roben,
>
> Its a vtiger bug.
>
> The problem is the restore process is a transaction.
> you can see transaction starts with $this-db->startTransaction()
>
> until the transaction completes other mysql process cannot see the db
> changes
>
> the vtiger_record_model::getInstance method in
> handleEmailLookupRestoreEvent function uses $adb instance
>
> $adb instance cannot see that db changes
> so vtiger_record_model::getInstance returns the error record deleted
>
> either $this->db or $adb should use for all queries inside a transaction
>
>
>
>
> Regards,
> Lajeesh
> Freelance developer / entrepreneur
>
>
>
> On Tue, Oct 8, 2019 at 11:07 PM Rubén A. Estrada Orozco <
> rulotec1 at gmail.com> wrote:
>
>> Thanks Lajeesh,
>>
>> Do you think that $this->db is not initialized or something when called
>> in the case of custom modules? If thats the case, then that's a vtiger bug,
>> I guess.
>>
>> I would like to find a solution without changing vtiger's code. Because I
>> cannot control that in many cases.
>>
>> Saludos
>>
>> Rubén
>>
>>
>> On Tue, Oct 8, 2019 at 9:11 AM lajeesh k <lajeeshk at gmail.com> wrote:
>>
>>> Hi
>>>
>>> In restore function change $this->db->pquery to $adb->pquery
>>> I had the same issue and fixed this way
>>>
>>>
>>> Regards,
>>> Lajeesh
>>> Freelance developer / entrepreneur
>>>
>>>
>>>
>>> On Fri, Oct 4, 2019 at 7:29 AM Rubén A. Estrada Orozco <
>>> rulotec1 at gmail.com> wrote:
>>>
>>>> Sending this message a second time, because it seems the first time
>>>> failed:
>>>>
>>>> Hi everyone
>>>>
>>>> Has anybody noticed that restoring records from custom modules fails?
>>>> It fails with modules I created as well as with modules from other 3rd
>>>> parties. I spent some hours trying to fix it without success :(
>>>>
>>>> I get the following error:
>>>>
>>>> [image: image.png]
>>>>
>>>> If you try to restore it a second time, it takes a while before the
>>>> http response is back:
>>>> [image: image.png]
>>>>
>>>> The error is the following:
>>>>
>>>> Error restoring records : Query Failed:UPDATE vtiger_crmentity SET
>>>> deleted=0,modifiedtime=?,modifiedby=? WHERE crmid = ?::ADODB error Error
>>>> restoring records : Query Failed:UPDATE vtiger_crmentity SET
>>>> deleted=0,modifiedtime=?,modifiedby=? WHERE crmid = ?::->Lock wait timeout
>>>> exceeded; try restarting transaction
>>>>
>>>> Thats because the first time the the restore function at CRMEntity.php
>>>> executes, it doesn't complete (an exception is thrown when triggering the
>>>> afterrestore event):
>>>>
>>>> [image: image.png]
>>>>
>>>> So the second time you try to restore the record, the vtiger_crmentity
>>>> table is locked because of the startTransaction() call at line 1388 (I
>>>> think) and that's why it takes a while.
>>>>
>>>> It is the following handler that throws the exception the first time
>>>> you try to restore the record:
>>>> modules/Vtiger/handlers/EmailLookupHandler.php because it tries to
>>>> instantiate the record that's being restored at line 84:
>>>>
>>>> [image: image.png]
>>>>
>>>> The strange thing is that it doesn't fail in the case of Vtiger native
>>>> modules but only with custom modules, which is pretty strange.
>>>>
>>>> Disabling the above event results in successfully being able to restore
>>>> records.
>>>>
>>>> Any help will be greatly appreciated.
>>>>
>>>> Saludos
>>>>
>>>> Rubén
>>>>
>>>> Saludos
>>>>
>>>> Rubén
>>>> _______________________________________________
>>>> http://www.vtiger.com/
>>>
>>> _______________________________________________
>>> http://www.vtiger.com/
>>
>> _______________________________________________
>> http://www.vtiger.com/
>
> _______________________________________________
> http://www.vtiger.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 25315 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 60670 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0008.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 144050 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0009.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 99490 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0010.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 20205 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0011.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 21016 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0012.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 21860 bytes
Desc: not available
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20191009/6cd5bdb4/attachment-0013.png>
More information about the vtigercrm-developers
mailing list