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?