我有两张桌子:
senderinfos:
id | Name | email_add | Address | ============================================ 1 | Amit | 1@1.com | park Street|
receiverinfos:
id | Name | email_add| Address | ======================================= 1 | SOS | 2@2.com | park1
我正在提供模型,视图和控制器代码:
模型
Senderinfo:
<?php
App::uses('AppModel', 'Model');
/**
* Admin Login Model
*
*/
class Senderinfo extends AppModel
{
public $name='Senderinfo';
public $usetables='senderinfos';
public $validate = array(
'contact' =>
array(
'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
'allowEmpty' => false,
'message' => 'Please Enter Contact.'
),
'name' =>
array(
'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
'allowEmpty' => false,
'message' => 'Please Enter Sender Name.'
),
'email_add' => array(
'email_add' => array(
'rule' => 'email',
'allowEmpty' => true,
'message' => 'Please Enter Valid Email',
'last' => true
)),
);
}
?>
Receiveinfo:
<?php
App::uses('AppModel', 'Model');
/**
* Admin Login Model
*
*/
class Receiverinfo extends AppModel
{
public $name='Receiverinfo';
public $usetables='receiverinfos';
public $validate = array(
'contact' =>
array(
'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
'allowEmpty' => false,
'message' => 'Please Enter Contact.'
),
'name' =>
array(
'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
'allowEmpty' => false,
'message' => 'Please Enter Sender Name.'
),
'email_add' => array(
'email_add' => array(
'rule' => 'email',
'allowEmpty' => true,
'message' => 'Please Enter Valid Email',
'last' => true
)),
);
}
?>
查看
send_money.ctp
<content>
<div class="pg_title txtLeft">Send Money</div>
<?php echo $this->Form->create('Agents', array('action' => 'send_money'));?>
<div style="display:none;"><input type="hidden" value="POST" name="_method"></div>
<fieldset title="SENDER'S INFORMATION"><legend>SENDER'S INFORMATION</legend>
<table>
<tbody>
<tr>
<td><label>Contact No.<span class="red">*</span></label></td>
<td><?php echo $this->Form->input('Senderinfo.contact',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Sender's Name<span class="red">*</span></label></td>
<td><?php echo $this->Form->input('Senderinfo.name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Email Address</label></td>
<td><?php echo $this->Form->input('Senderinfo.email_add',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Mailing Address</label></td>
<td><?php echo $this->Form->input('Senderinfo.address',array('label'=>false,'div'=>false,'type'=>'textarea','cols'=>39,'rows'=>3,'class'=>'textarea','style'=>'width:330px')); ?></td>
</tr>
</tbody>
</table>
</fieldset>
<fieldset title=""><legend>RECEIVER'S INFORMATION</legend>
<table>
<tbody>
<tr>
<td><label>Contact No.<span class="red">*</span></label></td>
<td><?php echo $this->Form->input('Receiverinfo.contact',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Receiver's Name<span class="red">*</span></label></td>
<td><?php echo $this->Form->input('Receiverinfo.name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Email Address</label></td>
<td><?php echo $this->Form->input('Receiverinfo.email_add',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Mailing Address</label></td>
<td><?php echo $this->Form->input('Receiverinfo.address',array('label'=>false,'div'=>false,'type'=>'textarea','cols'=>39,'rows'=>3,'class'=>'textarea','style'=>'width:330px')); ?></td>
</tr>
</tbody>
</table>
</fieldset>
<fieldset><legend>MONEY TRANSFER</legend>
<table>
<tbody>
<tr>
<td><label>Amount(tk)<span class="red">*</span></label></td>
<td><?php echo $this->Form->input('Transaction.sending_amount',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<td><label>Charge(tk)<span class="red">*</span></label></td>
<td><?php echo $this->Form->input('Transaction.charge',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td>
</tr>
<tr>
<?php
$foo = "";
$options = array('1' => 'Paid','0' => ' Due');
$attributes = array(
'legend' => false,
'value' => true,
'checked'=> ($foo == "Paid"),
);
?>
<td>
<?php echo $this->Form->radio('transaction.status',$options, $attributes);?>
</td>
</tr>
</tbody>
</table>
</fieldset>
<fieldset><legend>CUSTOM MESSAGE</legend>
<table>
<tbody>
<tr>
<td style="width: 158px;"><label>Message</label></td>
<td><?php echo $this->Form->input('transaction.message',array('label'=>false,'div'=>false,'type'=>'textarea','cols'=>39,'rows'=>3,'class'=>'textarea','style'=>'width:330px')); ?></td>
</tr>
</tbody>
</table>
</fieldset>
<table>
<tbody>
<tr>
<td colspan="2" align="right"><input type="reset" value="Reset"> | <?php echo $this->Form->submit('SEND', array('div' => false,'formnovalidate' => true));?></td>
</tr>
</tbody>
</table>
<?php print $this->Form->end();?>
</content>
<div class="clear"></div>
Controller :
public function send_money()
{
$this->layout='agent';
if(empty($this->data) == false)
{
if($this->Senderinfo->save($this->data))
{
$this->Receiverinfo->save($this->data);
//$this->Session->setFlash('Data inserted successfully.');
//$this->redirect('send_money');
}
/*if($this->Receiverinfo->save($this->data))
{
//$this->Session->setFlash('Data inserted successfully.');
$this->redirect('send_money');
}
*/
}
else
{
//$this->set('errors', $this->Senderinfo->invalidFields());
//$this->set('errors', $this->Receiverinfo->invalidFields());
}
}
我想将记录插入两个表中并进行完美验证。但是我的验证部分没有完美运行。这意味着当你看到我的查看页面时,你会看到发件人信息,接收者信息,由html图例包装的钱我想在点击提交按钮后一次做验证必填字段。在这个代码中发生的事情是当我按下提交按钮时发送者信息验证工作但接收者信息部分验证不起作用。当我在发送者部分放任何值时那个时候接收者信息部分的验证只能起作用。我该怎么做?
答案 0 :(得分:0)
首先;显然,你的应用程序中的'sender'和'receiver'之间存在“关系”,但不在你的模型/数据库定义中。 依赖数据库的“自动递增”ID(例如,接收方1和发送方1属于一起)不可靠。自动递增ID 会在两个表之间“不同步”,最终会导致'receiver 123'
实际上应与'sender 125'
“连接”。
如果我正确理解了这个表单的作用,那就是在数据库中创建一个“货币交易”。发件人和收件人应该(在这种情况下)可能不存储在单独的数据库中,除非您打算提供从列表中“选择”现有发件人/收件人的选项。
在这两种情况下,你的应用程序中应该有一个表格/模型“连接”两者,否则你只会随机列出一些人'发送'和'接收'的钱,但没办法知道哪个的人想要汇款给哪个收件人
如果您不打算“重复使用”发件人/收件人,那么正确的数据库设计应该是这样的:
id| sender_name | sender_email | sender_address | receiver_name | receiver_email | receiver_address
1 | Amid | 1@1.com | Parkstreet | SOS | 2@2.com | park 1
我会将此表命名为“汇款”和模型“Moneytransfer”
同时使用1个模型将简化验证,并保证发送者和接收者信息都存在且正确。
如果你真的想手动保存两个单独的记录(基于我上面的解释,我强烈建议你不这样做)你应该这样做; < / p>
$this->Mymodel->validates($data)
有关在控制器内手动验证的更多信息,请访问: http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html
有关在CakePHP中使用事务的更多信息,请访问: http://book.cakephp.org/2.0/en/models/transactions.html
<强> [更新] 强>
进一步浏览“表单”,似乎“似乎”也是一个“交易”模型? 如果就是这种情况并且在CakePHP中正确定义了模型关系,您将能够使用Model ::保存所有相关的记录saveAssociated()。这也将包含事务中的所有保存(原子)