i have a table called merchants in my database, in that table i have the field named merchant_code, and i want to generate a 8 code for my field in such a way that the first two characters contains the first two letters from my state_name from states table and next two characters contains first two letters from my city_name from cities table, and the last four characters should contain digits starting from ooo1. at the time of adding the merchant i want this code to automatically add, and save in my database i'm a beginner in cakehp, so please give me answer briefly, would be thankful if i found the solution
質問する
34 次
1 に答える
2
Merchant モデルはおそらく City モデルに (belongsTo として) 関連付けられており、さらに State モデル (belongsTo にも) に関連付けられています。Merchant モデルの afterSave コールバックを作成し、引数 $created を確認できます。true の場合は、関連付けられたモデルから 2 つのレコードをフェッチして、merchant_code を作成します。このようなもの: (City モデルに Containable 動作を使用することを考慮して):
public function afterSave($created) {
parent::afterSave($created);
if ($created) {
$this->City->contain('State');
$names = $this->City->find('first', array(
'conditions' => array(
'City.id' => $this->data['Merchant']['city_id']
),
'fields' => array(
'City.city_name',
'State.state_name',
)
));
if (!empty($names)) {
$merchant_code = substr($names['State']['state_name'], 0, 2)
. substr($names['City']['city_name'], 0, 2)
. sprintf('%1$04d', $this->getLastInsertID());
$this->saveField('merchant_code', $merchant_code);
}
}
}
于 2012-10-16T18:00:50.313 に答える