PHP Connector with Yii 2.0

I have downloaded the newest PHP Connector 2.2 version. I extracted it to a yii subfolder and created a controller:

[code]namespace app\controllers;

use Yii;
use app\models\Firmy;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

require_once (DIR . ‘/…/vendor/dhtmlx/grid_connector.php’);
require_once (DIR . ‘/…/vendor/dhtmlx/db_phpyii.php’);
/**

  • FirmyController implements the CRUD actions for Firmy model.
    /
    class FirmyController extends Controller
    {
    /
    *

    • @inheritdoc
      */
      public function behaviors()
      {
      return [
      ‘verbs’ => [
      ‘class’ => VerbFilter::className(),
      ‘actions’ => [
      ‘delete’ => [‘POST’],
      ],
      ],
      ];
      }

    /**

    • Generates grid view
      */
      public function actionGrid()
      {
      $connector = new GridConnector(null, “PHPYii”);
      $connector->configure(new Firmy(),“id”,“nr_kat,nazwa,nazwa_skr,ulica,miejscowosc,kod_pocztowy,nip,regon,id_formy_dzialalnosci,vat,status”);
      $connector->render();
      }[/code]

unfortunately I get the following error:

[code]PHP Fatal Error – yii\base\ErrorException

Class ‘app\controllers\GridConnector’ not found[/code]

Please check docs.dhtmlx.com/tutorials__conne … index.html

You can install latest version of dhtmlx connector by using composer

composer require dhtmlx/connector-php

I’m stucked with new problem in the connector:

how to filter the data returned with the connector to grid.

I created the following function:

[code]public function actionData() {
$data = FirmyZawieszenia::find()
->where([‘id_firmy’ => $_GET[‘id_firmy’]])
->all();

    $connector = new GridConnector(null, "PHPYii");
    $connector->configure(
        $data,
        "id", "data_zaw, data_odw"
    );
    $connector->render();
}[/code]

All works fine until the returned dataset is empty. Then I get the following error:
Source of data can’t be empty

By the way,

functions

$connnector->filter(...)

and

$connector->sort(...)

seems not to be working :frowning:

When you are providing the result of search ( $data = …:find() ) connector will not apply any post-filtering to the dataset.
The ->filter and ->sort API are designed for the case, when connector reads data directly from DB.
When you are reading data from the model’s method, you can place the logic of filtering in the model ( in the find call in your case )

Stanisłav,

maybe I am doing something wrong - please help me to solve the issue.

I created simple test DB table named test it looks as follows:

±—±-------------±-----------+
| id | text | foreign_id |
±—±-------------±-----------+
| 1 | Text value 1 | 1 |
| 2 | Text value 2 | 2 |
| 3 | Text value 3 | 4 |
| 4 | Text value 4 | 3 |
±—±-------------±-----------+

I created model using gii - there is nothing special in it - just default model settings.

Then I made TestController as follows:

[code]namespace app\controllers;

use app\models\Test;
use Dhtmlx\Connector\GridConnector;

class TestController extends \yii\web\Controller
{
public function actionGrid()
{
$connector = new GridConnector(null, “PHPYii”);
$connector->configure(
new Test(),
“id”, “id,text,foreign_id”
);
$connector->render();
}

public function actionGridsorted()
{
    $connector = new GridConnector(null, "PHPYii");
    $connector->configure(
        new Test(),
        "id", "id,text,foreign_id"
    );
    $connector->sort('foreign_id', 'desc');
    $connector->render();
}

public function actionGridfiltered() {
    $connector = new GridConnector(null, "PHPYii");
    $connector->configure(
        new Test(),
        "id", "id,text,foreign_id"
    );
    $connector->filter('foreign_id',4);
    $connector->render();
}

}[/code]

Unfortuantely the output of all the functions is exactly the same:

[code]

[/code]

What am I doing wrong?

The connector will apply the fitler and sort only when you are using raw DB connector

$connector = new GridConnector($pdo, "PDO"); $connector->filter('foreign_id',4); $connector->render_table("test", "id", "id,text,foreign_id" );

If you are providing a model, the connector will call find method of the model and output the result, you need to include all filtering in a model.
Why not use the next for example

$connector = new GridConnector(null, "PHPYii"); $data = Test::find() ->where(['foreign_id' => 4) ->all(); $connector->configure( $data, "id", "id,text,foreign_id" ); $connector->render();

So we’re getting back to my first post in this thread,

if I use approach suggested by you I get the error that data source cannot be empty :frowning: when the result of the query is empty…

[code]public function actionData() {
$data = FirmyZawieszenia::find()
->where([‘id_firmy’ => $_GET[‘id_firmy’]])
->all();

    $connector = new GridConnector(null, "PHPYii");
    $connector->configure(
        $data,
        "id", "data_zaw, data_odw"
    );
    $connector->render();
}[/code]

Please update dhtmlx/connector-php to version 3.0.4, it will fix the issue with empty datasets.

Can you please share the link to YII version? Standard connector does not support yii namespaces… and in the demo application I can see still the old one.

Or, if I am doing it all wrong, the manual to use standard version in YII app.

Can you please share the link to YII version?

The above code was taken from latest YII ( “yiisoft/yii2”: “>=2.0.5”, )
Default version of connector is using the classic PHP approach and will not work with namespaces and autoloading
If you will use the “modern” branch of connecto - it will support both features.

This version can be installed by composer ( composer require dhtmlx/connector-php ) or from demo-app in documentation

docs.dhtmlx.com/tutorials__conne … step1.html
dhtmlx.com/x/download/regula … dhtmlx.zip

I downloaded the ‘classic’ one and:

  1. Is the syntax different for that?
  2. I am facing the issue when done the following:

I added:

require_once (’…/vendor/dhtmlxconnector/grid_connector.php’);
require_once (’…/vendor/dhtmlxconnector/db_phpyii.php’);

as in classic PHP approach.

I have this function:

public function actionData() { $connector = new \GridConnector(null, "PHPYii"); $connector->configure( new Zdarzenia(), "id", "imie_nazwisko_pracownika,opis,data,dane_podmiotu,adr_ip" ); $connector->render(); }

and I have this lovely error:

trim() expects parameter 1 to be string, object given

in /var/www/htdocs/gekkon/vendor/dhtmlxconnector/db_common.php


    public function set_fieldset($value){
        $this->fieldset=$value;
    }
    /*! sets name of source table
 
        @param value 
            name of source table
    */
    public function set_source($value){
        $this->source=trim($value);   <----------------------------------------------------- This line is highlighted
        if (!$this->source) throw new Exception("Source of data can't be empty");
    }
    /*! sets data limits
 
        @param start
            start index
        @param count
            requested count of data
    */

Anything? Anyone?

Grab latest codebase from github
github.com/dhtmlx/connector-php

It must solve the above issue.

Thank you. It works!