CakePHP 2.3:在一个页面中一次性保存多个表中的多个表单 [英] CakePHP 2.3: Save multiple forms in multiple tables all at once in 1 page

查看:140
本文介绍了CakePHP 2.3:在一个页面中一次性保存多个表中的多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CakePHP 2.3设置商业投标系统。



它有5个表相关联(见下面的细节)。
此系统将有1个页面,每个表格上有5个表单。 (见下面的详细信息)。



表单1:表格提议

表格2:表格客户FK proposal_idogle
Form 3:表格产品FK client_id

表格4:表格规格FK product_id(产品的)_
表格5:表格附录FK product_id(产品的)



WHERE:

方案 hasOne 客户。 b ---------- v ---------- $
个客户 beongsTo 提案和有多个产品。$
---------- v ----------

products belongsTo 客户端AND hasMany 规格&附件。

---------- v ----------ETARY
规格属于产品。

appendices belongsTo 产品。



表单4可以有多行,因此它位于foreach循环生成更多输入。因为1个产品可以有多个规格和附录。



我承认,这很棘手:)



这是我最终选择的解决方案,但也许有更好的。例如,我可以做一个多步骤形式,但它似乎很难设置到位。无论如何,我可以建议。



同时,我相信我们可以做这个工作,这里是细节。对于每个表,我创建了一个模型并进行了适当的连接。在每个模型中,我启用了可包含的行为: public $ actsAs = array('Containable');



现在这里是ProposalsController.php:

  function admin_edit($ id = null){
$ this-> loadModel('Proposal','Client','Product','Specification','Appendice');
$ this-> Proposal-> contains('Proposal','Client','Product','Specification','Appendice');

if($ this-> request-> is('put')|| $ this-> request-> is('Post')){
$ data = $ this-> request-> data;
if(!empty($ data)){
//使用以下命令来避免验证错误:
// unset($ this-> Proposal-> Client-> validate ['proposal_id']);
// $ this-> Proposal-> saveAssociated($ data);
// $ this-> Session-> setFlash(Le contenu a bienétéédité);
// $ this-> redirect(array('action'=>'index',$ data ['Product'] ['id']))
debug($ data);
}
}
elseif($ id){
$ this-> Proposal-> id = $ id;
$ this-> request-> data = $ this-> Proposal-> read(null,$ id);
}
}

此编辑操作有许多错误,因为我不知道如何在这种情况下正确设置它。所以原谅我,但我给你一个原始代码:)。我想要这个admin_function来添加或编辑,但是在这种情况下,我不知道如何处理它。



而且视图admin_edit.ctp:

 < hr /> 
< h1>命题< / h1>
< hr />

<?php echo $ this-> Form-> create('Product'); ?>

<?php echo $ this-> Form-> input('Proposal.name',array('label'=>Nom de la proposition)); ?>
<?php echo $ this-> Form-> input('Proposal.created',array('label'=>Date decréation)); ?>
<?php echo $ this-> Form-> input('Proposal.due',array('label'=>Date d'échéance)); ?>
<?php echo $ this-> Form-> input('Proposal.content',array('label'=>Termes& conditions)) ?>

< hr />
< h1>客户< / h1>
< hr />

<?php echo $ this-> Form-> input('Client.name',array('label'=>Nom du client)); ?>
<?php echo $ this-> Form-> input('Client.project',array('label'=>Nom du projet)); ?>
<?php echo $ this-> Form-> input('Client.address',array('label'=>Adresse)) ?>
<?php echo $ this-> Form-> input('Client.phone',array('label'=>Téléphone)); ?>
<?php echo $ this-> Form-> input('Client.email',array('label'=>Email)) ?>
<?php echo $ this-> Form-> hidden('Client.proposal_id'); ?>

< hr />
< h1>产品< / h1>
< hr />

<?php echo $ this-> Form-> input('Product.0.name',array('label'=>Nom du produit)) ?>
<?php echo $ this-> Form-> input('Product.0.reference',array('label'=>Référence)); ?>
<?php echo $ this-> Form-> input('Product.0.image',array('label'=>Image)) ?>
<?php echo $ this-> Form-> input('Product.0.pu',array('label'=>Prix Unitaire)); ?>
<?php echo $ this-> Form-> input('Product.0.quantity',array('label'=>Quantité)); ?>
<?php echo $ this-> Form-> input('Product.0.ce',array('label'=>CE)) ?>
<?php echo $ this-> Form-> input('Product.0.nf',array('label'=>NF)) ?>
<?php echo $ this-> Form-> input('Product.0.rohs',array('label'=>RoHS)) ?>
<?php echo $ this-> Form-> hidden('Product.0.client_id'); ?>

< hr />
< h1>条件:< / h1>
< hr />

<?php
foreach(range(0,1)as $ i):
$ a = $ i + 1;
echo'< p>Spécification'。$ a。'< / p>';
echo $ this-> Form-> input('Specification。'。$ i。'name',array('label'=>Nom de laspécification));
echo $ this-> Form-> input('Specification。'。$ i。'value',array('label'=>Valeur de laspécification));
echo $ this-> Form-> hidden('Specification。'。$ i。'。product_id');
endforeach;
?>

< hr />
< h1>附件:< / h1>
< hr />

<?php
foreach(range(0,1)as $ j):
$ b = $ j + 1;
echo'< p> Annexe'。$ b。'< / p>';
echo $ this-> Form-> input('Appendice。'。$ j。'name',array('label'=>Image))
echo $ this-> Form-> input('Appendice。'。$ j。'。content',array('label'=>Description de l'annexe));
echo $ this-> Form-> hidden('Appendice。'。$ j。'product_id');
endforeach;
?>

<?php echo $ this-> Form-> end('valider'); ?>

最后,这里是debug($ data)的数组结果:

  array(
'Proposal'=> array(
'name'=>'命题3',
'created'=> array(
'month'=>'02',
'day'=>'19',
'year'=>'2013 ',
'hour'=>'08',
'min'=>'27',
'meridian'=>'am'
),
'due'=> array(
'month'=>'02',
'day'=>'19',
'year'=> 2013',
'hour'=>'08',
'min'=>'27',
'meridian'=&
'content'=>'Termes& conditions'
),
'Client'=> array(
'name'=>'Client 3',
'project'=>'Projet Client 3',
'address'=>'Adresse Client 3',
'phone'=> 'TéléphoneClient 3',
'email'=> 'Email@test.com',
'proposal_id'=> ''
),
'Product'=> array(
(int)0 => array(
'name'=>'Produit 3',
'reference'=>'RéférenceProduit 3',
'image'=>'Image Produit 3',
'pu'=>'100.77',
'quantity'=>'1000',
'ce'=> '1',
'nf'=>'0',
'rohs'=>'0',
'client_id'=>''

),
'Specification'=> array(
(int)0 => array(
'name'=>'Specification 1',
'value'=>'Valeur 1',
' product_id'=>''
),
(int)1 => array(
'name'=>'Specification 2',
'value'=> ;'Valeur 2',
'product_id'=>'

),
'Appendice'=> array(
(int)0 => array(
'name'=>'Image annexe 1',
'content'=>'Description de l'annexe',
'product_id'=>''
),
(int)1 => array(
'name'=>'Image annexe 2',
'content'=>'Description de l'annexe',
'product_id'=>''



所以在这个数组中,一切都在那里。我只需要获取这些数据并将其保存在相应的表中。第一个问题是使用表的关联来管理。然后是生成多行的2个foreach循环。



我想立即保存它,但它是可能的吗?



尝试了很多方法让它工作后,我完全疯了,我只是失去了自己。我真的绝望那一个,我混合了保存方法和request->数据之间的所有内容,包含的一切,但不能得到它保存。无论如何,我迫切需要帮助!



非常感谢您提供的所有帮助,我真的很感激!








我将我的模型添加到当前帖子以获取更多详细信息。



Proposal.php:

 <?php 
class Proposal extends AppModel {

public $ actsAs = array('Containable');

public $ validate = array(
'name'=> array(
'rule'=>'notEmpty',
'message'=> Veuillezpréciserun titre

);

public $ hasOne = array(
'Client'=> array(
'className'=>'Client',
'foreignKey'=> 'proposal_id',
'dependent'=> true

);
}

Client.php: b
$ b

 <?php 
class Client extends AppModel {

public $ actsAs = array('Containable') ;

public $ belongsTo = array(
'Proposal'=> array(
'className'=>'Proposal',
'foreignKey'=& 'proposal_id'

);

public $ hasMany = array(
'Product'=> array(
'className'=>'Product',
'foreignKey'=> 'client_id',
'conditions'=>'',
'order'=>'',
'limit'=>'',
' => true

);
}

Product.php: b
$ b

 <?php 
class Product extends AppModel {

public $ actsAs = array('Containable') ;

public $ belongsTo = array(
'Client'=> array(
'className'=>'Client',
'foreignKey'=> 'client_id'

);

public $ hasMany = array(
'Specification'=> array(
'className'=>'Specification',
'foreignKey'=> 'product_id',
'conditions'=>'',
'order'=>'',
'limit'=>'',
' => true
),
'Appendice'=> array(
'className'=>'Appendice',
'foreignKey'=>'product_id'
'conditions'=>'',
'order'=>'',
'limit'=>'',
'dependent'=> true

);
}

Specification.php: b
$ b

 <?php 
类规范extends AppModel {

public $ actsAs = array('Containable') ;

public $ belongsTo = array(
'Product'=> array(
'className'=>'Product',
'foreignKey'=> 'product_id'

);
}

Appendice.php: b
$ b

 <?php 
class Appendice extends AppModel {

public $ actsAs = array('Containable') ;

public $ belongsTo = array(
'ProductAppend'=> array(
'className'=>'Product',
'foreignKey'=> 'product_id'

);
}


解决方案

数据?

  if(!empty($ this-> data)){
$ this-& Proposal-> save($ this-> data);
}

您还应该检查sql-output


echo $ this-> element('sql_dump');


strong>编辑:



在编辑之后,首先看来你的关系错了。
查看本书中的示例: http ://book.cakephp.org/2.0/en/models/associations-linking-models-together.html



例如hasOne:the other model包含外键。



如前所述,您应该尝试从一个关系开始,让它工作。后来,它会更容易得到另一个工作。因此,尝试注释掉除您的提案和客户端模型之外的一切。然后尝试保存这两个。


I'm trying to set up a commercial proposal system using CakePHP 2.3.

It has 5 tables which are associated (see details below). This system will have 1 page on which are 5 forms for each table. (see details below).

Form 1: table proposals
Form 2: table clients FK proposal_id
Form 3: table products FK client_id
Form 4: table specifications FK product_id (of the product)
Form 5: table appendices FK product_id (of the product)

WHERE:

proposals hasOne clients.
----------v----------
clients beongsTo proposals AND hasMany products.
----------v----------
products belongsTo clients AND hasMany specifications & appendices.
----------v----------
specifications belongsTo products.
appendices belongsTo products.

The form 4 can have multiple row so it's inside a foreach loop to generate more input. Same for the form 5. Because 1 product can have multiple specifications and appendices.

I admit, it's pretty tricky :)

This is the solution I finally chose, but maybe there is better. For exemple, I could make a multi-step form but it seems quite difficult to put in place. Anyway, i'm open to suggestion.

Meanwhile, I'm sure we can make this work so here are the details. For each table, I have created a model and made the appropriated connections. In each model I have enable the containable behaviour: public $actsAs = array('Containable');.

Now here is the ProposalsController.php:

function admin_edit($id=null){
        $this->loadModel('Proposal','Client','Product', 'Specification', 'Appendice');
        $this->Proposal->contain('Proposal','Client','Product', 'Specification', 'Appendice');

        if ($this->request->is('put') || $this->request->is('Post')) {
            $data = $this->request->data;
            if (!empty($data)) {
                // Use the following to avoid validation errors:
                // unset($this->Proposal->Client->validate['proposal_id']);
                // $this->Proposal->saveAssociated($data);
                // $this->Session->setFlash("Le contenu a bien été édité");
                // $this->redirect(array('action' => 'index', $data['Product']['id']));
                debug($data);
            }
        }
        elseif($id){
            $this->Proposal->id = $id;
            $this->request->data = $this->Proposal->read(null,$id);
        }
    }

This edit action has many errors because I don't know how to set it up correctly in that case. So forgive me but I give you a raw code :). I would like this admin_function to add or edit, but again, in this case I don't know how to handle it.

And the view admin_edit.ctp :

<hr/>
<h1>Proposition</h1>
<hr/>

<?php echo $this->Form->create('Product'); ?>

<?php echo $this->Form->input('Proposal.name', array('label' => "Nom de la proposition")); ?>
<?php echo $this->Form->input('Proposal.created', array('label' => "Date de création")); ?>
<?php echo $this->Form->input('Proposal.due', array('label' => "Date d'échéance")); ?>
<?php echo $this->Form->input('Proposal.content', array('label' => "Termes & conditions")); ?>

<hr/>
<h1>Client</h1>
<hr/>

<?php echo $this->Form->input('Client.name', array('label' => "Nom du client")); ?>
<?php echo $this->Form->input('Client.project', array('label' => "Nom du projet")); ?>
<?php echo $this->Form->input('Client.address', array('label' => "Adresse")); ?>
<?php echo $this->Form->input('Client.phone', array('label' => "Téléphone")); ?>
<?php echo $this->Form->input('Client.email', array('label' => "Email")); ?>
<?php echo $this->Form->hidden('Client.proposal_id'); ?>

<hr/>
<h1>Produit</h1>
<hr/>

<?php echo $this->Form->input('Product.0.name', array('label' => "Nom du produit")); ?>
<?php echo $this->Form->input('Product.0.reference', array('label' => "Référence")); ?>
<?php echo $this->Form->input('Product.0.image', array('label' => "Image")); ?>
<?php echo $this->Form->input('Product.0.pu', array('label' => "Prix Unitaire")); ?>
<?php echo $this->Form->input('Product.0.quantity', array('label' => "Quantité")); ?>
<?php echo $this->Form->input('Product.0.ce', array('label' => "CE")); ?>
<?php echo $this->Form->input('Product.0.nf', array('label' => "NF")); ?>
<?php echo $this->Form->input('Product.0.rohs', array('label' => "RoHS")); ?>
<?php echo $this->Form->hidden('Product.0.client_id'); ?>

<hr/>
<h1>Spécifications :</h1>
<hr/>

<?php
foreach (range(0,1) as $i):
    $a=$i+1;
    echo '<p>Spécification '.$a.'</p>';
    echo $this->Form->input('Specification.'.$i.'.name', array('label' => "Nom de la spécification"));
    echo $this->Form->input('Specification.'.$i.'.value', array('label' => "Valeur de la spécification"));
    echo $this->Form->hidden('Specification.'.$i.'.product_id');
endforeach;
?>

<hr/>
<h1>Annexes :</h1>
<hr/>

<?php
foreach (range(0,1) as $j):
    $b=$j+1;
    echo '<p>Annexe '.$b.'</p>';
    echo $this->Form->input('Appendice.'.$j.'.name', array('label' => "Image"));
    echo $this->Form->input('Appendice.'.$j.'.content', array('label' => "Description de l'annexe"));
    echo $this->Form->hidden('Appendice.'.$j.'.product_id');
endforeach;
?>

<?php echo $this->Form->end('valider'); ?>

And finally, here is the array result of the debug($data):

array(
    'Proposal' => array(
        'name' => 'Proposition 3',
        'created' => array(
            'month' => '02',
            'day' => '19',
            'year' => '2013',
            'hour' => '08',
            'min' => '27',
            'meridian' => 'am'
        ),
        'due' => array(
            'month' => '02',
            'day' => '19',
            'year' => '2013',
            'hour' => '08',
            'min' => '27',
            'meridian' => 'am'
        ),
        'content' => 'Termes & conditions'
    ),
    'Client' => array(
        'name' => 'Client 3',
        'project' => 'Projet Client 3',
        'address' => 'Adresse Client 3',
        'phone' => 'Téléphone Client 3',
        'email' => 'Email@test.com',
        'proposal_id' => ''
    ),
    'Product' => array(
        (int) 0 => array(
            'name' => 'Produit 3',
            'reference' => 'Référence Produit 3',
            'image' => 'Image Produit 3',
            'pu' => '100.77',
            'quantity' => '1000',
            'ce' => '1',
            'nf' => '0',
            'rohs' => '0',
            'client_id' => ''
        )
    ),
    'Specification' => array(
        (int) 0 => array(
            'name' => 'Specification 1',
            'value' => 'Valeur 1',
            'product_id' => ''
        ),
        (int) 1 => array(
            'name' => 'Specification 2',
            'value' => 'Valeur 2',
            'product_id' => ''
        )
    ),
    'Appendice' => array(
        (int) 0 => array(
            'name' => 'Image annexe 1',
            'content' => 'Description de l'annexe',
            'product_id' => ''
        ),
        (int) 1 => array(
            'name' => 'Image annexe 2',
            'content' => 'Description de l'annexe',
            'product_id' => ''
        )
    )
)

So in this array, everything is there. I just need to take these data and save them all in the corresponding tables. The first problem is to manage with the table's associations. Then the 2 foreach loops that generate multiple row.

I would like to save it all at once but is it even possible?

After trying many ways to get it work, I've gone completely crazy and I just lost myself. I'm really desperate on that one, i mix up everything between the saving method and the request->data that contains everything but cannot get it saved. Anyway, i'm desperately in need for help!

Thank you very much in advance for all the help you can give me, I really appreciate!


[EDIT]

I add my model to the present post for more detail.

Proposal.php:

<?php
class Proposal extends AppModel {

    public $actsAs = array('Containable');

    public $validate = array(
        'name' => array(
            'rule'       => 'notEmpty',
            'message'    => "Veuillez préciser un titre"
        )
    ) ;

    public $hasOne = array(
        'Client' => array(
            'className'    => 'Client',
            'foreignKey'   => 'proposal_id',
            'dependent'    => true
            )
        );
}

Client.php:

<?php
class Client extends AppModel {

    public $actsAs = array('Containable');

    public $belongsTo = array(
        'Proposal' => array(
            'className'    => 'Proposal',
            'foreignKey'   => 'proposal_id'
            )
        );

    public $hasMany = array(
        'Product' => array(
            'className'     => 'Product',
            'foreignKey'    => 'client_id',
            'conditions'    => '',
            'order'         => '',
            'limit'         => '',
            'dependent'     => true
            )
        );
}

Product.php:

<?php
class Product extends AppModel {

    public $actsAs = array('Containable');

    public $belongsTo = array(
        'Client' => array(
            'className'    => 'Client',
            'foreignKey'   => 'client_id'
            )
        );

    public $hasMany = array(
        'Specification' => array(
            'className'     => 'Specification',
            'foreignKey'    => 'product_id',
            'conditions'    => '',
            'order'         => '',
            'limit'         => '',
            'dependent'     => true
            ),
        'Appendice' => array(
            'className'     => 'Appendice',
            'foreignKey'    => 'product_id',
            'conditions'    => '',
            'order'         => '',
            'limit'         => '',
            'dependent'     => true
            )
        );
}

Specification.php:

<?php
class Specification extends AppModel {

    public $actsAs = array('Containable');

    public $belongsTo = array(
        'Product' => array(
            'className'    => 'Product',
            'foreignKey'   => 'product_id'
            )
        );
}

Appendice.php:

<?php
class Appendice extends AppModel {

    public $actsAs = array('Containable');

    public $belongsTo = array(
        'ProductAppend' => array(
            'className'    => 'Product',
            'foreignKey'   => 'product_id'
            )
        );
}

解决方案

What's the output when you save the data?

 if (!empty($this->data)) {
               $this->Proposal->save($this->data);
}

You should also check the sql-output

echo $this->element('sql_dump');

edit:

Well after your edit, on the first look it seems that your relations are wrong. Check out the examples in the book: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

For example hasOne: the other model contains the foreign key.

As I said earlier, you should try to start with just one relation, getting it to work. Afterwords, it'll be much easier to get the other working. So try to comment-out everything except your Proposal and Client model. Then try saving just those two.

这篇关于CakePHP 2.3:在一个页面中一次性保存多个表中的多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆