cakephp添加记录一些参数固定 [英] cakephp adding record with some parameters fixed

查看:109
本文介绍了cakephp添加记录一些参数固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这种问题必须在cakephp(我最近开始使用)中很常见,但我没有找到一个明确的答案。

I'm sure this sort of problem must be common in cakephp (which I've recently started using), but I haven't managed to find a clear answer.

在我的数据库中,我有一个表,称为客户和联系人,在一对多的关系(客户有多个联系人;联系人属于客户)。当我向联系人表(/ contacts / add)添加记录时,我可以从包含数据库中所有客户的选择框中选择客户(customer_id)。如何设置它,以便我可以选择客户(/ customers / view / 6),然后为该特定客户添加联系人(例如/ contacts / add / 6);然后从添加联系人表单中删除选择框(也许用替换为隐藏的customer_id字段)?

In my database I have, among others, tables called customers and contacts, in a one-to-many relationship (Customer hasMany Contact; Contact belongsTo Customer). When I add a record to the contacts table (/contacts/add), I can choose the customer (customer_id) from a select box containing all the customers in the database. How can I set it up so that I can choose the customer first (/customers/view/6), then add a contact for that specific customer (e.g. /contacts/add/6); and then remove the select box from the "add contact" form (maybe replacing it with a hidden customer_id field)?

推荐答案

有几种方法可以做到这一点,但我认为最好的方法是使用命名参数

There are several ways to do this, but I think the best is using the named parameters.

基本上,在您的views / customers / view.ctp中,向联系人/添加链接添加customer_id:

Essentially, in your views/customers/view.ctp, you add a customer_id to the contacts/add link:

$html->link(__('Add contact', true), array('controller' => 'contacts', 'action' => 'add', 'customer_id' => $customer['Customer']['id']));

并在您的views / contacts / add.ctp中检查命名参数并使用隐藏字段:

and in your views/contacts/add.ctp you check for the named parameter and use a hidden field:

if (isset($this->params['named']['customer_id'])) {
    echo $form->input('customer_id', array('type' => 'hidden', 'value' => $this->params['named']['customer_id']));
} else {
    echo $form->input('customer_id');
}

或已选择正确客户的选择:

or a select with the right customer already selected:

echo $form->input('customer_id', array('selected' => @$this->params['named']['customer_id']));

这篇关于cakephp添加记录一些参数固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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