Web Tool Bag  
Home · Articles · Downloads · Discussion Forum · Web Links · News Categories · Synonyms DatabaseApril 25 2017 01:59:54
Navigation
Home
Articles
Downloads
Discussion Forum
Web Links
News Categories
Synonyms Database
Search
Users Online
Guests Online: 2
No Members Online

Registered Members: 856
Unactivated Members: 118
Newest Member: lakim
Forum Threads
Newest Threads
Uncaught Error: _reg...
Module build failed:...
Installation
mochi script questions
Redirect with captch...
Hottest Threads
Installation [12]
Any questions and... [5]
Captcha picture d... [4]
Integrate with Vi... [4]
Mods: Sucess/Than... [4]
 
Latest Articles
Mouse pointer disapp...
UBUNTU install via U...
Geany tried to acces...
How to create MySql ...
Ubuntu 12.10 mouse f...
Gallery 2 Module Development: Database part 1
While making this tutorial i will talk about creating tables. I will talk about the rest in another tutorial, this tutorial alone is already quite some text.

Before I will start with explaining how to make a database, there is something you really should know. There are 2 ways of making a database. First, the a bit more difficult but good way. Second, the easy but wrong way.

The good way: Make!
The right way is by using a file called gnumakefile to make the DB files. You need to make a .xml file where you tell the gnumakefile file what fields you want in your table. When you've made the xml file, you need to run ‚Äúmake‚ÄĚ or ‚Äúgmake‚ÄĚ in the folder where gnumakefile is located. (there are some needed details about this, i will write about that in a minute) Then gnumakefile is called by make (or gmake), and two files are made. An .INC file, and a .TPL file. (which will be located in a new folder that's also newly made)

The filename of the .inc file depends about the type of storage you are using, a map or an entity. You can read what the difference is in my first tutorial. (Click) When you want a map type of table, the file will be called Maps.inc. (notice the capital M) And an entity type of table will be named Entities.inc. (again with the capital E)

The .tpl file will be stored in a new folder called GalleryStorage. The tpl itself is called schema.tpl.

When you activate a new module, g2 will search the schema.tpl file, and reads the data in the file.. Followed by creating a brand new custom table!

So, lets stop our little chit chat and make ourself a custom table!

I will talk about making a Map type table in this tutorial, making a entity shouldn't be much different.

The files for the database are stored in a new folder. So go over to your module folder, and create a new folder called classes. (ex. /modules/mymodule/classes) Then make a new .xml file called Maps.xml.

The xml file look a bit like this:

Code:

1
<!DOCTYPE maps SYSTEM "../../../lib/tools/dtd/MapsDefinition2.0.dtd">
2
<maps>
3
  <map>
4
    <map-name>ModuleNameMap</map-name>
5
    <schema>
6
      <schema-major>1</schema-major>
7
      <schema-minor>0</schema-minor>
8
    </schema>
9
    <member>
10
      <member-name>itemId</member-name>
11
      <member-type>INTEGER</member-type>
12
      <primary/>
13
    </member>
14
    <member>
15
      <member-name>stringStorage</member-name>
16
      <member-type>STRING</member-type>
17
      <member-size>MEDIUM</member-size>
18
      <required/>
19
    </member>
20
  </map>
21
</maps>


I think this would make sense right?

- <map-name>ModuleNameMap</map-name> Defines your table name.
- <member>...</member> Defines a new field.
- <member-name>itemId</member-name> Defines a field name.
- <member-type>INTEGER</member-type> Defines the type of field.
- <member-size>MEDIUM</member-size> Defines the size of the field.


Save the file, and run gnumakefile in the /modules/mymodule/classes folder. You can get gnumakefile from from the /modules/comment/classes module. Or download it from Combined-Minds.net. (the version that came with 2.1 development edition.. if this version doesn't work with the current version of g2, please reply to this tutorial)

If this worked, upload the new files to your server and install/activate the module. And the database is build.

Ah, and now some of you got a problem right? Your using Windows and don't have make or gmake? Well there are some things you can do..

- Download cygwin, a linux emulator to still be able to use linux stuff on windows
- Run the make file on the server by using SSH
- Being a bad boy and go over to the wrong side of this tutorial


You are still reading this tutorial? Ah, so you've chosen to do it the wrong way?

The bad way: Create the files yourself!
Today (22-feb-07) i had a kind of discussion with on of the main developers of the g2 project. We were talking about how creating a table in g2. I told him that i make tables by using the technique that i will explain in a few minutes. He actually became a bit irritated when i said i would write a tutorial, and talk about the wrong way of making a table.

The reason why this way is wrong, is because you don't know if the files you will create will work on all servers. And the way of making the files with the make file won't change, but the way i will talk about in a minute will probably change in a few g2 versions. (don't worry, i will update the tutorial)

So, only use the following way if the good way doesn't work.

The bad technique is writing the 2 files needed yourself. It's not difficult at all, so lets start at looking at an example.

Maps.inc

Code:

1
<?php
2
$mapInfo
['ModuleNameMap'] = array(
3
    
'itemId'=>array(
4
       
'type'=>STORAGE_TYPE_INTEGER,
5
       
'size'=>STORAGE_SIZE_MEDIUM,
6
       
'notNull'=>true),
7
    
'stringStorage'=>array(
8
       
'type'=>STORAGE_TYPE_STRING,
9
       
'size'=>STORAGE_SIZE_MEDIUM,
10
       
'notNull'=>true)
11
    
);
12
?>



This basically is the same as the Maps.xml shown before. But written on a different way.

schema.tpl (remember that this file should be located in a new folder named GalleryStorage)

Code:

1
## mysql
2
#  ModuleNameMap
3
CREATE TABLE DB_TABLE_PREFIXModuleNameMap (
4
 DB_COLUMN_PREFIXitemId int(11) NOT NULL,
5
 DB_COLUMN_PREFIX stringStorage  varchar(128) NOT NULL
6
) TYPE=DB_TABLE_TYPE
7
/*!40100 DEFAULT CHARACTER SET utf8 */;
8

9
INSERT INTO DB_TABLE_PREFIXSchema (
10
 DB_COLUMN_PREFIXname,
11
 DB_COLUMN_PREFIXmajor,
12
 DB_COLUMN_PREFIXminor
13
) VALUES('PersoonNaamMap', 1, 0);


Again very simple, plain SQL. But notice DB_COLUMN_PREFIX. This is just a define that contains the prefix for the database. You are already working at a wrong way, so you can remove DB_COLUMN_PREFIX and add the prefix yourself if you want.

When you're done, save the files and upload it to your g2 installation. Install, or reactivate your module, and the table should be made!

Again please remember this way isn't like its meant. Always try to use the make file way!

http://www.combined-minds.net

Posted by admin on December 10 2007 17:48:30 3291 Reads · Print
Ratings
Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Login
Username

Password



Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Member Poll
Which PHP framework do you preffer?

Symfony

Zend

CodeIgniter

PHP on TRAX

eZ Components

Fusebox

PhpOpenbiz

Prado

QPHP

Seagull

You must login to vote.
Shoutbox
You must login to post a message.

Vince
03/10/2011 21:17
Hi, How to remove Register from Login screen? I don't want them to register and have full access! if you leave register then they should not have any rights until the admin assigns them

webtoolz
26/09/2011 11:28
Please describe your problem with more details. Thank you.

bimmer98
22/11/2010 20:31
Help. There was a problem with the request; error regarding feedbackzdr form program

Web hosting industry articles
Copyright © 2017 - www.webtoolbag.com