Automate table table creation from inside CakePHP

I am trying to write a webapp with CakePHP, and, like most webapps, I would like to create an installer that determines if the database has been initialized and, if not, performs the installation process.

This process will be fully automated (it is assumed that the database itself already exists and that it is granted full administrative access through an anonymous account without a password ... this is for the sandbox environment, so do not worry about security), therefore it should be able to detect (independently from the query!) if the database tables were created and initialized, and if not, to perform this initialization transparently, and then continue to fulfill the original user request.

I thought about writing my type of Bootstrap controller through which all queries are routed, and a single SQL query is executed to determine if the database tables exist, but this seemed cumbersome (and the controller requires an appropriate model that does not need in this case ) Another possibility was to override the AppModel and put the same test in it, but I did not know how to do this, since there is no documentation in these lines.

Thanks in advance!

tl; dr version : What is the equivalent of CakePHP (or how can I write the equivalent for CakePHP) for the J2EE servlet "init ()" method?

+3
source share
4 answers

AppError - , , SQL ?

+3

, , ? .

, , , , , , !

, - . script, , .


CakePHP, Cake ?

http://book.cakephp.org/view/734/Schema-management-and-migrations

+2

- , , .

<?php
class AppModel extends Model {
    var $create_query = false;

    function __construct($id = false, $table = null, $ds = null) {
        parent::__construct($id, $table, $ds);

        if (!empty($this->create_query)) {
            $this->query($this->create_query);
        }
    }
}
?>

var $create_query = 'CREATE TABLE IF NOT EXISTS ...; ` . MySQL CREATE TABLE IF NOT EXISTS.

, , () . , , . CakePHP . , , cake/libs/model/model.php.

: , .

Model , Model:: __ Model:: setSource(). Model:: setSource() , , . , . , , CakePHP .

<?php
function setSource($tableName) {
    // From Model::setSource().  I believe this is needed to make query() work.
    $this->setDataSource($this->useDbConfig);

    // Create our table if we have a create query
    if (!empty($this->create_query)) {
        $this->query($this->create_query);
    }

    // Now call the parent
    parent::setSource($tableName);
}
?>
+2

.

-, : , app/config/database.php, . , , .

PHP ( ) . script , THEN -, . , -, AppError (, SetupWizardController) CakePHP. , database.php/setup - , AppError AppController ( , , AppController -, ), -, //myapp. script , .

, , AppError .

- . , app/config/environment.php. ( ) . , , - , database.php environment.php. database.php , environment.php. $Default database.php NoDB Datasource, AppController ( , ), . , AppController.

+1
source

Source: https://habr.com/ru/post/1721560/


All Articles