I am having a field 'leavepack_id' in a table with the below table definition
CREATE TABLE IF NOT EXISTS `#__position` (
`position_id` int(11) NOT NULL AUTO_INCREMENT,
`leavepack_id` int(9) DEFAULT NULL,
PRIMARY KEY (`position_id`),
FOREIGN KEY (`leavepack_id`) REFERENCES `#__leavepack`(`leavpack_id` ON UPDATE CASCADE ON DELETE SET NUL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
By the above definition, the field leavepack_id can accept a valid id or NULL.
Here is the code I have in my model
if ($data['leavepack_id'] == '') $data['leavepack_id'] = NULL;
$success = parent::save($data);
The problem:
When an existing record which has a valid id is saved with null then the update retains the ID as is instead of storing as NULL.
In other words:
Assume the existing record has position_id = 1 and leavepack_id = 2
. When updating the value comes as position_id = 1 and leavepack_id = NULL
. The problem is NULL is not stored.
After debugging some of the core Joomla files, I am able to find the root cause. The bind() function in JTable ignores the field that has NULL value.
Can someone please help me on this.