LoadById returns random record for null value

thread: 8 messages  |  last: a year ago  |  started: wednesday, april 7, 2010, 7:31 pm pdt


#1  |  David B (Adelaide, SA) Australia
Wednesday, April 7, 2010, 7:31 PM PDT

Hi all,

I don't know if this is fixed in the latest version of qcodo, but I have been having trouble because of the following intersting scenario:

$objMyObject = MyObject::LoadById(null);

the above line retrieves a MyObject object apparently at random from my database. Is this expected behaviour? I am having to check:

if ($intId !== null) $objMyObject = MyObject::LoadById($intId)

...first everywhere. Has anybody else experienced this (using mysqli5)?

#2  |  David B (Adelaide, SA) Australia
Sunday, April 11, 2010, 6:28 PM PDT

Hmm perhaps nobody thinks this is important/just bad coding on my part because I should know what my variable's value is first...

ok but consider the scenario where the id is a foreign key of some object:

as well as:

$objMyObject = MyObject::LoadById($intId);

returning a randon object because $intId is null, also:

MyObject::LoadById($objAnotherObject->MyObjectId)

where MyObjectId === null returns a random object too. This really does make it a bit of a frustrating issue for me.

#3  |  Chris Peterson (Seattle, WA) United States of America Qcodo Core Contributor
Monday, April 12, 2010, 2:19 PM PDT

What version of Qcodo and what version of MySQL are you using?

Have you monitored the MySQL logs to see what query is actually being run against the database?

I have tried the LoadById(null) on a few different tables now and I am seeing a blank response as expected.  Maybe do some other testing on the object you are passing in to make sure it truly is sending a null value.  Also did you override the LoadById() method and/or change anything that would alter the results?

#4  |  Mike Ho (Sunnyvale, CA) United States of America Qcodo Administrator
Monday, April 12, 2010, 2:37 PM PDT

David,

Also, if you get a chance, could you show us the table definition?  Best / most detailed way of doing this is to run a

mysql> show create table TABLE_NAME;

in the MySQL command line console.  If you could post the results of that, it might shed some light on the issue.

But yeah, like Chris I've confirmed that passing in both null and empty string yields a correct null result as expected.

#5  |  David B (Adelaide, SA) Australia
Monday, April 12, 2010, 4:29 PM PDT

Thanks guys. Sorry for not having a more concrete/practical example but we recently had to upgrade qcodo to the latest version and I just wanted to see if this issue had been fixed. I think that it has (or else I had a werid combination of database adapter/php before or something).

Mysql adapter is/was mysqli5 and the following code was enough to reproduce this problem on 0.3.43 (and this was tested extensively):

$objUser = Report::LoadById($objReport->ApprovedByUserId);

Note this is without a relationship defined on ApprovedByUserId. I had to use the following workaround:

$objUser = (is_null($obj->ApprovedByUserId) ? '' : User::LoadById($obj->ApprovedByUserId));

I found that running User->LoadById(null) was enough to cause a random User object to be loaded. After upgrading to .4.something (how do you find the version?) it now seems to be behaving. If I can reproduce this issue again I will post. Thanks.

#6  |  David B (Adelaide, SA) Australia
Monday, April 12, 2010, 5:37 PM PDT

Just for prosperity's sake, here is the table create sql from a table where is was happening:

| plan  | CREATE TABLE `plan` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `previous_id` bigint(20) unsigned default NULL COMMENT '',
  `plan_proforma_id` bigint(20) unsigned NOT NULL default '0' COMMENT '',
  `plan_contact_id` bigint(20) unsigned default '0',
  `plan_context_id` bigint(20) unsigned NOT NULL default '0' COMMENT '',
  `plan_progress_id` bigint(20) unsigned default NULL COMMENT '',
  `plan_organisation_id` bigint(20) unsigned NOT NULL default '0',
  `plan_status_type_id` int(10) unsigned default NULL COMMENT '',
  `t_stamp_plan_modified` datetime default NULL COMMENT '',
  `is_editable` tinyint(4) default NULL COMMENT '',
  `year` varchar(7) NOT NULL default '0' COMMENT '',
  `submitted_to_c_e_o` tinyint(1) unsigned default NULL COMMENT '',
  `version` int(20) NOT NULL default '0' COMMENT '',
  `optional_financial_plan` mediumblob COMMENT '',
  `optional_financial_plan_filename` varchar(128) default NULL COMMENT '',
  `financial_plan` mediumblob COMMENT '',
  `financial_plan_filename` varchar(128) default NULL COMMENT '',
  `staff_html` mediumtext COMMENT '',
  `board_html` mediumtext COMMENT '',
  `show_user_name` int(1) unsigned default '0' COMMENT '',
  `executive_summary_overview` mediumtext,
  `submitted_by_user_id` bigint(20) unsigned default '0' COMMENT '',
  `approved_by_user_id` bigint(20) unsigned default '0' COMMENT '',
  `rejected_by_user_id` bigint(20) unsigned default '0' COMMENT '',
  `archived_by_user_id` bigint(20) unsigned default '0' COMMENT '',
  `submitted_to_c_e_o_by_user_id` bigint(20) unsigned default NULL,
  `declined_by_c_e_o_user_id` bigint(20) unsigned default NULL COMMENT '',
  `notes_org` mediumtext COMMENT '',
  `notes_s_t_o` mediumtext COMMENT '',
  `notes_c_o` mediumtext,
  `feedback_content` text COMMENT '',
  `feedback_technical` text COMMENT '',
  `feedback_other` text COMMENT '',
  `comments_submit` text COMMENT '',
  `comments_s_t_o` text COMMENT '',
  `comments_from_c_e_o` text COMMENT '',
  `comments_to_c_e_o` text COMMENT '',
  `t_stamp_approved` datetime default NULL COMMENT '',
  `t_stamp_rejected` datetime default NULL COMMENT '',
  `t_stamp_submitted` datetime default NULL COMMENT '',
  `t_stamp_submitted_to_c_e_o` datetime default NULL COMMENT '',
  `t_stamp_declined_by_c_e_o` datetime default NULL COMMENT '',
  `last_modified_user_id` bigint(20) unsigned default NULL,
  `created_by_user_id` bigint(20) unsigned default NULL,
  `t_stamp_updated` datetime default NULL COMMENT '',
  `t_stamp_created` datetime default NULL COMMENT '',
  PRIMARY KEY  (`id`),
  KEY `plan_status_type_id` (`plan_status_type_id`),
  KEY `plan_context_id` (`plan_context_id`),
  KEY `plan_contact_id` (`plan_contact_id`),
  KEY `plan_progress_id` (`plan_progress_id`),
  KEY `plan_proforma_id` (`plan_proforma_id`),
  KEY `year` USING BTREE (`year`),
  KEY `plan_organisation_id` USING BTREE (`plan_organisation_id`)
) ENGINE=MyISAM AUTO_INCREMENT=6842 DEFAULT CHARSET=latin1 |
.bc
#7  |  Mike Ho (Sunnyvale, CA) United States of America Qcodo Administrator
Tuesday, April 13, 2010, 7:33 AM PDT

David... definitely a weird bug that you saw, and yeah, it's definitely something that I hadn't seen before.

So I'm not really sure why/how v0.4.x fixed it, but I'm glad it's fixed now for you.

As a note, if you ever want to check which version you are running, you can either view your includes/qcodo/_core/qcodo.inc.php file (it's at the top of the file), or you can do

<?php
    
print QCODO_VERSION;
?>

or finally, if you are in the command shell, you can always run

# Unix/linux/mac
/path/to/my/cli/qcodo

# windows
c:\path\to\cli\qcodo.bat

The Qcodo CLI runner will report the Qcodo version at the top of the menu.

#8  |  David B (Adelaide, SA) Australia
Tuesday, April 13, 2010, 4:02 PM PDT

Thanks Mike. Sorry about posting about an issue when I can no longer reproduce it! It did cause a bit of trouble for us and we bug-tested extensively to find the cause; but it now seems to be behaving as expected and that is nice. I will remember how to find the version too; my CLI environment is not working; we only get it working for updates (custom config files, long story).



Copyright © 2005 - 2012, Quasidea Development, LLC
This open-source framework for PHP is released under the terms of The MIT License.