WPDB not saving to DB and no error given

Ok, I am not a WP noob by any means but I am stuck on this one...

I am working on importing XML data into a custom table. The code used is too much to paste here so I will share the problem area and hopefully I just overlooked something simple...

For dev, I do use:

define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true ); 

Lets start with the DB

    // Build table structure     CREATE TABLE {$wpdb->dayforce_milestones_data} (       reference_code bigint(20) NOT NULL NOT NULL ,       first_name varchar(60) NOT NULL ,       last_name varchar(60) NOT NULL ,       start_date DATE NULL,       birthday DATE NULL,       department varchar(60) NOT NULL ,       title varchar(60) NOT NULL ,       status tinyint(1) NOT NULL DEFAULT 1 ,       PRIMARY KEY  (reference_code) ,       KEY start_date (start_date) ,       KEY birthday (birthday) ,       KEY status (status)      ) {$charset_collate} ; 

I modified this from my DBDelta object for simplicity. $charset_collate is defined as:

$charset_collate = ''; if ( ! empty( $wpdb->charset ) ) {     $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; } if ( ! empty( $wpdb->collate ) ) {     $charset_collate .= " COLLATE {$wpdb->collate}"; } 

and $wpdb->dayforce_milestones_data is defined as wptests_dayforce_milestones_data

Without having to explain or get into the logic of how I am building the data I have an array of objects that utilize the __toString magic method to output the value for the query for a single object... this looks like this:

/**  * Output object for insert query string value of object  * @return string  */ public function __toString() {     global $wpdb;      return '(' .     $wpdb->_escape($this->getReferenceCode()).', '.     "'" . $wpdb->_escape($this->getFirstName()) . "', " .     "'" . $wpdb->_escape($this->getLastName()) . "', " .     "'" . $wpdb->_escape($this->getStartDate()) . "', " .     "'" . $wpdb->_escape($this->getBirthday()) . "', " .     "'" . $wpdb->_escape($this->getDepartment()) . "', " .     "'" . $wpdb->_escape($this->getTitle()) . "', " .     $wpdb->_escape($this->getStatus()) . ' ' .     ')'; } 

referenceCode and Status are integers. This allows me to use the object like this:

/**  * Saves the items to the DB  * @return bool  */ public static function save() {     global $wpdb;     $_itemsArray = array_chunk(DayForceItems::getItems(), self::INSERT_LIMIT);     foreach ($_itemsArray as $itemChunk) {         // Start new Query         $_sql = "INSERT INTO {$wpdb->dayforce_milestones_data}                            (reference_code, first_name, last_name, start_date, birthday, department, title, status)                        VALUES "; // reset query for new batch         // Build values         $_sql .= implode(','.PHP_EOL, $itemChunk);         // Append ON DUPLICATE to query         $_sql .= PHP_EOL." ON DUPLICATE KEY UPDATE              first_name = VALUES(first_name),             last_name = VALUES(last_name),             start_date = VALUES(start_date),             birthday = VALUES(birthday),             department = VALUES(department),             title = VALUES(title),             status = VALUES(status) ;";          print $_sql.PHP_EOL.PHP_EOL;          // Run query         $wpdb->show_errors();         $wpdb->query($_sql);          print $wpdb->print_error().PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL;     } } 

With INSERT_LIMIT set to 10, this outputs:

INSERT INTO wptests_dayforce_milestones_data (reference_code, first_name, last_name, start_date, birthday, department, title, status)      VALUES     (8932, 'Jose', 'HIDDEN', '2016-06-07', '1996-01-22', 'Onion PM', 'Onion Peeler', 1 ),         (8933, 'Yunior', 'HIDDEN', '2016-06-07', '1996-04-22', 'Sanitation', 'Sanitation Tech 1', 1 ),         (8934, 'Miguel', 'HIDDEN', '2016-06-08', '1979-03-13', 'Retail B', 'Specialist', 1 ),         (8935, 'Sandra', 'HIDDEN', '2016-07-08', '1990-03-26', 'production 1', 'Line Worker', 0 ),         (8936, 'Reyna', 'HIDDEN', '2016-07-19', '1988-06-26', 'production 1', 'Line Worker', 0 ),         (8937, 'Raquel', 'HIDDEN', '2016-06-13', '1989-09-12', 'Retail B', 'Specialist', 0 ),         (8938, 'Wilmer', 'HIDDEN', '2016-06-14', '1988-07-18', 'Sanitation', 'Sanitation Tech 1', 0 ),         (8939, 'Alva', 'HIDDEN', '2016-07-18', '1996-06-05', 'production 1', 'Line Worker', 1 ),         (8940, 'William', 'HIDDEN', '2016-06-13', '1987-11-23', 'production 1', 'Line Lead', 0 ),         (8941, 'Yaneth', 'HIDDEN', '2016-06-13', '1974-12-15', 'production 1', 'Line Worker', 1 )      ON DUPLICATE KEY UPDATE          first_name = VALUES(first_name),         last_name = VALUES(last_name),         start_date = VALUES(start_date),         birthday = VALUES(birthday),         department = VALUES(department),         title = VALUES(title),         status = VALUES(status) ; 

Now if I copy and paste this query, it works fine... The issue is when I use $wpdb->query it does nothing and gives no error. Even removing the ON DUPLICATE part still no dice...

The only error I have ever seen was something like:

WordPress database error  for query ... 

OR

WordPress database error: [] ... 

Where ... is the actual query, that if you copy an paste, runs perfectly in MySQL. When I output $wpdb->last_error I get nothing... even when I see the above "errors".

Ok, so I thought I spent enough time with the upsert and decided $wpdb->replace would suffice for now... so I tried doing this:

/**  * Saves the items to the DB  * @return bool  */ public static function save() {     global $wpdb;      $_items = DayForceItems::getItems();      $wpdb->show_errors();      foreach ($_items as $item) {          $result = $wpdb->replace(             $wpdb->dayforce_milestones_data,             array(                 'reference_code' => $item->getReferenceCode(),                 'first_name' => $item->getFirstName(),                 'last_name' => $item->getLastName(),                 'start_date' => $item->getStartDate(),                 'birthday' => $item->getBirthday(),                 'department' => $item->getDepartment(),                 'title' => $item->getTitle(),                 'status' => $item->getStatus()             ),             array(                 '%d',                 '%s',                 '%s',                 '%s',                 '%s',                 '%s',                 '%s',                 '%d'             )         );          var_dump($result);          $wpdb->print_error();     }  } 

Which gives me a var_dump of int(1) but nothing is in the database.

Baffled, I tried to just run a hard-coded query:

$wpdb->query("     INSERT INTO {$wpdb->dayforce_milestones_data}        (reference_code, first_name, last_name, start_date, birthday, department, title, status)       VALUES        (001002, 'W F Fritz', 'Stelter', '2015-12-31', '1959-12-01', 'Executives', 'Vice President of Sales', 1)  "); 

Which again, gives no errors but nothing is saved to the database... I also tried this with the table name hardcoded too and the same issue.

So here I am.. looking for guidance.

It should be noted that I am using PHPUnit to run my tests and that is how this is being ran. This is using a properly setup WordPress testing suite and I have no issues with this on any other setup that I develop. I also tried this using the actual site and the same issue (no testing, actual WordPress)

Here is an example of the test that runs this...

class dayForceMilestoneTests extends \WP_UnitTestCase {      public function testXMLParser() {         $tmp = \DayForce_Milestones\Cron\Processor::run();         // Do assertions         $this->assertTrue($tmp, 'Failed to parse the XML directory');     }  } 

It's obvious the test is running properly and it is obvious that the output of the query is correct and copy/pasting the output is a valid MySQL query and works fine when running in PHPMyAdmin and MySQL command line. The issue here appears to be $wpdb. The test does create the database properly and all other queries appear to work properly. The only issue I am having is that none of this is being saved to the database and no errors are given.

Thanks to any help on this.

EDIT I switched from Windows to Ubuntu for development and now this issue is no longer happening. I am assuming this has to do with the test suite on Windows. It now works as expected when testing on Linux.

Replay

Category: plugin development Time: 2016-07-30 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 2.420 (s). 13 q(s)