Php Symfony my Controller::updateAction()要求您为$名称“;论点
使用Symfony 2,当我尝试传递参数/变量时,出现以下错误:Php Symfony my Controller::updateAction()要求您为$名称“;论点,php,symfony,doctrine-orm,routes,updates,Php,Symfony,Doctrine Orm,Routes,Updates,使用Symfony 2,当我尝试传递参数/变量时,出现以下错误: Controller "MyBundle\Controller\ManageController::updateAction()" requires that you provide a value for the "$name" argument (because there is no default value or because there is a non optional
Controller "MyBundle\Controller\ManageController::updateAction()"
requires that you provide a value for the "$name" argument (because there is
no default value or because there is a non optional argument after this one).
$(function() {
$('#selectpicker').change(function() {
this.form.submit();
});
});
public function updateAction($name) {
$em = $this->getDoctrine()->getManager();
$parc = $em->getRepository('MySpaceDatabaseBundle:Parcs')->find($name);
$form = $this->createForm(new ParcsType(), $parc);
$request = $this->get('request');
if ($request->isMethod('POST') | ($form->isValid())) {
$form->bind($request);
$parc = $form->getData();
$em->flush();
return $this->redirect($this->generateUrl('index_parcs'));
}
//retourner le formulaire d'ajout si c'est invalide
else {
return $this->render('MySpaceManageBundle:Parcs:updateParcs.html.twig', array('form' => $form->createView(), 'parc' => $parc, 'name' => $name));
}
}
这是实体/模型地块
:
namespace MyBundle\DatabaseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Parcsimmobilier
*
* @ORM\Table(name="parcs")
* @ORM\Entity
*/
class Parcs
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=150, nullable=false)
*/
private $name;
首先,我有一个小树枝,它允许用户修改/更新一个Parc,我使用JavaScript允许html中的选择标记在另一个路由/树枝中显示一个表单当做出选择时,这是选择用户想要更新的Parc的代码:
{% block content %}
<div class="form-group">
<label class="control-label">Which Parcs would you like to update ?</label>
<form action="{{ path('UpdateParcs_form' , {'name':'parcs.name'}) }}" method="GET">
<select class="form-control" id="selectpicker">
<option disabled selected> select your parc </option>
{% for parcsimmobilier in parc %}
<option value="parc"><strong>{{ parcs.name}}</strong></option>
{% endfor %}
</select>
</form>
</div>
{% endblock %}
现在,who的细枝显示要更新所选parc的表单:
{% block content %}
<form action="{{ path('updateParcs_process') }}" method="POST">
<div>
{{ form_label(form.name, "change the name of the parc", {'label_attr': {'class': 'control-label'}}) }}
<div>
{{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<input type="submit" value="Update" class="btn btn-success"/>
</form>
{% endblock %}
这是我的路线的路线文件:
# index for management
index_Parcs:
path: /manageparcs
defaults: { _controller: MySpaceManageBundle:Manage:indexParcs }
requirements:
methods: GET
# route for display the form after the select choice
updateParcs_form:
path: /manageparcs/update/form/{name}
defaults: { _controller: MySpaceManageBundle:Manage:update }
requirements:
methods: GET
# route for processing form with update
updateParcs_process:
path: /manageparcs/update/form/success
defaults: { _controller: MySpaceManageBundle:Manage:update }
requirements:
methods: POST
我的问题是:
为什么我会有这个错误?我怎样才能修好它?
我如何使用这个updateAction()正确更新我的实体/模型。我如何在Symfony中通过路由传递参数。除了控制器中有$nom
作为参数,但路由配置中有名称
之外,您做的都是正确的事情。根据其他文件,只需更改配置即可:
将routing.yml
中的name
更改为nom
:
updateParcs_form:
path: /manageparcs/update/form/{nom}
defaults: { _controller: MySpaceManageBundle:Manage:update }
requirements:
methods: GET
第二条路由没有像第一条路由那样指定名称参数。
原因是该方法需要参数,但基于此配置,将不提供任何参数
除非要为POST请求创建另一个操作,否则需要将名称添加到POST URI
path: /manageparcs/update/form/{name}/success
您需要使用此参数生成URI 有时使用name
,有时使用nom
。不匹配…@Paziツ, 对不起,我粘贴代码时出错了。我是一名法国开发人员,我翻译我的函数和名称,以便在stackoverflow上发现问题。我将编辑我的代码。updateAction($name=null)将处理错误消息。然而,使用两条不同的路径来执行相同的操作并使用不同的参数是有点不确定的。我会用两个动作。请注意,您也可以直接将默认值添加到路由。@Cerad,我尝试了您的解决方案,但出现了以下错误:MySpace\DatabaseBundle\Entity\Parcs的查询缺少标识符id
Yep。您需要花一些时间查看find和findBy之间的差异。对不起,我在粘贴代码时犯了错误。作为一名法国开发人员,当我发布我的问题时,我会在这个网站上翻译成英语以便更好地理解。我忘记了一些字段“nom”,现在我已经更新了代码。真的很抱歉。他在POST请求中传递了name参数。@Flosculus,我不再有错误控制器“MyBundle\Controller\ManageController::updateAction()”。。。此参数之后为非可选参数)。
但是,更新不匹配。事实上,我想更改一个parc的名称,我已经按照您的建议更新了我的代码,但如果我在表格中输入文本以更改邮件,它不会在我的选择标记中更新所选parc的名称。更多信息,我只是意识到,在我的url中,当我按照您的建议更改路由文件中的代码时,我选择的名称字段的名称不是像localhost/manageparcs/update/form/nameOfMyparc
那样的,我的参数parcs.name是像localhost/manageparcs/update/form/parcs.name?
那样的。我想,我忘记了一些事情,比如select必须在我的模型/实体中选择name字段。(对不起,我有点困惑)
path: /manageparcs/update/form/{name}/success