Build editor instance dynamically
Build editor instance dynamically
Hi,
I purchased a single-seat Editor license. I have a problem with server script.
staff.php - server script
Editor::inst( $db, 'datatables_demo' )
->fields(
Field::inst( 'first_name' )->validator( 'Validate::notEmpty' ),
Field::inst( 'last_name' )->validator( 'Validate::notEmpty' ),
Field::inst( 'position' ),
Field::inst( 'email' ),
Field::inst( 'office' ),
Field::inst( 'extn' )->validator( 'Validate::numeric' ),
Field::inst( 'age' )->validator( 'Validate::numeric' ),
Field::inst( 'salary' )->validator( 'Validate::numeric' ),
Field::inst( 'start_date' )
->validator( 'Validate::dateFormat', array(
"format" => Format::DATE_ISO_8601,
"message" => "Please enter a date in the format yyyy-mm-dd"
) )
->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
)
->process( $_POST )
->json();
I want to build editor instance dynamically . How can i do it ?
Ex: if age field is numeric in mysql database, automatically determine the field. Is there any thing like this ?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi,
If you want to use the database as the model for your data, you would need to query the database to get information about each column (
SHOW
for example) and then build the Editor instance based on that. It should be quite possible, but I haven't seen that being used as it will be more efficient, generally speaking, to have the model in PHP. The only case I can think of where it would be more efficient to do it in SQL would be if the database column types change very often (daily). At that point, you might want to store the table definition in its own table!In general the process of building an Editor PHP class dynamically is quite simply, you would just call
field()
as often as you need to add the columns. In the above code you have the code chained, but you could readily put it into a loop and add a field for each item in an array you are looping over.Regards,
Allan
Thank you for your answer Allan.
i want to make an application like phpmyadmin so i need the database as the model of data. (sorry my english is not good. i hope you can understand)
i change the code like below
but i have an error message at js consol
Uncaught Unable to automatically determine field from source. Please specify the field name - dataTables.editor.js:4521
Regards,
Çağrı
What does staff.php - server script - look like?
```php
<?
include( "../../php/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
Editor::inst( $db, 'datatables_demo' )
<?php > ``` ?>->fields(
Field::inst( 'first_name' )->validator( 'Validate::notEmpty' ),
Field::inst( 'last_name' )->validator( 'Validate::notEmpty' ),
Field::inst( 'position' ),
Field::inst( 'email' ),
Field::inst( 'office' ),
Field::inst( 'extn' )->validator( 'Validate::numeric' ),
Field::inst( 'age' )->validator( 'Validate::numeric' ),
Field::inst( 'salary' )->validator( 'Validate::numeric' ),
Field::inst( 'start_date' )
->validator( 'Validate::dateFormat', array(
"format" => Format::DATE_ISO_8601,
"message" => "Please enter a date in the format yyyy-mm-dd"
) )
->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
)
->process( $_POST )
->json();
My apologies - I can see you posted it before.
Did you make any changes to DataTables.php?
no i didn't.
the only change is at Editor-1.3.1/php/Database/Driver/Mysql/Query.php
i inserted
$pdo->exec("SET CHARACTER SET utf8");
to line 52 to fix charset problem
Have a look at the discussion in this thread: http://datatables.net/forums/discussion/21143 .
I'll write up a tech note about this error soon.
Allan