Cake PHP自动形式和多个表格

时间:2020-12-06 06:06:05

I have a form with the following fields:

我有一个包含以下字段的表单:

  • firstName
  • lastName
  • emailAddress

followed by (up to) ten name/email fields, like so

接下来是(最多)十个名称/电子邮件字段,就像这样

  • friendName-1
  • friendEmail-1
  • friendName-2
  • friendEmail-2 ... etc.
  • friendEmail-2 ......等

The firstName, lastName & emailAddress fields will be saved into a 'referrals' table, but 10 name/email pairs will go into a 'friends' table, containing a foreign key refferal_id.

firstName,lastName和emailAddress字段将保存到'referrals'表中,但10个名称/电子邮件对将进入'friends'表,其中包含外键refferal_id。

Normally I use the cakephp 'automagic' forms, and set up my validation in the model, and do a $this->model->save(); If it fails I fall back on my model validation rules and the error messages get spat back into my form html. No problems.

通常我使用cakephp'automagic'表单,并在模型中设置我的验证,然后执行$ this-> model-> save();如果它失败了,我会回到我的模型验证规则上,错误消息会回到我的表格html中。没问题。

How would I do this for TWO tables though? I'm thinking that the automagic stuff is not the way to go, but I feel I will lose a lot without it, such as validation in my models and custom error messages.

我怎么能为TWO表做这个呢?我认为自动化的东西不是可行的方法,但我觉得没有它会失去很多,例如模型中的验证和自定义错误消息。

Is there a happy medium? I'm fairly new to cake so I'm wondering if theres a way to use automagic with multiple tables.

有幸福的媒介吗?我很擅长蛋糕,所以我想知道是否有一种方法可以使用多个表格的自动化。

2 个解决方案

#1


See http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

Create the relationship in your Referral model:

在推荐模型中创建关系:

var $hasMany = array('Friend');

Essentially, make sure you name your fields correctly, e.g.

基本上,请确保正确命名您的字段,例如

echo $form->create('Referral');
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
foreach (range(0,9) as $index) {
  echo $form->input('Friend.'.$index.'.name');
  echo $form->input('Friend.'.$index.'.email');
}
echo $form->end();

And in your referrals controller:

在您的推荐控制器中:

function add() {
   if(!empty($this->data)) {
      $this->Referral->saveAll($this->data, array('validate'=>'first'));
   }
}

#2


First, I really hope that you don't have 20 fields in table called 'friendName-1', 'friendEmail-1', etc. Your second table should have 3 fields--your referral_id, friendname, and friendemail. If you don't want to normalize, then you don't need the second table.

首先,我真的希望你的表中没有20个字段叫做'friendName-1','friendEmail-1'等。你的第二个表应该有3个字段 - 你的referral_id,friendname和friendemail。如果您不想标准化,那么您不需要第二个表。

But I digress, the way to keep your automagic forms is to put a "hasMany" in your referrals model and/or a "belongsTo" in your "friends" model. Then you can still use the FormHelper and Model->Save().

但是我离题了,保持自动化形式的方法是在你的“朋友”模型中将“hasMany”放在你的推荐模型和/或“belongsTo”中。然后你仍然可以使用FormHelper和Model-> Save()。

#1


See http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

Create the relationship in your Referral model:

在推荐模型中创建关系:

var $hasMany = array('Friend');

Essentially, make sure you name your fields correctly, e.g.

基本上,请确保正确命名您的字段,例如

echo $form->create('Referral');
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
foreach (range(0,9) as $index) {
  echo $form->input('Friend.'.$index.'.name');
  echo $form->input('Friend.'.$index.'.email');
}
echo $form->end();

And in your referrals controller:

在您的推荐控制器中:

function add() {
   if(!empty($this->data)) {
      $this->Referral->saveAll($this->data, array('validate'=>'first'));
   }
}

#2


First, I really hope that you don't have 20 fields in table called 'friendName-1', 'friendEmail-1', etc. Your second table should have 3 fields--your referral_id, friendname, and friendemail. If you don't want to normalize, then you don't need the second table.

首先,我真的希望你的表中没有20个字段叫做'friendName-1','friendEmail-1'等。你的第二个表应该有3个字段 - 你的referral_id,friendname和friendemail。如果您不想标准化,那么您不需要第二个表。

But I digress, the way to keep your automagic forms is to put a "hasMany" in your referrals model and/or a "belongsTo" in your "friends" model. Then you can still use the FormHelper and Model->Save().

但是我离题了,保持自动化形式的方法是在你的“朋友”模型中将“hasMany”放在你的推荐模型和/或“belongsTo”中。然后你仍然可以使用FormHelper和Model-> Save()。