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

报告相同问题?