私は、これはあなたを助けますことを願っています。 私はあなたの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;
}
}
おかげで、我々はのcontrolerにそれを得るためのコマンドであることをOKです:$ lastWeight =する$ user-> getPerformance() - > getLastWeight(); ?それが言うので :クラスの「getLastWeight」「教義\ ORM \ PersistentCollection」という名前の未定義のメソッドを呼び出そうとしました。 – user6533866
私の回答を編集して私のコントローラでリポジトリメソッドをどのように使用するかを示しました – Mz1907
これで、このエラーが表示されます:メンバ関数orderBy()を呼び出して文字列を返します 日付フィールドは、 – user6533866