Php 如何在同一模型中使用多表验证
我想验证同一页面上的两个表单,并将其插入Cakephp 3.0中的数据库。请建议我在同一页面中为不同的表使用它的完美方法 关于控制器Php 如何在同一模型中使用多表验证,php,cakephp,model,cakephp-3.0,Php,Cakephp,Model,Cakephp 3.0,我想验证同一页面上的两个表单,并将其插入Cakephp 3.0中的数据库。请建议我在同一页面中为不同的表使用它的完美方法 关于控制器 <?php namespace App\Controller; use Cake\ORM\TableRegistry; use App\Controller\AppController; class AboutController extends AppController { public function index() { $
<?php
namespace App\Controller;
use Cake\ORM\TableRegistry;
use App\Controller\AppController;
class AboutController extends AppController
{
public function index()
{
$tempsub2 = $this->Aboutt->newEntity($this->request->data);
if ($this->request->is('post')) {
$tempsub2 = $this->Aboutt->patchEntity($tempsub2, $this->request->data);
if($this->Aboutt->save($tempsub2))
{
$this->redirect(['controller'=>'Main','action' => 'index']);
}
}
$this->set(compact('tempsub2'));
$this->set('_serialize', ['tempsub2']);
$tempsub = $this->Aboutt->newEntity($this->request->data);
if ($this->request->is('post')) {
$tempsub = $this->Aboutt->patchEntity($tempsub, $this->request->data);
if($this->Aboutt->save($tempsub))
{
$this->redirect(['controller'=>'Main','action' => 'index']);
}
}
$this->set(compact('tempsub'));
$this->set('_serialize', ['tempsub']);
}
}
订阅
订阅
在处理实际保存操作之前,使用beforeSave()执行任何验证。或者根据您的代码,我理解的是,您需要在两个不同的模型上执行save,但只使用一个表单。如果是这种情况,则在代码中进行以下更改:
如果您需要关于任何部分的任何其他帮助,请务必让我知道。谢谢您,先生,我不知道ajax在cakephp 3.0中是如何工作的,我也不知道如何使用save函数来验证页面中是否有单个表单。你在你的网站上有preety gud的工作,我会从你的网站上的联系人那里给你发jst文本。如果有人能正确地引导我,那将对我很有帮助。谢谢!若答案有用,你们应该投票,或者把它标记为已回答。无论如何,我也会分享解决您Ajax需求的解决方案。
<?php
namespace App\Model\Table;
use App\Model\Entity\recommenda;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\Validation\Validator;
use Cake\ORM\Table;
class AboutTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('about');
}
public function validationDefault(Validator $validator)
{
$validator = new Validator();
$validator
->add('email', 'validFormat', ['rule' => 'email','message' => 'E-mail must be valid']);
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
return $rules;
}
}
<?php echo $this->Form->create($tempsub); ?>
<div class="col-md-9">
<div class="input-container">
<!-- <input type="text" id='email' name='email' placeholder="Write your E-mail ID and Click Subscribe Button" id="sub-2">
-->
<?php
echo $this->Form->input('email',array('type' => 'text','label'=>false,"placeholder"=>"Write your E-mail ID and Click Subscribe Button")); ?>
<button>Subscribe</button>
</div>
</div>
<?php echo $this->Form->end(); ?>
<?php echo $this->Form->create($tempsub2); ?>
<div class="col-md-9">
<div class="input-container">
<!-- <input type="text" id='email' name='email' placeholder="Write your E-mail ID and Click Subscribe Button" id="sub-2">
-->
<?php
echo $this->Form->input('email',array('type' => 'text','label'=>false,"placeholder"=>"Write your E-mail ID and Click Subscribe Button")); ?>
<button>Subscribe</button>
</div>
</div>
<?php echo $this->Form->end(); ?>