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

Flexible schemas

Hey,

I'm implementing an application which allows the user to individually create a table layout for a specific purpose. All tables contain the same schema to some extend (id, updated, created, deleted, ...) but are extensible to some degree by the users input (add column, define column type, ...). All tables will be named using the following convention "table_$id". I would like to use one model which retrieves the current id from my input (new Model()->setId($id)) and sets the table name accordingly.

How can I tell the model to retrieve the remaining table schema data from the live db while using some default values using the annotations schema? Or would you recommend an entirely different way to do this? :)

Thanks in advance for your feedback, Philipp

Example code:

<?php

class ModelWithChangingID extends Model
{

    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false)
     */
    public $id;
    /**
     * @Column(type="integer", nullable=false)
     */
    public $entity_id;
    /**
     * @Column(type="datetime", nullable=false)
     */
    public $created;
    /**
     * @Column(type="datetime", nullable=true)
     */
    public $updated;
    /**
     * @Column(type="integer", nullable=true)
     */
    public $deleted;

    protected $_id;

    public function getSource()
    {
        if (empty($this->_id))
        {
            throw new \Exception('Table id can not be empty');
        }

        return 'table_' . $this->getId();
    }

    public function getId()
    {
        return $this->_id;
    }

    public function setId($id)
    {
        $this->_id = $id;
    }

}

I'm actually writing a ParanoidBehavior Plugin allow you to adding created_at, deleted_at, modified_at, modify-userid, delete-userid, create-user__id fields to your models on the fly, by doing it both model creation, scaffolding, or simply running an update on a model, and doing model creating and migration writing in the background, and the model layering tech is changed too, as the simple layer of models is simply not enough, which means a base class will always created for models which are sitting under the models/base dir naming base{$className} and the models will creating under the models dir extending the base classes, and the scaffold will always regenerate only the base classes, and not touching your code implemented into the models