We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Dynamic/Generic Model when adding db table

Hi

I need to build a system that enable user to add tables and columns to database and I want to manipulate these table/attributes with Phalcon model.

I don't want to add new model file each time the user add a table I prefer a solution with one generic model for the added tables (the default tables have their predefined models).

How I can perform this feature (I want to use the last Phalcon version >=3)?

Thx in advance



85.5k
edited Sep '16

probably PHQL is the right choice.

Never the less... I guess you can create a class that creates classes ( classception ) :D and it should work. In order to change the source of the model i think i was using onConstruct which was caucing issues on its own.

But I guess when person creates a table you could generate the model and save it in user's name namespace and directory so the autoloader would work and you wont be having isssues ( ithink )



3.3k

Hi Izo thanks for your response.

I don't want to generate model file each time table is created, I want a solution with 2 generic models :

  • CustomTable : model of the added table
  • CustomCollumn : model of each collumn

When I need to use a table model (in forms for exemple) I want to instanciate the CustomTable model that set the source to the right value and perform a function getCollumns that give me all table collumns. The attributes of the CustomTable model will be only the table name and the display name and attributes of CustomCollumn will be name, displayName, type, fieldOptions.

I don't know if the collumnMap match to what I want to do. If it do the model will be look like this:

<?php

class CustomTable extends Phalcon\Mvc\Model
{
public $tableName;
public $displayName;

public function initialize()
{
    $this->setSource($this->$tableName);
}

public function columnMap()
{
   return CustomCollumn::getCollumns(); //function that build an array of collumns
}

}


85.5k

i dont think making this work will worth the while. I suggest you using standart queries. Maybe create a helper class that does the it like phalcon orm ?