Although this should never appear in your application, my colleague showed me this funny code: $a = array(null=>1); print_r($a);
the result is
Array
(
[]=>1
)
If we do echo $a[null], the output is 1. However, if we echo $a[''], we get the same output as well.
If we try xdebug_debug_zval('a'), we can find out that internally, PHP use '', not null as the array index.
Thursday, September 16, 2010
Monday, August 30, 2010
SugarCRM design flaw (I believe it is)
i have a website. When a user submits a form, it will create a Lead in SugarCRM by calling its REST API.
It all works good until I create a logic hook for the Lead module. This is a after_save logic hook, which means it should only be executed after a Lead is saved.
The logic hook function is very simple, with only one line:
function RunMe(SugarBean $bean, $event, $arguments)
{
$bean->retrieve($bean->id);
}
Now, the problem comes out. The Leads created through REST service lost their Email information!
I try to create a Lead within SugarCRM, the Email information is saved properly.
This brings up two questions:
1. it seems $bean->retrieve($bean->id) cannot retrieve its email properly.
2. how can email get lost due to this after_save logic hook. Shouldn't the email get saved before this logic hook is fired? Further more, it only happens when we create Leads through REST. Leads created within SugarCRM Lead Module do not have this problem.
Solution:
I change my logic hook code like below
function RunMe(SugarBean $bean, $event, $arguments)
{
$emailAddress = clone $bean->emailAddress;
$bean->retrieve($bean->id);
$bean->emailAddress = $emailAddress;
}
Now, problem solved. Here is a thread on SugarCRM forum with similar problem. http://panther.sugarcrm.com/forums/showthread.php?t=44872
"The problem was that after_save, the bean discards all the new field info. So we needed to use $bean->retrieve($bean->id); to get it back. Then, it didn't retrieve the email addresses properly, so we needed to pull the email address out before we ran the retrieve".
In SugarCRM, Leads extends Person. When we save a Lead, SugarCRM will call Person::save() method. Within the Person's save method, it will call SugarBean's save() method first, and only after that, it calls $this->emailAddress->save() to save the email. But SugarBean's Save() method will fire after_save logic hook, and the logic hook code $bean->retrieve($bean->id) cannot retrieve its emailAddress properly, so, that explains why email is lost.
But the question becomes, why Leads created in Sugarcrm don't have this problem.
It all works good until I create a logic hook for the Lead module. This is a after_save logic hook, which means it should only be executed after a Lead is saved.
The logic hook function is very simple, with only one line:
function RunMe(SugarBean $bean, $event, $arguments)
{
$bean->retrieve($bean->id);
}
Now, the problem comes out. The Leads created through REST service lost their Email information!
I try to create a Lead within SugarCRM, the Email information is saved properly.
This brings up two questions:
1. it seems $bean->retrieve($bean->id) cannot retrieve its email properly.
2. how can email get lost due to this after_save logic hook. Shouldn't the email get saved before this logic hook is fired? Further more, it only happens when we create Leads through REST. Leads created within SugarCRM Lead Module do not have this problem.
Solution:
I change my logic hook code like below
function RunMe(SugarBean $bean, $event, $arguments)
{
$emailAddress = clone $bean->emailAddress;
$bean->retrieve($bean->id);
$bean->emailAddress = $emailAddress;
}
Now, problem solved. Here is a thread on SugarCRM forum with similar problem. http://panther.sugarcrm.com/forums/showthread.php?t=44872
"The problem was that after_save, the bean discards all the new field info. So we needed to use $bean->retrieve($bean->id); to get it back. Then, it didn't retrieve the email addresses properly, so we needed to pull the email address out before we ran the retrieve".
In SugarCRM, Leads extends Person. When we save a Lead, SugarCRM will call Person::save() method. Within the Person's save method, it will call SugarBean's save() method first, and only after that, it calls $this->emailAddress->save() to save the email. But SugarBean's Save() method will fire after_save logic hook, and the logic hook code $bean->retrieve($bean->id) cannot retrieve its emailAddress properly, so, that explains why email is lost.
But the question becomes, why Leads created in Sugarcrm don't have this problem.
Friday, July 23, 2010
sugarcrm logic hook problem
I created an after_save logic hook for Lead. My SugarCRM is version 5.5.2 and the function is quite simple:
function runMeAfterSave(SugarBean $bean, $event, $arguments)
{
if ($bean->first_name == 'someone') {
//do some tasks
}
}
The function works very well on my localhost. When i put it on another server, however, it doesn't work at all. So, i var_dump($bean->first_name), and it turns out to be NULL all the time, no matter what value i enter into first name field. I var_dump some other fields and get NULL as well. The server SugarCRM version is 5.5.3. I can't find any reason why it can't work.
Finally, i try var_dump($bean->id). This time i'm lucky! I can get its ID! So i add one line in my code: $bean->retrieve($bean->id) and everything works fine now.
Later I found a thread on SugarCRM forum regarding this issue http://panther.sugarcrm.com/forums/showthread.php?t=44872. It says "The problem was that after_save, the bean discards all the new field info. So we needed to use $bean->retrieve($bean->id); to get it back"
function runMeAfterSave(SugarBean $bean, $event, $arguments)
{
if ($bean->first_name == 'someone') {
//do some tasks
}
}
The function works very well on my localhost. When i put it on another server, however, it doesn't work at all. So, i var_dump($bean->first_name), and it turns out to be NULL all the time, no matter what value i enter into first name field. I var_dump some other fields and get NULL as well. The server SugarCRM version is 5.5.3. I can't find any reason why it can't work.
Finally, i try var_dump($bean->id). This time i'm lucky! I can get its ID! So i add one line in my code: $bean->retrieve($bean->id) and everything works fine now.
Later I found a thread on SugarCRM forum regarding this issue http://panther.sugarcrm.com/forums/showthread.php?t=44872. It says "The problem was that after_save, the bean discards all the new field info. So we needed to use $bean->retrieve($bean->id); to get it back"
Thursday, July 8, 2010
sugarcrm warning message
I created a new module. When i save a new entry, sugarcrm will show this message:
Warning: preg_match() expects parameter 2 to be string, array given in /var/www/crm/include/utils.php on line 1715
Call Stack
# Time Memory Function Location
1 0.0000 72612 {main}( ) ../index.php:0
2 0.0001 73516 require_once( '/var/www/crm/include/entryPoint.php' ) ../index.php:38
3 0.0019 214344 clean_incoming_data( ) ../entryPoint.php:124
4 0.0067 235068 clean_string( ) ../utils.php:1797
5 0.0068 235120 preg_match ( )
../utils.php:1715
So I traced the code back to line 1797 in utils.php, and found this code "if (isset($_REQUEST['language'])) clean_string($_REQUEST['language']);".
Actually in this new module, i created a field called 'language' and it is MultiSelect type, which is an array. After i change the field name, this warning message disappeared.
Warning: preg_match() expects parameter 2 to be string, array given in /var/www/crm/include/utils.php on line 1715
Call Stack
# Time Memory Function Location
1 0.0000 72612 {main}( ) ../index.php:0
2 0.0001 73516 require_once( '/var/www/crm/include/entryPoint.php' ) ../index.php:38
3 0.0019 214344 clean_incoming_data( ) ../entryPoint.php:124
4 0.0067 235068 clean_string( ) ../utils.php:1797
5 0.0068 235120 preg_match ( )
../utils.php:1715
So I traced the code back to line 1797 in utils.php, and found this code "if (isset($_REQUEST['language'])) clean_string($_REQUEST['language']);".
Actually in this new module, i created a field called 'language' and it is MultiSelect type, which is an array. After i change the field name, this warning message disappeared.
Sunday, April 4, 2010
DON'T USE 'CREATE TABLE XXX SELECT * FROM YYY' TO BACKUP AN INNODB TABLE
The reason is simple: the backup table will lose all key/index attributes from the original innodb table.
Monday, March 22, 2010
Replace built-in PHP fatal errors with exceptions
We may want to use Exception instead of php4 style error handling systems to make error handling more consistent. We can build a bridge from old error handling to the new Exception handling.
class PhpErrorException extends Exception {}
function errorHandler($errno, $errstr, $errfile, $errline) {
throw new PhpErrorException ($errstr,$errno);
}
$handler= set_error_handler('errorHandler');
Now you can test it. For example: run echo 5/0; and you will see an exception is thrown instead that a PHP warning is given off.
class PhpErrorException extends Exception {}
function errorHandler($errno, $errstr, $errfile, $errline) {
throw new PhpErrorException ($errstr,$errno);
}
$handler= set_error_handler('errorHandler');
Now you can test it. For example: run echo 5/0; and you will see an exception is thrown instead that a PHP warning is given off.
Friday, March 19, 2010
memory usage in PHP
A very excellent blog about PHP memory usage: http://blog.preinheimer.com/index.php?/archives/354-Memory-usage-in-PHP.html
Unlike C/C++ developers, most PHP developers never think about memory management in PHP. It is true that PHP can free the memory after a request is processed. However, if you use PHP to process critical tasks with large amount of data, you'd better read this blog carefully, and test its memory-usage-example.php by yourself. You will see how different it is.
Unlike C/C++ developers, most PHP developers never think about memory management in PHP. It is true that PHP can free the memory after a request is processed. However, if you use PHP to process critical tasks with large amount of data, you'd better read this blog carefully, and test its memory-usage-example.php by yourself. You will see how different it is.
Subscribe to:
Posts (Atom)