CakePHP获取表单值并检查数据库(如果存在)

时间:2014-02-07 03:26:06

标签: php mysql cakephp model controller

我想从帖子中获取数据,然后在另一个模型中查看 这是我的模特

table Goods :
id,
goods_plu

table Transaction :
id,
transaction_plu

如果我想添加交易并获取plu,则检查是否

if($trans_plu == $goods_plu )

如果货物表中存在trans_plu 数据将保存在数据库中

这是我的表格

<?php echo $this->Form->create('Transaction'); ?>
   <input type="text" name="trans_plu">
   <?php echo $this->Form->button('Save'); ?>
<?php echo $this->Form->end(); ?>

感谢提前!

1 个答案:

答案 0 :(得分:0)

您在事务控制器中进行检查,您可以查看trans_plu是否在货物表中。

class TransactionsController extends AppController{
   public $uses = array('Transaction', 'Goods');
   public function add(){
      if(!empty($this->data){
         $goodsCount = $this->Goods->read('goods_plu', $this->data['Transaction']['trans_plu']);
         if(!is_null($goodsCount)){
            $this->Transaction->save($this->data);
         }
      }
   }
}

让我知道它是怎么回事