We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27117
    • 28 Posts
    I try to create a form and store the data in a Database. Then I would like to write this data into a document's content and description field.

    In my form, I have a date field and some text fields. In my resource I have a TV called "showDate". Based on the TV "showDate" I would like to read the data from the Database.

    I worked the whole day on this and followed along Bob's Guide "Custom DB Tables" but then I discovered "formit2db" and I'am not quite shure, which way a should go.

    What I did so far is creating the custom Database and I put in manually some content. And I created the Xpdo classes as it is written in Bob's Guide. But when I try to test my classes, I get a "added Package" but "Total count = 0" Message.

    Maybe anyone can point me into the right direction.

    This is my code to read the data from the DB:
    <?php
    $path = MODX_CORE_PATH . 'components/headlines/';
    $result = $modx->addPackage('headlines',$path . 'model/','tb_');
    if (! $result) {
      return 'failed to add package';
    } else {
      $output = '<p>added Package</p>';
    }
    
    $headlines = $modx->getCollection('headlines'); 
    
    $output .= '<p>Total: '. count($headlines) . '</p>';
    
    foreach($headlines as $quote) {
    
        $output .= '<p>Topic: ' . $quote->get('topic');
        $output .= '
    Quote: ' . $quote->get('quote');
        $output .= '
    Author: ' . $quote->get('author') . '
    </p>';
    }
    
    return $output;



    Thanks. [ed. note: shotup last edited this post 14 years, 9 months ago.]
      Modx 2.1.3, PHP 5.2
      • 27117
      • 28 Posts
      I found the error in the code. Instead of:
      $headlines = $modx->getCollection('headlines'); 


      I need to write "Headlines" with a capital H:
      $headlines = $modx->getCollection('Headlines'); 
      [ed. note: shotup last edited this post 14 years, 9 months ago.]
        Modx 2.1.3, PHP 5.2
        • 27117
        • 28 Posts
        I succeeded in writing my formit data into the db (still following Bob's Guide).

        But when I try to do the same thing with my own formit/db data, nothing is written to the db.

        I have read a lot of forum posts and I studied Bob's Guide over and over again but I can't get it to work.

        For now, I am completely lost. I don't know, if I have made an error while creating my db table or maybe my formit hook script is wrong.

        Maybe someone can help me?

        Btw, I am on Revo 2.1.3.

        Any help is very much appreciated!
          Modx 2.1.3, PHP 5.2
          • 3749
          • 24,544 Posts
          Did you remember to load the class (e.g., addPackage() )? Did you check the return value of addPackage() to see if it's true? Did you remember to call save()?

          Can you post the code that writes your data to the DB?



          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/MODx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 27117
            • 28 Posts
            Hi Bob

            Thanks for your help. So far I think I have loaded the class, checked the return value of addPackage and save call is also there.

            My Package is called "headlines", my db table prefix is "tb_" and the class name is also "headlines".

            This is my custom write2db hook script:

            <?php
            $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/headlines/';
            $modx->addPackage('headlines',$base_path.'model/','tb_');
            $headlines_form = $modx->newObject('Headlines');
            $headlines_form->fromArray($scriptProperties['fields']);
            $headlines_form->save();
            return true;


            I am not quite shure, if my db table is right. I followed your example but I was lost where to set the fulltext key so I choosed none. Maybe the error lies there?

            This is what my headlines.map.inc.php looks like:

            <?php
            $xpdo_meta_map['Headlines']= array (
              'package' => 'headlines',
              'version' => '1.1',
              'table' => '_headlines',
              'fields' => 
              array (
                'date' => NULL,
                'mod' => '',
                'news1' => '',
                'news2' => '',
                'news3' => '',
                'news4' => '',
                'news5' => '',
                'info1' => '',
                'info2' => '',
                'info3' => '',
                'sport1' => '',
                'sport2' => '',
                'sport3' => '',
              ),
              'fieldMeta' => 
              array (
                'date' => 
                array (
                  'dbtype' => 'int',
                  'precision' => '20',
                  'phptype' => 'integer',
                  'null' => false,
                  'index' => 'index',
                ),
                'mod' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'news1' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'news2' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'news3' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'news4' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'news5' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'info1' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'info2' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'info3' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'sport1' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'sport2' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
                'sport3' => 
                array (
                  'dbtype' => 'varchar',
                  'precision' => '200',
                  'phptype' => 'string',
                  'null' => false,
                  'default' => '',
                  'index' => 'index',
                ),
              ),
              'indexes' => 
              array (
                'mod' => 
                array (
                  'alias' => 'mod',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'mod' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'news1' => 
                array (
                  'alias' => 'news1',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'news1' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'news2' => 
                array (
                  'alias' => 'news2',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'news2' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'news3' => 
                array (
                  'alias' => 'news3',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'news3' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'news4' => 
                array (
                  'alias' => 'news4',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'news4' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'news5' => 
                array (
                  'alias' => 'news5',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'news5' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'info1' => 
                array (
                  'alias' => 'info1',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'info1' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'info2' => 
                array (
                  'alias' => 'info2',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'info2' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'info3' => 
                array (
                  'alias' => 'info3',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'info3' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'sport1' => 
                array (
                  'alias' => 'sport1',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'sport1' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'sport2' => 
                array (
                  'alias' => 'sport2',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'sport2' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'sport3' => 
                array (
                  'alias' => 'sport3',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'sport3' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
                'date' => 
                array (
                  'alias' => 'date',
                  'primary' => false,
                  'unique' => false,
                  'type' => 'BTREE',
                  'columns' => 
                  array (
                    'date' => 
                    array (
                      'length' => '',
                      'collation' => 'A',
                      'null' => false,
                    ),
                  ),
                ),
              ),
            );
            


              Modx 2.1.3, PHP 5.2
              • 27117
              • 28 Posts
              Well, I found the error.

              In headlines.mysql.schema.xml the table name was "_headlines" instead of "headlines". The same error was in headlines.map.inc.php

              I can't believe how many hours I spent to search for this...

              Now I can write to and read from the db.
                Modx 2.1.3, PHP 5.2
                • 3749
                • 24,544 Posts
                Great. I'm glad you got it worked out. smiley

                For the fulltext part, look at the definition of the modResource object in the modx schema (core/model/schema/modx.mysql.schema.xml).


                ---------------------------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
                MODx info for everyone: http://bobsguides.com/MODx.html
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 27117
                  • 28 Posts
                  Thank you Bob
                  Sometimes I can't believe how blind and stupid I am in such things smiley

                  I spent the whole day working on this and thanks to your guide, I learned a lot and enjoy modx even more now. Thank you so much for your tutorials and your nice writing!

                  There is just one thing that is not quite clear for me:

                  I have a date value in a textfield in my form (format is dd.mm.yyyy). Before storing the date in the db, I would like to convert it into a Unix Timestamp Integer but I have absolutely no clue, where I have to do this.

                  I'm sorry for asking so many noob questions but all this database and php stuff is pretty new for me.

                  Thanks!



                    Modx 2.1.3, PHP 5.2
                    • 3749
                    • 24,544 Posts
                    I think one way would be to provide an override method for set() in the class definition, but you could also just do this before saving it:


                    $timestamp = strtotime($date);
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 27117
                      • 28 Posts
                      Thanks for your help, I figured out how to write the timestamp into the db:

                      This is my new hook code for the formit call:
                      $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/headlines/';
                      $modx->addPackage('headlines',$base_path.'model/','tb_');
                      $headlines_form = $modx->newObject('Headlines');
                      //Converts the date from the form field into UnixTimestamp
                      $headlines_form->set('date', strtotime($_POST['date']));
                      
                      $headlines_form->save();
                      
                      return true;


                      Now I try to read the content from the db based on the resources tag "publishedon".

                      I would like to acomplish the following:

                      Show me all records, where the db field "date" equals resources tag "publishedon".

                      My Query looks like this:
                      // Query the Database
                      $query = $modx->newQuery('Headlines');
                      $query->where(array(
                        'date' => '[[*publishedon:strtotime:date=`%d.%m.%Y`]]',
                      ));


                      I see that I have to do some date format conversion because I only need the date and not the date/time from the Timestamp but I can't figure out how and where to do that.
                        Modx 2.1.3, PHP 5.2