Codegen from 0.3 to 0.4

thread: 6 messages  |  last: about 7 years ago  |  started: sunday, september 11, 2011, 2:51 am pdt

#1  |  danOxy123 Spain
Sunday, September 11, 2011, 2:51 AM PDT

Hello, recently i upgraded qcodo from to 0.3 to 0.4 following “my own guideline”.

It seems to work everything excepting one detail:

Lets imagine i have a table called “person” with a foreign key “id_friend” (which points to the id in the friend table)

- When i applied codegen in qcodo0.3 i had a member object called “Friend” in PersonGen. I could access to the friend by calling $personObject->Friend

- But now in qcodo0.4 i have a member object called “idFriendObject” in the Gen class. So i obtain errors in all the places where i wrote $objectPerson->Friend

How can i fix that?. I have a lot of classes and it is tedious to make that changes manually.


#2  |  Mike Ho (San Diego, CA) United States of America Qcodo Administrator
Sunday, September 11, 2011, 11:12 PM PDT


Hmm... that's odd... the extra “object” really shouldn't be there.

Can you provide the schema definition to the specific two tables that is causing this strange behavior?

Also, provide your codegen.xml file as well.


#3  |  danOxy123 Spain
Monday, September 12, 2011, 10:20 AM PDT

Hello, thanks for answering. These are the files:

<?xml version=“1.0” encoding=“UTF-8” ?>
   This XML file defines the settings used when running the Code Generator.
   When running CodeGen, you must specify a CodeGenSettings XML file like
   this one to use.  Feel free to copy, modify, etc.
   <!-- General CodeGen Settings -->
   <name application=“My Application”/>
   <templateEscape begin=“&lt;%” end=“%&gt;”/>

   <!-- Define Data Sources (e.g. Databases, Services, etc.) -->

       <database index=“1”>
           <!-- Append Prefixes/Suffixes to the Following Code Generated Items -->
           <className prefix=“" suffix=““/>
           <associatedObjectName prefix=“" suffix=““/>

           <!-- Table Type Identifiers - Used to Classify Tables in your Data Model -->
           <typeTableIdentifier suffix=“_type”/>
           <associationTableIdentifier suffix=“_assn”/>

           <!-- Other -->
           <stripFromTableName prefix=““/>

           <!-- Specific Tables to Exclude or Include in the CodeGen process -->
                   * “include” is used to override any “exclude” patterns or lists
                   * “list” should be a  comma-separated list of table names
                   * “pattern” is a regexp pattern
               So for example, if you only want to codegen any table prefixed with “qc_”,
               you should set an excludeTables pattern to exclude all, or “[0-9a-zA-Z_]*”.
               And then you should set an includeTables pattern of “^qc_[0-9a-zA-Z_]*”.
           <excludeTables pattern=“" list=““/>
           <includeTables pattern=“" list=““/>

           <!-- Support for “Beta 2” version of Manual Queries and Hard Coded Column Names -->
           <manualQuery support=“false”/>

           <!-- Virtual Foreign Key Relationships -->
               There are two ways to define relationships.  To signify a virtual FK
               relationship (for example, on foo.bar_id which would key to
               you can:

               A) Simply define the relationship in the <relationships/> tag using
                  the following format:
                   foo.bar_id =>
                   bar.other_id =>
                   (with one relationship per line)
                  Note that because the relationships data uses a “>” symbol, proper
                  XML rules dictate that the content of the relationships script must
                  be escaped with <![CDATA[ and ]]> tags.

               B) You can define the relationships in totally separate relationships
                  script file by using the <relationshipsScript/> tag, where you can
                  specify a filepath and a format.  Format can either be:
                   qcodo - follows the same “foo.bar_id =>” as above
                   sql - parses the script, looking for SQL-based ALTER TABLE statments like:
                       ALTER TABLE foo ADD CONSTRAINT blah FOREIGN KEY (bar_id) REFERENCES bar (id);
           <relationshipsScript filepath=“" format=“sql”/>

Example of one particular case:
CREATE TABLE `contact` (
 `id` int(11) NOT NULL auto_increment,
 `nom` varchar(255) default NULL,
 `prenom` varchar(255) default NULL,
 `mail` varchar(255) NOT NULL,
 `adresse` varchar(255) default NULL,
 `objet` varchar(255) NOT NULL,
 `message` varchar(255) default NULL,
 `id_membre` int(11) default NULL,
 PRIMARY KEY  (`id`),
 KEY `contact_membre` (`id_membre`)
ALTER TABLE `contact`


Then in the member objects section of ContactGen class:
// Member Objects
case 'IdMembreObject':
// Gets the value for the Membre object referenced by intIdMembre
// @return Membre
   try {
   if ((!$this->objIdMembreObject) && (!is_null($this->intIdMembre)))
       $this->objIdMembreObject = Membre::Load($this->intIdMembre);
           return $this->objIdMembreObject;
   } catch (QCallerException $objExc) {
                       throw $objExc;

When i got the next in qcodo0.3:

case 'Membre':
                    * Sets the value for the Membre object referenced by intIdMembre
                    * @param Membre $mixValue
                    * @return Membre
                   if (is_null($mixValue)) {
                       $this->intIdMembre = null;
                       $this->objMembre = null;
                       return null;
                   } else {
                       // Make sure $mixValue actually is a Membre object
                       try {
                           $mixValue = QType::Cast($mixValue, 'Membre');
                       } catch (QInvalidCastException $objExc) {
                           throw $objExc;

                       // Make sure $mixValue is a SAVED Membre object
                       if (is_null($mixValue->Id))
                           throw new QCallerException('Unable to set an unsaved Membre for this Contact');

                       // Update Local Member Variables
                       $this->objMembre = $mixValue;
                       $this->intIdMembre = $mixValue->Id;

                       // Return $mixValue
                       return $mixValue;

#4  |  danOxy123 Spain
Monday, September 12, 2011, 10:23 AM PDT

sorry, for the 0.3 case i pasted code from the set method. it would be:

// Member Objects
case 'Membre':
* Gets the value for the Membre object referenced by intIdMembre
* @return Membre
try {
if ((!$this->objMembre) && (!is_null($this->intIdMembre)))
   $this->objMembre = Membre::Load($this->tntIdMembre);
   return $this->objMembre;
} catch (QCallerException $objExc) {
   throw $objExc;

This case is extensible to any relation between two tables

#5  |  Mike Ho (San Diego, CA) United States of America Qcodo Administrator
Monday, September 12, 2011, 10:39 AM PDT

I'm not actually sure why the 0.3 version shows just “membre”... but the reason why you're getting IdMembreObject is because you have the column named id_membre.

The correct standard for FK naming is supposed to be membre_id

If you change your column to be named membre_id, it will create a property named “membre” as you would expect.

#6  |  danOxy123 Spain
Monday, September 12, 2011, 10:58 AM PDT

Thanks a lot!!. I changed the column name and it works!

The bad point is that i will need to change a looot of columns and i dont know if that can create any kind of collateral effect.

That column names where stablished before i joined the project so i dont know if the people who worked on it made modifications on QCodo core.

Thanks for your support again

Copyright © 2005 - 2019, Quasidea Development, LLC
This open-source framework for PHP is released under the terms of The MIT License.