尝试用树枝渲染数据库中的实体地址 [英] Trying to render entity address from database with twig

查看:44
本文介绍了尝试用树枝渲染数据库中的实体地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个用户可以在数据库中添加多个地址.地址将在他添加地址的同一页面中呈现,然后他可以选择他想要使用的页面.问题是它不呈现任何东西,但地址保存在数据库中.

这是我的代码:

PostController.php公共函数验证Action(){}公共函数地址Action(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);如果 ($form->isValid()) {$em = $this->getDoctrine()->getManager();$entity->setUser($user);$em->persist($entity);$em->flush();返回 $this->redirect($this->generateUrl('address'));}}return $this->render('FLYBookingsBundle:Post:address.html.twig', array('user' => $user,'形式' =>$form->createView()));}

address.html.twig

 

<div class="accordion-inner">{% if user.address|length != 0 %}<div class="span4"><form action="{{ path('validation') }}" method="POST"><h4>账单地址</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 }}{% 结束为 %}<button class="btn btn-primary">确认我的帐单邮寄地址</button></表单>

{% 万一 %}<div class="span4 offset2"><h4>添加新的账单地址</h4><form action="{{ path ('address') }}" method="POST">{{ form_widget(form) }}<button class="btn btn-primary" type="submit">添加</button></表单>

用户{#124 ▼#id:21#facebook_id:空#facebook_access_token:空-命令:空-地址:空#createdAt: 日期时间 {#127 ▶}#updatedAt: 日期时间 {#126 ▶}#twoStepVerificationCode:空#dateOfBirth:空#名字:空#姓氏:空#网站:空#传记:空#性别:你"#locale: 空#时区:空#电话:空#facebookUid:空#facebook名称:空#facebookData:空#twitterUid:空#twitterName:空#twitterData:空#gplusUid:空#gplusName:空#gplusData:空#token:空#用户名:天狼星"#usernameCanonical:天狼星"#email: "XXXXXXXXXXXXXX@gmail.com"#emailCanonical:XXXXXXXXXXXX@gmail.com"#启用:真#salt:tcng810acpww4gcg480s4ko0ssw8c4k"#password: "byEnk7zLFY2Y2ya9S0Vwk+hvwHEaoyYQyVzpxrtKjZJgHWplDSgdSqvw/PnAZ8Bu973udixrNOCALbghf68d9g=="#plainPassword:空#lastLogin: 日期时间 {#128 ▶}#confirmationToken: 空#passwordRequestedAt:空#groups:PersistentCollection {#105 ▶}#locked: 假#过期:假#expiresAt: 空#角色:[]#credentialsExpired: 假#credentialsExpireAt:空}

解决方案

试试这个:

{% for data in user %}{% if data.address|length != 0 %}/.. 布拉布拉 ../{% 万一 %}{% 结束为 %}

One user can add many addresses in the database. The address will be render in the same page that he added the address then he can choose which one he would like to use. Problem is it doesn't render anything but the address is save in the database.

This is my code:

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>

EDIT:

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
}

解决方案

Try this:

{% for data in user %}
  {% if data.address|length != 0 %}
     /.. Blabla ../
  {% endif %}
{% endfor %}

这篇关于尝试用树枝渲染数据库中的实体地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆