We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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: http://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`)


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


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


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


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?


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


I'm following the code samples here: http://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).

$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.


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

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.