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条)

报告相同问题?

悬赏问题

  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了