Jump to content

Fieldtype module, field names cannot be used in selectors


Recommended Posts

Posted (edited)

i wrote a simple fieldtype which implements a date_from and date_to sub field. When defining a field "interval" and and then using it in a template "sampletemplate" everything works.

In "sampletemplate.php"  fields can be accessed by $page->interval->date_from   and   $page->interval->date_to - everything fine!

When using in a selector these fieldnames are invalid. e.g.

$pages->find("template=sampletemplate, interval.date_to > today")  throws an exception, no column with that name...

the schema definition for the fields is

    public function getDatabaseSchema(Field $field) : array  {
        $schema = parent::getDatabaseSchema($field);
        $schema['data'] = 'date default null'; // date_from
        $schema['to']   = 'date default null'; // date_to
        $workspace->url = 'KEY data (`data`, `to`)';
        $schema['keys']['to']   = 'KEY `to` (`to`)';
        return $schema; 
    }

i.e. date_from is mapped on standard column 'data' and date_to is mapped on 'to'.  When i use the real database column name 

$pages->find("template=sampletemplate, interval.to > today")

it works.

How is it possible to "correct" the names so that the name to access the field (e.g. $page->interval->data_from) and the name to be used in a selector is the same?

and: Is there a description of the PW database schema definition to be used in a getDatabaseSchema() ?

Edited by thei
Posted

i did the following

    public function getMatchQuery($query, $table, $subfield, $operator, $value) {
        $subfieldMap = [ 'date_from'=>'data', 'date_to'=>'to'];
        $sub = $subfieldMap[$subfield];
        return parent::getMatchQuery($query, $table, $sub, $operator, $value);
    }

overwriting the basic function getMatchQuery() defined in core/FieldType.php

this helps.

Is this the regular way to implement Fieldtypes?

Posted (edited)

next effect: mapping of fieldnames to true columns names has also be handled by overloading  getMatchQuerySort() of FieldType

    private function mapColumnNames($fieldName)  {
        $subfieldMap = [ 'date_from'=>'data', 'date_to'=>'to'];
        $col = array_key_exists($fieldName, $subfieldMap) ? $subfieldMap[$fieldName] : $fieldName;
        return $col;
    }

    /**
     * adapts the getMatchQuery() of the parent by mapping the column names
     */
    public function getMatchQuery($query, $table, $subfield, $operator, $value) {
        return parent::getMatchQuery($query, $table, $this->mapColumnNames($subfield), $operator, $value);
    }

    /**
     * adapts the getMatchQuerySort() of the parent by mapping the column names
     */
    public function getMatchQuerySort(Field $field, $query, $table, $subfield, $desc) {
        $col = $this->mapColumnNames($subfield);
		// return PW internal sort query string
        return "_sort_{$field->name}_{$subfield}.{$col} " . ($desc ? 'DESC' : 'ASC');
    }

Welcome to the PW reverse engineering club - Happy hour wednesday 17-18 pm

Edited by thei

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...