如何通过邮政编码将一个 table 加入另一个 table?
How to Join one table to another table by zipcode?
我有两个 table 我想加入 Ad.zipcode
和 Zipcode.zip
请查看下面的详细信息 -
Table : 广告
+----+----------+---------+----------+
| 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 |
+----+----------+---------+----------+
Table : 邮政编码
+--------+---------+----------+
| zip | city | state |
+--------+---------+----------+
| 789520 | ctc | odisha |
| 756114 | bhadrak | odisha |
| 123456 | bbsr | odisha |
| 756114 | rlc | odisha |
| 789502 | bls | odisha |
---------+---------+----------+
我想加入 ads
table 中包含的邮政编码 我需要在第二个 table 中获取记录 ads
zipcode
是 756114
我曾尝试加入 $hasMany
但 id 不存在于 zipcodes
table 中,为什么加入不起作用。
我的代码:
public $hasMany = array(
'Zipcode' => array(
'className' => 'Zipcode',
'foreignKey' => 'zip',
),
);
在我上面的代码中,我得到了空白数组。请帮我。
您好,我正在使用 CakePHP 版本-2.x
如何将模型与另一个模型关联?
在您的邮政编码模型中,您需要:
public $primaryKey = 'zip';
附带说明一下,为什么你在 table 中没有自动数字 PK?邮政编码通常包含非数字字符...
在广告模型位置:
public $belongsTo = array(
'Zipcode' => array(
'className' => 'Zipcode',
'foreignKey' => 'zipcode',
)
);
邮编模型地址:
public $primaryKey = 'zip';
public $hasMany = array(
'Ad' => array(
'className' => 'Ad',
'foreignKey' => 'zipcode',
)
);
我有两个 table 我想加入 Ad.zipcode
和 Zipcode.zip
请查看下面的详细信息 -
Table : 广告
+----+----------+---------+----------+
| 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 |
+----+----------+---------+----------+
Table : 邮政编码
+--------+---------+----------+
| zip | city | state |
+--------+---------+----------+
| 789520 | ctc | odisha |
| 756114 | bhadrak | odisha |
| 123456 | bbsr | odisha |
| 756114 | rlc | odisha |
| 789502 | bls | odisha |
---------+---------+----------+
我想加入 ads
table 中包含的邮政编码 我需要在第二个 table 中获取记录 ads
zipcode
是 756114
我曾尝试加入 $hasMany
但 id 不存在于 zipcodes
table 中,为什么加入不起作用。
我的代码:
public $hasMany = array(
'Zipcode' => array(
'className' => 'Zipcode',
'foreignKey' => 'zip',
),
);
在我上面的代码中,我得到了空白数组。请帮我。 您好,我正在使用 CakePHP 版本-2.x
如何将模型与另一个模型关联?
在您的邮政编码模型中,您需要:
public $primaryKey = 'zip';
附带说明一下,为什么你在 table 中没有自动数字 PK?邮政编码通常包含非数字字符...
在广告模型位置:
public $belongsTo = array(
'Zipcode' => array(
'className' => 'Zipcode',
'foreignKey' => 'zipcode',
)
);
邮编模型地址:
public $primaryKey = 'zip';
public $hasMany = array(
'Ad' => array(
'className' => 'Ad',
'foreignKey' => 'zipcode',
)
);