duanqiao1926 2017-05-19 06:06
浏览 30
已采纳

如何在实体symfony中显示最后一个数组?

I don't know if this is possible in symfony, that is getting the last array when using respository. In my controller a have this function

/**
 * @param $id
 * @return Food
 */
public function getFood($id)
{

    $em = $this->getDoctrine()->getManager();
    $food = $em->getRepository("AppBundle:Food")->findById($id);
    if (!$food) {
        throw $this->createNotFoundException("Invalid Food");
    }

    return $food;
}

this will show the single food that I need. In my repository I have this code

/**
     * Display Food and get the last transaction
     * @param $id
     */
    public function findById($id){

        $query =  $this->createQueryBuilder(f)
            ->where('f.id = :id')
            ->setParameter('id', $id)
            ->getQuery()
            ->getResult()[0];

        return $query;
    }

This code successfully displaying the data of food Foods

 - id
 - name
 - price
 - packages
     - id
     - name_package
     - category
 - User
     - id
     - username
     - password
     - email
- transaction []
    1 - {
         - id
         - date
         - emailStatus
         - email
     }
    2 - {
         - id
         - date
         - emailStatus
         - email
     }
    3 - {
         - id
         - date
         - emailStatus
         - email
     }

How can I get the last transaction of food? I just want to display the last part of transaction that is like this one.

update

     - id
     - name
     - price
     - packages
         - id
         - name_package
         - category
     - User
         - id
         - username
         - password
         - email
    - transaction []
         3 - {
             - id
             - date
             - emailStatus
             - email
         }
  • 写回答

5条回答 默认 最新

  • doudiecai1572 2017-05-22 01:20
    关注

    Thanks everyone here. I have an idea now how to solve. What I did is just a simple way. In my Food entity class, I add this method

    /**
         * @return Transactions
         */
        public function getLastTransactions()
        {
            $this->transactions = $this->getTransactions()->last();
    
            return $this->transactions;
        }
    

    and then in my Food controller

     /**
         * @param $id
         * @return Food
         */
        public function getFood($id)
        {
    
            $em = $this->getDoctrine()->getManager();
            $food = $em->getRepository("AppBundle:Food")->find($id);
            $food->getLastTransactions();
    
    
            return $food;
        }
    

    I can now control the transaction and then you can see the full info of my food entity.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥30 matlab appdesigner私有函数嵌套整合