No related objects are found, yet they exist!

Hello all!

I've been playing around with Yii for quite a while now, trying to create an administration system for a summer camp which will handle events, participants and money exchanges ( i.e. purchases ). However, now I have met a wall I cannot seem to pass.

I have the following setup between the models Event, Person and Exchange:

Event MANY_MANY Person HAS_MANY Exchange

When I now try to do ( with $model being an Event ):

foreach ( $model -> participants as $participant )


  foreach ( $participant -> exchanges as $exchange )


    echo $exchange -> amount;

in which participants and exchanges are both lazy loaded, no amounts are printed, even though I know that there are many people connected to the event, and that there are exchanges connected to at least one of the people.

My relations are defined as such:

Person:


  'participated_in' => array(self::MANY_MANY, 'Event', 'Participants(event_id, person_id)'),


  'exchanges' => array(self::HAS_MANY, 'Exchange', 'person_id')


Exchange:


  'event' => array(self::BELONGS_TO, 'Event', 'event_id'),


  'person' => array(self::BELONGS_TO, 'Person', 'person_id')


Event:


  'participants' => array(self::MANY_MANY, 'Person', 'Participants(person_id, event_id)'),


  'exchanges' => array(self::HAS_MANY, 'Exchange', 'event_id')


The weird part is that if I do:

Person::model()->findByPK(1)->exchanges

I get an empty array. However, if I do:

Exchange::model()->findAllByAttributes(array('person_id' => 1))

I get all the relevant exchanges… :S

Also, I've checked the Yii debug log, and executed the SQL it uses when lazy loading myself, and when running it manually, I get the correct rows in return. Seems like Yii just doesn't manage to parse them correctly?

Hope someone has some better insight on this than I have…

Jon(hoo)

That's very strange. What yii version are you using? Could you upload relevant scripts and data so that I can debug with? Thanks.

You can see the relevant files at: http://docs.internat…blic/yii-debug/

Event.php, Exchange.php and Person.php are the models, oksnoen.db is the sqlite3 database and kiosk.php is the view that is trying to display the data. This is where I try to loop through the exchanges of all the participants of the event…

The sumAmount method is declared in the Person model, and loops through all exchanges and adds together those that matches the given criterias.

Hope you can help me somehow!

Jon

EDIT: I use Yii 1.0.7

Wow. You were completely right…

I guess I am too used to auto_increment…

The finished product will be running with PostgreSQL, and I've never used SQLite before, which it seems is quite a lot less strict that other SQL engines…

But still, I think Yii should somehow come with an error instead of returning just an empty array?

Hi,

Please check if Primary key on your Exchange table (col exchange_id) has a value, that’s why the relation doesn’t find anything and you find a record via attribute search (which is set for a “1” person). I’m not a sqlite user, but when viewing the source of your db - i see a PK declaration, but strange thing that it doesn’t throw anything on save ;)

Cheers

ps. glad if someone will explain :)



CREATE TABLE Exchange (


 exchange_id INT,


 completed BIGINT,


 amount INT,


 person_id INT,


 event_id INT,


 PRIMARY KEY ( exchange_id ),


 CONSTRAINT person_exchange_fk FOREIGN KEY ( person_id ) REFERENCES Person ( person_id ),


 CONSTRAINT event_exchange_fk FOREIGN KEY ( event_id ) REFERENCES Event ( event_id )


isn’t that because the not null is missing in declaration ?? hehe, funny , never thought that you can apply a primary key constraint to a null field :D

Hmm… How did my reply appear above your post…?

Anyways, my reply is above what you wrote =p

glad i could help ;)

Well that's not a yii error, cause a non-PK object is a filthy one, that shouldn't happen so the database should be kept consistent really.

ps. i’ve deleted my post accidentally, that’s why it appeared below yours ;)

True, but I still think it should say somewhere in the log that Yii dropped certain rows because they lack a PK value?

Would have made this problem much more apparent at least…

Thanks to gregghouse for helping out!