【发布时间】:2015-06-09 10:07:05
【问题描述】:
我有两张桌子我想加入Ad.zipcode 和Zipcode.zip 请查看以下详细信息 -
表格:广告
+----+----------+---------+----------+
| id | user_id | zipcode | photo |
+----+----------+---------+----------+
| 1 | 1 | 751010 | img1.jpg |
| 2 | 2 | 123456 | img2.jpg |
| 3 | 3 | 756114 | img3.jpg |
| 4 | 4 | 121010 | img4.jpg |
| 5 | 5 | 789520 | img5.jpg |
| 6 | 6 | 404040 | img6.jpg |
| 7 | 7 | 805020 | img7.jpg |
+----+----------+---------+----------+
表格:邮政编码
+--------+---------+----------+
| zip | city | state |
+--------+---------+----------+
| 789520 | ctc | odisha |
| 756114 | bhadrak | odisha |
| 123456 | bbsr | odisha |
| 756114 | rlc | odisha |
| 789502 | bls | odisha |
---------+---------+----------+
我想加入ads 表中包含的邮政编码,我需要获取第二个表中ads zipcode 是756114 的记录
我已尝试加入$hasMany,但zipcodes 表中不存在id,为什么加入不起作用。
我的代码:
public $hasMany = array(
'Zipcode' => array(
'className' => 'Zipcode',
'foreignKey' => 'zip',
),
);
在我上面的代码中,我得到了空白数组。请帮我。 嗨,我使用的是 CakePHP version-2.x
如何将模型与另一个模型关联?
【问题讨论】:
标签: mysql cakephp-2.0 model-associations