[SOLVED] Foreign key relationships in QCubed?

I have created two tables:

create tables cities(

cityID int not null auto_increment,

name varchar(40) not null,

primary key(cityID),

unique key(name));


create table stud(

studID int not null auto_increment,

name varchar(40) not null,

city int not null,

primary key (studID),

key(name),

foreign key (city) references cities(cityID))

 

Now say I want to access city name of a person by its forieign key, how do I do that? I have got so far:

<?php $objStud = Stud::Load(1); ?>

Student ID: <?php _p($objStud->StudID); ?> <br />

Name: <?php _p($objStud->Name); ?> <br />

City: <?php _p($objStud->CityCities->Name); ?> <br /> 

I get the error: Undefined GET property or variable in 'Stud' class: CityCities

All help will be highly appreciated.

Login or Register to comment on this topic.
Re [Foreign key relationships in QCubed?]:

Please tell me the line on the code where you are getting this error. When you get that error, the error page shown by QCubed would contain the line number as well as some code around the lines where the error occurred. As far as I can guess, this is because the User with the ID 1 would not be existing. 

When you try to load an object from the database using Load or Query* functions and that object does not exist then the result is returned as null. You should check for it. Otherwise the following lines would try to get a property out of a null value and the error will pop up. Ideally, the code should have been :

<?php

    $objUser = User::Load(1);

    if($objUser != null) {

    ?>

    Username: <?php _p($objUser->Username); ?> 

    Password: <?php _p($objUser->Password); ?> 

    Name: <?php _p($objUser->StudID->Name); ?> 

<?php } ?>

 

I hope that helps.

Again, the reason I asked about your IDE is because in most cases the IDE will show you auto-complete suggestions along with data types (both eclipse and PHPStorm do that). AFAIK, Aptana was forked from Eclipse so it should do the same as well. Do notice the auto-complete suggestions thrown at you. 

EDIT:

On my end, the code that seems to be right is: 

<?php _p($objUser->StudIDObject->Name); ?>

You are using <?php _p($objUser->StudID->Name); ?> which would be wrong. Like I said, pay attention to the data types that your IDE suggests. QCubed tries to detect which table columns are being used for foreign key to another table and the resulting object (the one you get as an expanded object for the foreign key relationship to another table) is named accordingly. 

Permalink
Login or Register to comment on this topic.