MJoin Fails with Parse error: syntax error, unexpected '['

MJoin Fails with Parse error: syntax error, unexpected '['

wgrwgr Posts: 3Questions: 1Answers: 0

I am able to use joins easily on my current architecture and have used them extensively. I am trying to use an Mjoin for a categorization table that links games and category in a Many to one format. I don't see a material difference between my existing setup and the code example. Some ideas on how to debug this would be great as this seems to be the only thing I am unable to get working. Postgres, PHP

                                 Table "public.games"
  Column   |         Type          |                     Modifiers
-----------+-----------------------+----------------------------------------------------
 id        | integer               | not null default nextval('games_id_seq'::regclass)
 name      | character varying(64) | not null
 type      | integer               | not null
 positions | integer               | not null
 category  | integer               | default 9
Indexes:
    "games_pkey" PRIMARY KEY, btree (id)
    "games_name_key" UNIQUE, btree (name)
Referenced by:
 TABLE "categories" CONSTRAINT "categories_game_id_fkey" FOREIGN KEY (game_id) REFERENCES games(id)


                                Table "public.category"
 Column |         Type          |                       Modifiers
--------+-----------------------+-------------------------------------------------------
 id     | integer               | not null default nextval('category_serial'::regclass)
 name   | character varying(30) |
Indexes:
    "category_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "categories" CONSTRAINT "categories_category_id_fkey" FOREIGN KEY (category_id) REFERENCES category(id)

     Table "public.categories"
   Column    |  Type   | Modifiers
-------------+---------+-----------
 game_id     | integer | not null
 category_id | integer | not null
Indexes:
    "category_pk" PRIMARY KEY, btree (game_id, category_id)
Foreign-key constraints:
    "categories_category_id_fkey" FOREIGN KEY (category_id) REFERENCES category(id)
    "categories_game_id_fkey" FOREIGN KEY (game_id) REFERENCES games(id)


<!DOCTYPE html> <html> <head> <meta charset=\"utf-8\"> <link rel=\"shortcut icon\" type=\"image/ico\" href=\"http://www.datatables.net/favicon.ico\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, minimum-scale=1.0, user-scalable=no\"> <title>Edit Game Names</title> <link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css\"> <link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.datatables.net/buttons/1.6.1/css/buttons.dataTables.min.css\"> <link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.datatables.net/select/1.3.1/css/select.dataTables.min.css\"> <link rel=\"stylesheet\" type=\"text/css\" href=\"css/editor.dataTables.min.css\"> <style type=\"text/css\" class=\"init\"> </style> <script type=\"text/javascript\" language=\"javascript\" src=\"https://code.jquery.com/jquery-3.3.1.js\"></script> <script type=\"text/javascript\" language=\"javascript\" src=\"https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js\"></script> <script type=\"text/javascript\" language=\"javascript\" src=\"https://cdn.datatables.net/buttons/1.6.1/js/dataTables.buttons.min.js\"></script> <script type=\"text/javascript\" language=\"javascript\" src=\"https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js\"></script> <script type=\"text/javascript\" language=\"javascript\" src=\"js/dataTables.editor.min.js\"></script> <script type=\"text/javascript\" language=\"javascript\" class=\"init\"> var editor; // use a global for the submit and return data rendering in the examples $(document).ready(function() { editor = new $.fn.dataTable.Editor( { ajax: \"controllers/edit_games.php\", table: \"#edit_games\", fields: [ { label: \"Game Name:\", name: \"games.name\" }, { label: \"Type:\", name: \"games.type\", type: \"select\", placeholder: \"Select a game type\" }, { label: \"Positions:\", name: \"games.positions\" }, { \"label\": \"Categories:\", \"name\": \"category[].id\", \"type\": \"checkbox\" } ] } ); $('#edit_games').DataTable( { dom: \"Bfrtip\", ajax: { url: \"controllers/edit_games.php\", type: 'POST' }, columns: [ { data: \"games.name\" }, { data: \"game_types.name\" }, { data: \"games.positions\" }, { data: \"category\", render: \"[, ].name\" } ], select: true, buttons: [ { extend: \"create\", editor: editor }, { extend: \"edit\", editor: editor }, { extend: \"remove\", editor: editor } ] } ); } ); </script> </head> <body class=\"edit_games php\"> <table id=\"edit_games\" class=\"display\" cellspacing=\"0\" width=\"100%\"> <thead> <tr> <th>Game Name</th> <th>Type</th> <th>Positions</th> <th>Categories</th> </tr> </thead> <tfoot> <tr> <th>Game Name</th> <th>Type</th> <th>Positions</th> <th>Categories</th> </tr> </tfoot> </table> </body>
<?php

// DataTables PHP library
include( "../lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;


/*
 * Example PHP implementation used for the join.html example
 */
Editor::inst( $db, 'games' )
    ->field( 
        Field::inst( 'games.name' ),
        Field::inst( 'games.positions' ),
        Field::inst( 'games.type' )
            ->options( Options::inst()
                ->table( 'game_types' )
                ->value( 'id' )
                ->label( 'name' )
            ),
        Field::inst( 'game_types.name' )
    )
    ->leftJoin( 'game_types', 'game_types.id', '=', 'games.type' )
    ->join(
        Mjoin::inst( 'category' )
            ->link( 'games.id', 'categories.game_id' )
            ->link( 'category.id', 'categories.category_id' )
            ->order( 'name asc' )
            ->fields(
                Field::inst( 'id' )
                    ->options( Options::inst()
                        ->table( 'category' )
                        ->value( 'id' )
                        ->label( 'name' )
                    ),
                Field::inst( 'name' )
            )
    )
    ->process($_POST)
    ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    Can you show me the complete error message please

    Thanks,
    Allan

  • wgrwgr Posts: 3Questions: 1Answers: 0

    Sorry realized I left that out. Parse error: syntax error, unexpected '[' in domain/public_html/htdocs/editors/lib/Editor/Join.php on line 768
    The referenced code section.

        /**
         * Validate input data
         *
         * @param array $errors Errors array
         * @param Editor $editor Editor instance
         * @param string[] $data Data to validate
         * @param string $action `create` or `edit`
         * @internal
         */
        public function validate ( &$errors, $editor, $data, $action )
        {
            if ( ! $this->_set && ! isset($data[$this->_name.'-many-count']) ) {
                return;
            }
    
            $this->_prep( $editor );
    
            $joinData = isset($data[$this->_name]) ? 
            $data[$this->_name] : 
            [];
    
            for ( $i=0 ; $i<count($this->_validators) ; $i++ ) {
                $validator = $this->_validators[$i];
                $fn = $validator['fn'];
                $res = $fn( $editor, $action, $joinData );
    
                if ( is_string($res) ) {
                    $errors[] = array(
                        "name" => $validator['fieldName'],
                        "status" => $res
                    );
                }
            }
    
            if ( $this->_type === 'object' ) {
                $this->_validateFields( $errors, $editor, $joinData, $this->_name.'.' );
            }
            else {
                for ( $i=0 ; $i<count($joinData) ; $i++ ) {
                    $this->_validateFields( $errors, $editor, $joinData[$i], $this->_name.'[].' );
                }
            }
        }
    
  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin
    Answer ✓

    Ah:

    [];

    Are you using PHP 5.3? There was a fix for this a little while ago, but it hasn't been promoted to a release yet. We'll be tagging and updating in the next few weeks. Until then, you could make that change locally. That said, it would be a really good idea to upgrade from PHP 5.3. It hasn't been supported by the PHP team since 2012.

    Allan

  • wgrwgr Posts: 3Questions: 1Answers: 0

    Yikes, looks like that was the issue. I'll make the changes locally. Thank you!

This discussion has been closed.