Custom table in MODx Revolution

imageIn this article we will examine the following questions:
    the
  1. Creating custom tables for MODx Revolution.
  2. the
  3. Generation of XML schema and php files classes for xPDO.
  4. the
  5. Work with custom tables.


One of the biggest difficulties in migrating from Evolution to Revolution — an xPDO IMHO. After all, as was previously just got into the muscle via phpMyAdmin, created a sign, and work with it, sending pure SQL queries through $modx->db.
Now any changes to the database do not much bother $modx except full drop tables, etc. As linked xPDO and MODx (or rather $modx), I wrote here.
Consider the difficulties in working with custom tables in MODx Revolution — a considerable obstacle for many programmers.
But really, it's not so difficult.
All of the following, the algorithm in three words:
    the
  1. via phpMyAdmin (or other convenient tool to use) to create the needed tables
  2. the
  3. Using the attached code is generated for the necessary work xPDO files
  4. the
  5. Using the $modx- > addPackage() loadable in the right place our new class and are already working with our tables via the methods of $modx- > getObject(), $modx->getCollection (), etc.


So, we need to generate code files (Source here):

<?php



/*******************************************************/

/* Configs in the chart */

/*******************************************************/


// the Class Name. So it will then be referred to the Class when calling $modx- > addPackage()
<br>
$obj = 'program';


/*
table Prefix. If the prefix is not different from the system, it is possible not to specify.
sorry, xPDO when generating does not allow to list the names of specific tables,
that we need, and allows you to filter out only the prefix.
*/
<br>
$tablePrefix='modx_program_';


// Folder where will be written the XML schema and all files of object to create

// path to the class files then you will register the call to $modx- > addPackage();
<br>
$Path = dirname(__FILE__).'/model/';


// the File schema
<br>
$Schema = $Path.'/'.$obj.'.mysql.schema.xml';


/*******************************************************/


<br>
// Loaded main file-the config of the site or by the need to register all basic settings
<br>
require_once dirname(dirname(dirname(__FILE__))).'/core/config/config.inc.php';


// Podrugam main class MODx
<br>
include_once MODX_CORE_PATH . 'model/modx/modx.class.php';


// Initialize the class MODx
<br>
$modx= new modX();


// Initialize the context if fundamentally
<br>
// $modx- > initialize('mgr');


// Set the logging settings

// Not necessary
<br>
$modx- > setLogLevel(modX::LOG_LEVEL_INFO);
<br>
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');


// !!! Be sure!
<br>
// Loaded main class-packer
<br>
$modx- > addPackage('transport.modPackageBuilder' " false true);

// Pointer type of database (MySQL / MsSQL etc.)
<br>
$manager = $modx->getManager();


// Class generator schemes
<br>
$generator = $manager->getGenerator();



// Generated file-XML
<br>
// /xpdo/om/mysql/xpdogenerator.class.php

<br>
// public function writeSchema($schemaFile, $package= ", $baseClass= ", $tablePrefix= ", $restrictPrefix= false)
<br>
// $tablePrefix - specify if want only the tables that begin with this prefix.

<br>
// $restrictPrefix - indicates true if you want to get only the table prefix

<br>
$xml= $generator- > writeSchema($Schema, $obj, 'xPDOObject', $tablePrefix ,$restrictPrefix=true );


// Generates classes and maps (php) xml schema
<br>
$generator- > parseSchema($Schema, $Path);

<br>
<br>
print "<br /><br />Done";




Upload this file to the server, write it the correct path to the configuration MODx-a, and the directory where will be created the files generated. Accesses the file through the browser. As a result, in the specified folder needs to be created. xml file which will outline our table and a php file.
These files, if you really must, you can move to another folder, where you choose to store your new Class. In the future, these files will always need to work with your tables.

Now you can work with your tables. In order to better understand the mechanism of interaction with xPDO XML schema, as well as to understand what is written in the file-generator, and which objects to get the output, here is my post on this issue. Honestly, at first without it was just no...

the

1. Connection packages and perform queries to the schema



the

1.1 the Selection of records from a table


The first thing to do is to understand what do you need in order to apply to the scheme. For this study the XML file.
That's the basic creating block of our schema:

<model package="testtbl" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="Tbl" table="tbl" extends="xPDOObject">



Most importantly, that we then need is:
1.
<model package="testtbl"
the value of the attribute package we need to know what package we all need to load. In this case, testtbl. Call package will:

$modx- > addPackage( $package, $path, $prefix);
// !!! Check in the function addPackage not the channel, so as to utmp the name of the package in principle and to the path specified was a directory, and all.


Consider carefully the options.
the $package. The name of the package. In our case, just testtbl
the $path. The path to the package directory (which has the XML file and the folder name of the Object in which all the PHP files)
the $prefix. If you create a schema was specified a prefix different from well done, it is necessary to specify it.

There is another little trick of how to accurately determine the required prefix.
Formula: $prefix = $fullTableName — $tableName;
There are $fullTableName is the full name of the database table to which you are applying,
the $tableName — the value of attribute table <object of our XML file

Executed query:

$result = $modx- > getCollection('Tbl');
foreach($result as $row){
print “<br />Next:”. $row->get(‘columnName’);
}


the

1.2 creating a new entry in the table


private static testCreateRows function(){

$pkg = 'testtbl';


$modx- > addPackage( 'testtbl' $Path 'modx_kl_test')) ;

$row = $modx- > newObject('Tbl');
$row->fromArray(array(
'id' => 5
));
$row->save();

return ;
}



It seems to be. If something is not clear, ask.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Monitoring PostgreSQL + php-fpm + nginx + disk using Zabbix

Templates ESKD and GOST 7.32 for Lyx 1.6.x

Customize your Google