Jump to content

Fieldtype::getLoadQueryAutojoin(), what is it used for?


abdus
 Share

Recommended Posts

I'm developing a multi value fieldtype and there's this method called getLoadQueryAutojoin().

// FieldtypeMulti.php

/**
 * Return the query used for Autojoining this field (if different from getLoadQuery) or NULL if autojoin not allowed. 
 *
 * @param Field $field
 * @param DatabaseQuerySelect $query
 * @return DatabaseQuerySelect|NULL
 *
 */
public function getLoadQueryAutojoin(Field $field, DatabaseQuerySelect $query) {
    if($this->get('useOrderByCols')) {
        // autojoin is not used if sorting or pagination is active
        $orderByCols = $field->get('orderByCols');
        if(count($orderByCols) > 0) return null;
    }
    $table = $this->database->escapeTable($field->table);	
    $schema = $this->trimDatabaseSchema($this->getDatabaseSchema($field)); 
    $fieldName = $this->database->escapeCol($field->name); 
    $separator = self::multiValueSeparator; 
    foreach($schema as $key => $unused) {
        $query->select("GROUP_CONCAT($table.$key SEPARATOR '$separator') AS `{$fieldName}__$key`"); // QA
    }		
    return $query; 
}

Why would someone need to autojoin a field, or not need a autojoin? Any benefits/drawbacks? I havent used the feature it at all in my 3+ years PW experience.

I'm not sure whether I need to implement it but I am leaning towards returning null and disable it altogether.

Link to comment
Share on other sites

From Ryan:

Quote

Using autojoin on FieldtypeMulti fields (like pages, files, images, comments, etc.) is a tricky matter because the primary page load query is designed to execute with 1 fetch with no data repetition. That ensures the fastest possible performance. Fields with multiple entries (FieldtypeMulti) are not well suited to being retrieved in 1 fetch. However, ProcessWire can still do it using a MySQL GROUP_CONCAT function to bundle the multiple entires into a string suitable for that 1 fetch. The problem is that MySQL has a byte limit on how much it will GROUP_CONCAT (it's a MySQL setting that we can't count on). As a result, autojoin is okay to use on simple multi-entry fields like Page relations that don't have hundreds of entries–that's because it's just a bunch of integers. But it's not well suited for more complex fields like images that might have long descriptions. As a result, I don't recommend using it with file/image fieldtypes.

 

  • Like 4
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...