We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20546
    • 74 Posts
    I wonder what would be the easiest way to add new fields to an existing database-table without losing the data within that table. I tried to modify my schema.xml and generated the new map-file for it but it won’t add a new field to my table.

    Is there another way to do that?

    Thanks
    • What schema xml? Can you be more specific what you are asking about? Is this an xPDO question or are you asking about changing the structure of core MODx tables?
        • 15001
        • 697 Posts

        I think he wants adding a new column to an existing MySQL table, not necessarily belonging to the MODx core set of tables.

        If you have the new field values in an Excel table or equivalent, you can export 2 columns in a csv file. These two column would be the record IDs and the new field values.

        Then, create the new table column (i.e. field) in phpMyAdmin.

        After this, open you csv file in a text editor allowing column mode edition.
        Typically www.crimsoneditor.com for Windows. The equivalent for Mac is Emerald editor.

        In this editor, toggle to "column mode" and enter your SQL queries, which fill the new field for each column row where record ID equals the ID value in your csv file.
        "Column mode" can sound strange, but you’ll immediately understand what I mean once you have Crimson Editor.

        Copy-paste the whole set of SQL requests in the "SQL" Tab of phpMyAdmin and run them.

        That’s all.
        • Quote from: Jul at Jun 19, 2010, 06:50 PM

          I think he wants adding a new column to an existing MySQL table, not necessarily belonging to the MODx core set of tables.
          If that’s the case, the column can be added through the mysql client, or phpmyadmin, or whatever. xPDO does not automatically alter tables at this point, though schema migrations are planned for future xPDO releases.
            • 15001
            • 697 Posts
            I agree. But if the number of records is high, once the column created with phpMyAdmin, it is more efficient injecting SQL code than editing records manually in phpMyAdmin.

            Using an editor allowing column mode is much useful to edit hundred of lines simultaneously. This makes very easy to build the SQL requests, especially if the data are available in csv or in a spreadsheet (e.g. Excel, OpenOffice Calc).
            • The OP never said anything about editing individual records; not sure what you are talking about.
                • 15001
                • 697 Posts

                (...)add new fields to an existing database-table without loosing the data within that table

                This make me think that he does not want want to simply append a table column, but also fill that column with values.

                So, if one wants to automate this, a solution is an SQL query like this:

                To create the new colum, the SQL request is:
                ALTER TABLE table_name ADD new_column_name datatype

                (phpMyAdmin offers simpler way to create new table column)

                To edit the records in the new column:
                UPDATE table_name SET new_column_name = new_value WHERE identifier_column_name=identifier_value;


                Example:
                UPDATE forum_users SET Firstname="Jason"  WHERE UserId="OpenGeek"
                UPDATE forum_users SET Firstname="Julien" WHERE UserId="Jul"
                (a.s.o)

                Not tested, but should be something like that...

                With a code editor allowing column mode, you only type once the text that is common to all rows.

                So, if you have a csv file with the unique key in one column and the value to insert in another colum, it’s ultra-easy to create the whole set of SQL requests with a code editor allowing column mode and using the SQL tab of phpMyAdmin. Then, you don’t have to write a single line of PHP code.
                It’s also much easier than editing each table row manually in phpMyAdmin.
                  • 20546
                  • 74 Posts
                  Thanks for your replies. I was just wondering if there was a simpler solution than manually add new columns. But that’s no problem.
                  • Quote from: mt85 at Jun 23, 2010, 09:49 AM

                    Thanks for your replies. I was just wondering if there was a simpler solution than manually add new columns. But that’s no problem.
                    Again, no, but an implementation of "schema migrations" for xPDO is definitely on the roadmap to address this.
                      • 20546
                      • 74 Posts
                      That sounds promising. smiley

                      Thanks