2016-10-15 2 views
-3

私はパフォーマンスモニタリング用のウェブサイトを作成しています。条件付きエンティティフィールドの最後の値を取得します。

私は2つのテーブルがあります:ユーザーテーブルと多対1でリンク

  • パフォーマンステーブル

  • 公演と1対多数でリンク

    • Usersテーブルを

      私はちょうど最後の体重を取得したいはnullではない私のテーブルのパフォーマンスとtwigでそれを表示

      このデータベースのexempleについては

      database screenshot 、結果は次のようになります。80

      私は、クエリを試みたが、私はあなたの助けを事前に 感謝を行う方法がわからないので、私はarrorメッセージが表示されます!

    答えて

    0

    私は、これはあなたを助けますことを願っています。 私はあなたのPerformanceエンティティ用のリポジトリクラスを持っているとします。私はSymfony3アプリケーション

    <?php 
    
    namespace AppBundle\Repository; //replace AppBundle by the name of your bundle 
    
    use Doctrine\ORM\Tools\Pagination\Paginator; 
    
    /** 
    * PerformanceRepository 
    * 
    * This class was generated by the Doctrine ORM. Add your own custom 
    * repository methods below. 
    */ 
    class PerformanceRepository extends \Doctrine\ORM\EntityRepository 
    { 
    
        public function getLastWeigth() 
        { 
         $qb = $this->getEntityManager()->createQueryBuilder() 
          ->select('p') 
          ->from($this->_entityName, 'p') 
          ->expr()->isNotNull('p.weight') 
          ->orderBy('p.date', 'desc') 
          ->setMaxResults(1); 
         $query = $qb->getQuery(); 
         $result = $query->getSingleResult(); 
    
         return $result; 
        } 
    } 
    

    編集のためにこの種のコードを使用します。

    <?php 
    namespace AppBundle\Controller\Performance; 
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
    use AppBundle\Repository\PerformanceRepository; 
    
    class PerformanceController extends Controller 
    { 
    
        public function lastWeightAction() 
        { 
         $repo = $this->getDoctrine()->getManager()->getRepository('AppBundle:Performance'); 
         $lastPerformance = $repo->getLastWeigth(); 
    
         //some other code 
        } 
    } 
    

    EDIT2:ここにsymfonyの3アプリケーションのコントローラでの使用のExempleある あなたが取得する必要がある場合ユーザーIDによる最後の重量:あなたの答えのための

    class PerformanceRepository extends \Doctrine\ORM\EntityRepository 
    { 
    
        public function getLastWeigth($userId) 
        { 
         $qb = $this->getEntityManager()->createQueryBuilder() 
          ->select('p') 
          ->from($this->_entityName, 'p') 
          ->join('p.user', 'u') 
          ->where('u.id = :userId') 
          ->expr()->isNotNull('p.weight') 
          ->orderBy('p.date', 'desc') 
          ->setMaxResults(1); 
          ->setParameter(':userId', $userId); 
         $query = $qb->getQuery(); 
         $result = $query->getSingleResult(); 
    
         return $result; 
        } 
    } 
    
    +0

    おかげで、我々はのcontrolerにそれを得るためのコマンドであることをOKです:$ lastWeight =する$ user-> getPerformance() - > getLastWeight(); ?それが言うので :クラスの「getLastWeight」「教義\ ORM \ PersistentCollection」という名前の未定義のメソッドを呼び出そうとしました。 – user6533866

    +0

    私の回答を編集して私のコントローラでリポジトリメソッドをどのように使用するかを示しました – Mz1907

    +0

    これで、このエラーが表示されます:メンバ関数orderBy()を呼び出して文字列を返します 日付フィールドは、 – user6533866

    関連する問題