Search code examples
qcubed

QCubed - Join tables on multiple columns


Our company inherited an application using the Qcubed PHP framework. One of the features of this framework is the QQuery querying method. We have been unable to determine if its possible to create a join between two tables on two columns using QQuery.

Are there any QCubed developers still out there?


Solution

  • Are there any QCubed developers still out there? -> Yes, lot's of them. Most of the action can be found on the github

    https://github.com/qcubed/qcubed

    Joins: The fun thing about QQuery is that it joins tables automagically when the correct foreign keys are set up between tables. The examples page has tons of information on this, check it out:

    http://qcu.be/examples/index.php

    Extra tip: Do try to define your joins before you load them. Qcubed wil automatically do the join when a related object is requested, but it's better to expand them in advance.

    Quick example:

    //loads a Person object into memory
    //select * from person where id = 1;
    $objPerson = Person::Load(1); 
    echo $objPerson->Name . ' lives in ' . $objPerson->Town->Name;//will cause a query in the style "select * from town where id=x;"
    
    //loads a Person object into memory, and joins the Town table.
    //select * from person LEFT JOIN town on town.id = person.town_id where id = 1;
    $objPerson = Person::Load(1, QQ::Clause(QQ::Expand(QQN::Person()->Town))); 
    echo $objPerson->Name . ' lives in ' . $objPerson->Town->Name;//no extra query, town was already joined by the expand.
    

    Both will return the same data, but the latter is more efficient.

    To answer the part about having 2 columns as primary: I'm not a 100% sure, I have never used QCubed this way. However, check out http://qcu.be/examples/vendor/qcubed/framework/assets/php/examples/code_generator/primary_keys.php which mentions :

    QCubed also offers some support for tables that have multiple-column Primary Keys defined on it. For tables that have multi-column Primary Keys, QCubed will fully generate the object itself. But note that you will not be able to use this generated object as a related object for another table (in other words, QCubed does not support multi-column Foreign Keys). However, with all the generated Load methods in these objects, it is still possible to fully develop an application with tables that use multi-column Foreign Keys. Basically, whenever you want to access a related object via a multi-column Foreign Key, you can simply call that object's Load method directly to retrieve that object.

    Check out the codegen.xml file in your local install if you want to define relationships without foreign keys.