我有兩個表我想加入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
表中的郵政編碼我需要獲取記錄在t他第二個表,ads
zipcode
是756114
我試圖在$hasMany
但ID不存在投身於zipcodes
表爲什麼要加入不工作。
我的代碼:
public $hasMany = array(
'Zipcode' => array(
'className' => 'Zipcode',
'foreignKey' => 'zip',
),
);
在我aboave代碼我得到空數組。請幫幫我。 您好我正在使用CakePHP版本2.x
如何將模型與另一個模型關聯?
'zip'可以包含重複值。我如何設置主鍵? – Chinmay235