一个用户可以在数据库中添加许多地址。地址将在他添加地址的同一页面中呈现,然后他可以选择他想要使用的地址。问题是除了地址保存在数据库中之外,它不会呈现任何内容。
这是我的代码:
PostController.php
public function validationAction()
{
}
public function addressAction(Request $request)
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$entity = new UserAddress();
$form = $this->createForm(new UserAddressType(), $entity);
if ($this->get('request')->getMethod() == 'POST')
{
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity->setUser($user);
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('address'));
}
}
return $this->render('FLYBookingsBundle:Post:address.html.twig', array('user' => $user,
'form' => $form->createView()));
}
address.html.twig
<div id="collapseOne" class="accordion-body collapse in">
<div class="accordion-inner">
{% if user.address|length != 0 %}
<div class="span4">
<form action="{{ path('validation') }}" method="POST">
<h4>Billing address</h4>
{% for address in user.address %}
<label class="radio">
<input type="radio" name="address" value="{{ address.id }}" {% if loop.index0 == 0 %}checked="checked"{% endif %}>
{{ address.address }}, {{ address.zipcode }} {{ address.city }} - {{ address.country }} <a href="{{ path('AddressDelete', { 'id' : address.id }) }}"><i class="icon-trash"></i></a>
<br />{{ address.surname }} {{ address.name }}
</label>
{% endfor %}
<button class="btn btn-primary">Confirm my billing address</button>
</form>
</div>
{% endif %}
<div class="span4 offset2">
<h4>Add a new billing address</h4>
<form action="{{ path ('address') }}" method="POST">
{{ form_widget(form) }}
<button class="btn btn-primary" type="submit">Add</button>
</form>
</div>
</div>
</div>
修改
User {#124 ▼
#id: 21
#facebook_id: null
#facebook_access_token: null
-commandes: null
-address: null
#createdAt: DateTime {#127 ▶}
#updatedAt: DateTime {#126 ▶}
#twoStepVerificationCode: null
#dateOfBirth: null
#firstname: null
#lastname: null
#website: null
#biography: null
#gender: "u"
#locale: null
#timezone: null
#phone: null
#facebookUid: null
#facebookName: null
#facebookData: null
#twitterUid: null
#twitterName: null
#twitterData: null
#gplusUid: null
#gplusName: null
#gplusData: null
#token: null
#username: "sirius"
#usernameCanonical: "sirius"
#email: "XXXXXXXXXXXXXX@gmail.com"
#emailCanonical: "XXXXXXXXXXXX@gmail.com"
#enabled: true
#salt: "tcng810acpww4gcg480s4ko0ssw8c4k"
#password: "byEnk7zLFY2Y2ya9S0Vwk+hvwHEaoyYQyVzpxrtKjZJgHWplDSgdSqvw/PnAZ8Bu973udixrNOCALbghf68d9g=="
#plainPassword: null
#lastLogin: DateTime {#128 ▶}
#confirmationToken: null
#passwordRequestedAt: null
#groups: PersistentCollection {#105 ▶}
#locked: false
#expired: false
#expiresAt: null
#roles: []
#credentialsExpired: false
#credentialsExpireAt: null
}
答案 0 :(得分:0)
试试这个:
{% for data in user %}
{% if data.address|length != 0 %}
/.. Blabla ../
{% endif %}
{% endfor %}