We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36474
    • 108 Posts
    TUTO utiliser une table personnalisée avec xpdo
    Ce tuto est une traduction tiré du guide présent sur le site : http://bobsguides.com/custom-db-tables.html
    Si l'auteur du guide voit un inconvénient à ce tuto, je demanderais aux administrateurs du forum de bien vouloir supprimer ce sujet.

    SOMMAIRE

    1 - Création de la table

    2 - Création du snippet CreateXpdoClasses

    3 - Création des fichiers

    4 - Test de votre classe


    Ce tuto consiste à :

    • - Créer une table citation. Qui contiendra des citations, leur auteur et leur sujet.
    • - Créer un snippets CreateXpdoClasses qui créera les fichiers nécessaire à l'utilisation de Xpdo dans notre arborescence.
    • - Créer une page dans laquelle on appellera ce snippets
    • - Et pour finir, on testera le tout en affichant les citations présente dans notre table.

    Bref. Rien d'extra-ordinaire. Mais beaucoup d'entre nous se désespère est n'arrive à le faire.

    1 - Création de la table

    Lancer PhpMyAdmin et ouvrez votre base de données MODx et cliquez sur l'onglet "SQL". Coller le code suivant et cliquez sur le bouton "Go".

    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    CREATE TABLE IF NOT EXISTS `bobs_quotation` (
    `id` int(25) unsigned NOT NULL auto_increment,
    `quote` mediumtext NOT NULL,
    `author` varchar(200) NOT NULL default '',
    `topic` varchar(20) NOT NULL default '',
    PRIMARY KEY (`id`),
    KEY `topic` (`topic`),
    KEY `author` (`author`),
    FULLTEXT KEY `quote` (`quote`,`author`,`topic`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
    

    Nous avons également besoin de quelques citations dans la table afin de tester nos classes. Toujours dans phpMyAdmin, sélectionner la table bobs_quotation et cliquez sur l'onglet "Insertion" en haut. Ajoutez quelques citations avec un topic et un author en remplissant les champs Valeur (laissez le champ id vide et ne bidouillez pas les autres champs) et cliquez sur le bouton "Go" au fond. Ajoutez en plus si vous le souhaitez. Si vous cliquez sur l'onglet «Parcourir», vous devriez voir vos citations dans la table.


    2 - Création du snippet CreateXpdoClasses

    Coller le code ci-dessous dans un nouveau snippet que vous appellerez CreateXpdoClasses
    Voici le code. Vous n'avez pas besoin de le modifier pour l'utiliser dans ce tutoriel.

    <?php
    /**
    * @package = CreateXpdoClasses
    *
    * Create Xpdo Classes script
    *
    * This script creates xPDO-ready classes from existing custom
    * database tables. It only needs to be run once.
    *
    * It assumes that your custom tables have been imported into
    * the MODx DB and use a different table prefix than the MODx tables.
    
    * In theory, you can use a separate DB but this has not been tested
    * and the process of using the classes would be more complicated.
    * To do this, you would need to set the following variables after
    * including the config file, but before creating the $modx object:
    * 
    * $database_server = 'yourDbServer';
    * $database_type = 'mysql';
    * $dbase = 'yourDbName';
    * $database_user = 'user';
    * $database_password = 'password';
    * $table_prefix = 'yourPrefix_';
    *
    * In the snippets that use the classes, you'd need to instantiate
    * a new $xpdo object, and use $xpdo-> rather than $modx-> to call
    * the methods.
    * $xpdo = new xPDO('mysql:host=localhost;dbname=yourDbName',
    *   $database_user,$database_password,$table_prefix);
    *
    * Note: If you are running this outside of MODx and testing it
    * in the Manager, it will log you out when it runs, even though
    * the Manager screen will still be visible. Actions taken in the
    * Manager (e.g., saving a snippet) will not be successful. After
    * running the script, reload the Manager page in your browser
    * and you will be prompted to log back in.
    *
    *
    */
    /* assume we're in a snippet */
    $outsideModx = false;
    
     if (!defined('MODX_CORE_PATH')) {
         $outsideModx = true;
        /* put the path to your core in the next line to run outside of MODx */
        define(MODX_CORE_PATH, 'c:/xampp/htdocs/test/core/');
        include_once MODX_CORE_PATH . '/model/modx/modx.class.php';
        $modx= new modX();
        $modx->initialize('mgr');
    }
    
    /* set these if running outside of MODx */
    
    if ($outsideModx) {
        $myPackage = 'mypackage';
    
        /* table prefix; must match the prefix of the tables to process */
        $myPrefix  = 'bobs_';
    
        /* optional -- only if your table prefix is the same as the modx prefix */
        // $myTables = 'bobs_quotation';
    }
    
    
    
    /* These two switches let you write the schema and/or create the classes;
     * useful for debugging and for leaving a manually edited schema file alone*/
    
    $createSchema = $modx->getOption('createSchema',$scriptProperties,true); 
    $createClasses = $modx->getOption('createClasses',$scriptProperties,true);
    
    /* Used to include the phpDoc templates below or other custom templates */
    $includeCustomTemplates = empty($includeCustomTemplates)? 
        false : $includeCustomTemplates;
    
    /* $myPackage is the name of your package. Use this in your
     * addPackage() call to load all classes created by this script.
     *  The class files will be created under the
     * /core/components/$myPackage/model/ 
     * directory.
     * Example:
     *     $myPackage = 'quotes';
     *     $myPrefix = 'bobs_';
     *     $path = MODX_CORE_PATH . 'components/' . $myPackage . '/';
     *     $result = $modx->addPackage($myPackage, $path . 'model/', $myPrefix);
     *     if (! $result) {
     *         return('Failed to add package');
     *     }
     */
    $myPackage = empty($myPackage)? 'mypackage' : $myPackage;
    
    /* table prefix; must match the prefix of the tables you want to process */
    $myPrefix = empty ($myPrefix)? '' : $myPrefix;
    
    /* Table names to process -- this is only necessary if your table prefix is
     * the same as that of the MODx tables. You can send a comma-separated list
     * of full table names. In that case the class name will be the table name 
     * minus the prefix with any underscores removed and any letter after an 
     * underscore in upper case.
     *
     * You can also send an array of arrays of tableName => className, 
     * which allows you to specify the exact class name rather then letting
     * MODx create it from the table name. Each inner array specifies a full
     * table name and the class name to use.
     *
     * NOTE: This feature may not be implemented yet.
     *
     * Examples:
    
     $myTables = 'bobs_quotation';
    
     $myTables = array(
         array(
            'bobs_quotation'=>'bobQuotation'
         )
     );
    
    */
    $myTables = empty($myTables)? '' : $myTables;
    
    /* You shouldn't need to modify the code beyond this point
    ********************************************************** */
    
    $sources = array(
        'config' => MODX_CORE_PATH . 'config/config.inc.php',
        'package' => MODX_CORE_PATH . 'components/' . $myPackage . '/',
        'model' => MODX_CORE_PATH. 'components/' . $myPackage . '/model/',
        'schema' => MODX_CORE_PATH . 'components/' . $myPackage . '/schema/',
        );
    
    if (! file_exists($sources['package'])) {
        mkdir($sources['package'],0777);
    }
    
    if (! file_exists($sources['model'])) {
        mkdir($sources['model'],0777);
    }
    if (! file_exists($sources['schema'])) {
        mkdir($sources['schema'],0777);
    }
    
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);
    $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
    
    $manager = $modx->getManager();
    $generator = $manager->getGenerator();
    
    if ($includeCustomTemplates) {
        customTemplates($generator);
    }
    
    
    $file = $sources['schema'] . $myPackage . '.mysql.schema.xml';
    
    // echo '<br />File: ' . $file;
    
    /* boolean writeSchema (
     *   string $schemaFile,  // Full path to the schema file you want to write
     *   [string $package = ''], //  Name of your component
     *   [string $baseClass = ''], // xPDO base class to use; 
     *                                 send '' if using args below
     *   [string $tablePrefix = ''], // Table prefix (of tables to process)
     *   [boolean $restrictPrefix = false]), // Process only tables 
     *                                          with $tablePrefix
     *   [mixed $tableList = '' // Array of arrays of 
     *                             full-table-name=>className or 
     *                             a string with a comma-separated 
     *                             list of full table names; 
     *                             if you send the string the table
     *                             name will be used as the class name.
    */
    
    if ($createSchema) {
    
        $xml= $generator->writeSchema($file,
                    $myPackage, '',$myPrefix,true,$myTables);
    
        if ($xml) {
           $modx->log(modX::LOG_LEVEL_INFO, 
               'Schema file written to ' . $file);
        } else {
            $modx->log(modX::LOG_LEVEL_INFO, 
               'Error writing schema file');
        }
    }
    
    if ($createClasses) {
         if ($generator->parseSchema($file, $sources['model'])) {
             $modx->log(modX::LOG_LEVEL_INFO,
                'Schema file parsed -- Files written to '. $sources['model']);
         } else {
             $modx->log(modX::LOG_LEVEL_INFO, 'Error parsing schema file');
         }
    }
     $modx->log(modX::LOG_LEVEL_INFO, 'FINISHED');
     exit();
    
     function customTemplates($generator) {
    $generator->classTemplate= <<<EOD
    <?php
    /**
     * [+phpdoc-package+]
     * [+phpdoc-subpackage+]
     */
    class [[+class]] extends [[+extends]] {
    }
    ?>
    EOD;
    $generator->platformTemplate= <<<EOD
    <?php
    /**
     * [+phpdoc-package+]
     * [+phpdoc-subpackage+]
     */
    require_once (dirname(dirname(__FILE__)) . '/[+class-lowercase+].class.php');
    class [+class+]_[+platform+] extends [+class+] {
    }
    ?>
    EOD;
    $generator->mapHeader = <<<EOD
    <?php
    /**
     * [+phpdoc-package+]
     * [+phpdoc-subpackage+]
     */
    EOD;
    }
    


    3 - Création des fichiers

    Créer une nouvelle ressource (une page) appelée "CreateClasses" et mettre la balise suivante dans le champ Contenu :

    [[!CreateXpdoClasses? &myPackage=`quotes` &myPrefix=`bobs_`]]
    

    Lorsque vous prévisualisez votre ressource CreateClasses, vous devriez voir des messages vous disant que les fichiers de schéma et de la classe sont créés. Vous avez seulement besoin de faire cela une fois. Si vous souhaitez voir les fichiers, ils seront dans core/components/quotes.

    4 - Test de votre classe

    Créer un nouveau snippet intitulé «ShowQuotes» avec le code suivant :

    <?php
    $path = MODX_CORE_PATH . 'components/quotes/';
    $result = $modx->addPackage('quotes',$path . 'model/','bobs_');
    if (! $result) {
      return 'failed to add package';
    } else {
      $output = '<p>added Package</p>';
    }
    
    $quotes = $modx->getCollection('Quotation'); 
    
    $output .= '<p>Total: '. count($quotes) . '</p>';
    
    foreach($quotes as $quote) {
    
        $output .= '<p>Topic: ' . $quote->get('topic');
        $output .= '<br />Quote: ' . $quote->get('quote');
        $output .= '<br />Author: ' . $quote->get('author') . '<br /></p>';
    }
    
    return $output;
    


    Maintenant, créez une nouvelle ressource appelée "ShowQuotes" et mettre la balise suivante dans le contenu :

    [[ShowQuotes]]
    


    Lorsque vous prévisualisez votre ressource ShowQuotes, vous devriez voir vos citations. Si cela fonctionne, toutes les autres méthodes Xpdo fonctionneront sur votre table.

    J'ajouterais bientôt un fichier avec la traduction quasi complète du guide présent sur le site : http://bobsguides.com/custom-db-tables.html
    [ed. note: dexeryl last edited this post 12 years, 4 months ago.]
    • Salut dexeryl,

      Merci pour cette traduction, je suis sûr que cela pourra intéresser les non anglophones smiley

      J'en profite au passage pour rappeler que l'auteur initial (Bob Ray) est également l'auteur du guide officiel de MODX.
        • 17422
        • 225 Posts
        Merci dexeryl,

        et crois tu que tu pourrais faire la même chose pour apprendre à administrer cette table via le manager ? smiley