获取相关记录失败

时间:2014-10-25 02:56:42

标签: phalcon

我在让Phalcon ORM加载相关记录方面遇到了一些麻烦。一个简单的例子,为简洁起见,一些代码被截断:任何人都可以告诉我为什么要调用相关表格"忘记"来自父表的外键?

对于给定的表格用户,可以有多个 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

和../

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'));

此代码......

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

得到我......

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

问题:为什么对user_addrs的调用忘记了相关记录是针对users.id 103的?

0 个答案:

没有答案