Jump to content

[solved] New field is not added


szabesz
 Share

Recommended Posts

Hi,
I moved a site to a new home and ran into a "strange" issue:

does-not-create-field.thumb.gif.1c8769715c6d591a35e0bb2e333a564f.gif

No field is added. I have no error message "anywhere", not in the console, no PHP, not in server logs either.

The strange thing is that I can Duplicate fields: Setup > Fields > Actions > Duplicate/clone this field?
Meaning it is just the "Add New Field" which does nothing. I can also create pages.
Also,  "Add New Field" does work in local development environment (same site and "same state", of course), it is just the new server environment which is problematic.

Any ideas for troubleshooting? Thanks in advance :)

Link to comment
Share on other sites

Looking at ProcessField.module, this is where a field is saved

// ProcessField.module

public function ___executeAdd() {

    // unrelated shortcut feature: double check that all field tables actually exist
    // and re-create them in instances where they don't (like if a bunch of fields
    // were migrated over in an SQL dump of the "fields" table or something). 
    $this->wire("fields")->checkFieldTables(); 

    return $this->executeEdit(); 
}

public function ___executeEdit() {
    // ...
    if($this->field->id) {
        $headline = sprintf($this->_x('Edit Field: %s', 'edit headline'), $this->field->name); // Headline when editing a field
    } else {
        $headline = $this->_x('Add New Field', 'add headline'); // Headline when adding a field
    }
    // ...
    return $out;
}

And this is the checkFieldTables() method

protected function checkFieldTable(Field $field) {
    // if(!$this->wire('config')->debug) return;
    $database = $this->wire('database'); 
    $table = $database->escapeTable($field->getTable());
    if(empty($table)) return;
    $exists = $database->query("SHOW TABLES LIKE '$table'")->rowCount() > 0;
    if($exists) return;
    try {
        if($field->type && count($field->type->getDatabaseSchema($field))) {
            if($field->type->createField($field)) $this->message("Created table '$table'"); 
        }
    } catch(\Exception $e) {
        $this->trackException($e, false, $e->getMessage() . " (checkFieldTable)"); 
    }
}

Here I can deduce:

  • There's something wrong with the field table, it already exists but PW cant associate the field with its id/table?
  • $field->type is empty string or field does not return a schema.

Having null fieldtype probably means something wrong with the Module cache, refreshing cache may help.

Going forward, this is how field table is created

public function ___createField(Field $field) {

    $database = $this->wire('database');
    $schema = $this->getDatabaseSchema($field); 

    if(!isset($schema['pages_id'])) throw new WireException("Field '$field' database schema must have a 'pages_id' field."); 
    if(!isset($schema['data'])) throw new WireException("Field '$field' database schema must have a 'data' field."); 

    $table = $database->escapeTable($field->table); 
    $sql = 	"CREATE TABLE `$table` (";

    foreach($schema as $f => $v) {
        if($f == 'keys' || $f == 'xtra') continue; 
        $sql .= "`$f` $v, "; 
    }

    foreach($schema['keys'] as $v) {
        $sql .= "$v, ";
    }
    
    $xtra = isset($schema['xtra']) ? $schema['xtra'] : array();
    if(is_string($xtra)) $xtra = array('append' => $xtra); // backwards compat: xtra used to be a string, what 'append' is now. 
    $append = isset($xtra['append']) ? $xtra['append'] : '';

    $sql = rtrim($sql, ", ") . ') ' . $append;
    
    $query = $database->prepare($sql);
    $result = $query->execute();

    if(!$result) $this->error("Error creating table '{$table}'");

    return $result; 
}

From here it seems you'd get an error if table couldnt be created, so no issues with table creation. Also you've mentioned that duplication works fine.

These are somewhat obvious deductions from the source code.  But, to me, everything points to fields table in the DB. 

  • Like 2
Link to comment
Share on other sites

Thanks @abdus for taking a look at it!

24 minutes ago, abdus said:

probably means something wrong with the Module cache, refreshing cache may help

Unfortunately it did not help. I also tried cleaning all the other ProcessWire cache just in case but it did not help either.

However, since initially I forgot about refreshing the module cache, you gave me the idea to refresh the browser cache too (Chrome: Empty cache and hard reload).

And this one did help!

I still find it strange that this was the issue but browser caches can lead to troubles :) Thanx once more!

Edited by szabesz
typo
  • Like 1
Link to comment
Share on other sites

Glad to help :)

I'm intrigued, though.
I thought Empty Cache and Hard Reload affected just the asset/file cache. Does chrome cache redirects too? Or how can file cache stop fields from being saved?

 

Also, a useful hook to keep around. To refresh module cache on superuser login:

$this->addHookAfter('Session::loginSuccess', function (HookEvent $e) {
    /** @var User $user */
    $user = $e->arguments('user');
    if ($user->isSuperuser()) {
        $e->modules->refresh();
    }
});

 

  • Like 1
Link to comment
Share on other sites

5 hours ago, abdus said:

Or how can file cache stop fields from being saved?

I have a clue, it might help us in understanding what could have happened:
Normally, when we are on the first step page of "Add New Field" we have breadcrumb Admin > Setup > Fields with "Fields" linking to example.com/admin/setup/field/
However, right before clearing the browser cache I noticed that after the failed field adding attempt, it was linking to: example.com/admin/setup/field/add/ and that also made me suspicious :)

I'm guessing here but all this might be related to the fact that the site was moved. I cannot explain why though...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...