We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Get Related Records Defined in Has Many Relationship

This is a question I posted on StackOverflow originally but didn't seem to get any attention so I'm going to double post here and see if anyone happens to see what I'm doing wrong.

The basic issue is that I am relying on the ORM to pull related records within a model but it doesn't seem to know to pull related records.

Original: https://stackoverflow.com/questions/26558919/get-related-records-fails

I'm having some trouble getting the Phalcon ORM to load related records. A simple example with some code truncated for brevity: Can anyone tell me why calls to related tables "forget" the foreign key from the parent table?

For a given table Users, there can be many UserAddrs

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    .....
  PRIMARY KEY (`id`),
  UNIQUE KEY `password_UNIQUE` (`password`)
) ENGINE=InnoDB AUTO_INCREMENT=2806 DEFAULT CHARSET=latin1

and....

CREATE TABLE `user_addrs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `users_id` int(10) unsigned NOT NULL
  PRIMARY KEY (`id`),
  KEY `fk_user_addrs_users1_idx` (`users_id`),
  CONSTRAINT `fk_user_addrs_users1` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1

/models/Users.php

public function initialize()
{
    $this->hasMany("id", "PhalconRest\Models\UserAddrs", "users_id", array('alias' => 'UserAddrs'));

/models/UserAddrs.php

public function initialize()
{
    $this->belongsTo('users_id', 'PhalconRest\Models\Users', 'id', array('alias' => 'Users'));

This code....

    $user = Users::findFirst($id);
    $userAddrsRecs = $user->UserAddrs->toArray();

Gets me...

SELECT `users`.`id` FROM `users` WHERE `users`.`id` = 103 LIMIT :1

SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='user_addrs'

DESCRIBE `user_addrs`

SELECT `user_addrs`.`id` FROM `user_addrs` WHERE `user_addrs`.`users_id` = :0

Question: Why does the call to user_addrs forget that the related record was for users.id 103?



98.9k

Why do you think it's forgetting users.id is 103?



25.1k

I'm following the code samples here: https://docs.phalcon.io/en/latest/reference/models.html#taking-advantage-of-relationships

In particular, this section leads me to belive that I can load one record and then ask that record for is related data.

By accessing an attribute with the same name as the relationship will retrieve all its related record(s).

<?php
$robot = Robots::findFirst();
$robotsParts = $robot->robotsParts; // all the related records in RobotsParts

I'm trying to do something similar but with different relationship names (mine are aliased) but shouldn't I still get related records? $user = Users::findFirst($id); $userAddrsRecs = $user->UserAddrs->toArray();

My log file clearly shows that the 2nd call doesn't look for related records at all.



98.9k
Accepted
answer

This part of the log shows it's querying the data:

DESCRIBE `user_addrs`
SELECT `user_addrs`.`id` FROM `user_addrs` WHERE `user_addrs`.`users_id` = :0


25.1k
edited Oct '14

SELECT user_addrs.id FROM user_addrs WHERE user_addrs.users_id = :0

I think I get it. The part where the log reported ...wrote = :0... looked like garbage SQL to me at first...and second glance :)

Now I see that is the syntax for a bound paramater!

I also did some reading on Model Metadata.