doubian6241 2017-08-01 17:45
浏览 123
已采纳

在树枝中显示Symfony中JOIN查询的结果

I have 2 tables which are not related by ORM so I do theJOIN as follows:

$dql="SELECT u, o
      FROM BackendBundle:Orders o
      JOIN BackendBundle:Users u
      WITH o.userid=u.id";
$query=$em->createQuery($dql);

When I send the query to twig to show it on the screen it shows me an error.

This is the code in twig:

{% for ordenes in pagination %}
  <td>{{ ordenes.orderid }}</td>
  <td>{{ ordenes.orderdate|date("Y-m-d") }}</td>
{% endfor %}

And the error is this:

Neither the property "orderid" nor one of the methods "orderid()", "getorderid()"/"isorderid()" or "__call()" exist and have public access in class "BackendBundle\Entity\Users"

If I run twig this code:

{{dump(pagination)}}

It correctly shows me what the query returns:

  #currentPageNumber: 3
  #numItemsPerPage: 20
  #items: array:40 [▼
    0 => Orders {#522 ▼
      -orderid: 158
      -orderdate: DateTime {#476 ▶}
      -ordernumber: "FFFFFFFFFFF"
      -customername: "UUUUUUUUUUUU"
      -customeraddress: "OOOOOO"
      -countrycode: "US"
      -shippingaddress: "PPPPPP"
      -status: "Ordered"
      -subtotal: "5.00"
      -shippingcost: "2.82"
      -tax: "0.00"
      -discount: "0.00"
      -totalorder: "7.82"
      -shippingcarrier: "Fedex"
      -userid: "1206"
    }
    1 => Users {#525 ▼
      -id: 1206
      -username: "xxxxxxxxxxxxxx@hotmail.com"
      -yourname: null
      -firstname: "GGGG"
      -lastname: "IIIIII"
      -middlename: ""
      -address: "oooooooooo"
      -address2: ""
      -apartment: ""
      -pobox: ""
      -city: "Monsey"
      -state: "NY"
      -zipcode: "88888888"
      -country: "USA"
      -countrycode: "US"
      -email: "xxxxxxxxxxx@hotmail.com"
      -datein: DateTime {#521 ▶}
    }

My question is how can I show this result in twig ??

  • 写回答

1条回答 默认 最新

  • douguai7291 2017-08-02 04:03
    关注

    After reviewing and trying to solve this problem. Finally create the relationship in the files and between the 2 tables in this way:

    //BackendBundle\Entity\Orders:
    indexes:
        fk_order_user1_idx:
            columns:
                - user_id
    ......
    manyToOne:
        user:
            targetEntity: Users
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: null
            joinColumns:
                user_id:
                    referencedColumnName: id
            orphanRemoval: false
    

    Also in the Entity:

    //Orders.php
    /**
     * @var \BackendBundle\Entity\Users
     */
    private $user;
    ......
    /**
     * Set user
     *
     * @param \BackendBundle\Entity\Users $user
     *
     * @return Users
     */
    public function setUser(\BackendBundle\Entity\Users $user = null)
    {
        $this->user = $user;
    
        return $this;
    }
    
    /**
     * Get user
     *
     * @return \BackendBundle\Entity\Users
     */
    public function getUser()
    {
        return $this->user;
    }
    

    I updated the database:

    php bin/console doctrine:schema:update --force
    

    And I make the query in this way:

    $dql="SELECT u, o
          FROM BackendBundle:Orders o
          JOIN o.user u";
    $query=$em->createQuery($dql);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看