Need help restructuring this code
Need help restructuring this code
I have about 20 tables that mainly use 90% of the same columns... I currently have it so I pull in all the fields need across all the tables and the java side takes care of hiding/showing the needed columns. This worked for a while but it is becoming a little too complex and I am trying to turn my PHP code into "reusable" code. Another reason for this is that I added a Options::inst()
to one a field but that field will change over depending on table is loaded and I just don't see a way to make it work with out making my java file even longer and more complex than it currently is. To my main point... I need help with code I have just created. It works but I know there is a better way and I just cannot figure it out.
I have 4 main files:
Table.php (should call the 3 files below at some point.
Columns.php (defaults columns, add field, etc)
Functions.php (log functions)
Events.php (preCreate, etc events to set values, etc and calls the functions in Functions.php for logging changes)
namespace DataEntry;
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
Carbon\Carbon,
DataEntry\Table\Columns,
DataEntry\Table\Events;
class Table
{
private $table;
private $databaseInfo;
public function __construct($db)
{
$this->databaseInfo = $db;
$this->_define();
Columns::defaults($this->table);
}
public function addColumn($field){
$this->table->fields($field);
}
public function where($key, $value=null, $op="=")
{
$this->table->where($key, $value, $op);
}
public function toJson($parameters)
{
$this->
return $this->table
->process( $parameters )
->json();
}
private function _define()
{
$this->table = Editor::inst( $this->databaseInfo, 'charges', 'id' );
}
}
namespace DataEntry\Table;
use DataTables\Editor\Field;
class Columns {
public static function defaults($table){
$table->fields(
.........
);
}
public function addField($name){
$table->fields(
Field::inst($name);
);
}
}
I have another file that creates a new Table object and calls the functions...
$table = new Table($db);
$table->where();
$table->addColumn(
Field::inst('')
->options( Options::inst()
->table( '' )
->value( '' )
->label( '' )
->where( function ($q) {
........
})
)
);
$table->toJson($_POST);
I mainly want to know how I can access the $table variable inside the Columns file. I am currently not doing that and I would prefer to say something like
$table->column()->addField()