2016-08-16 6 views
2

Doctrineで特定のタイプのアイテムをすべて取得する必要があります。ここでは、 'date'フィールドはDateTimeとして保存されています。つまり、私はPHP DateTimeオブジェクトも扱っています。SymfonyとDoctrineを使って月別にアイテムを取得する

私は全体的に2つのことを行う必要があります:月と年を指定した場合、その月と年のすべてのレコードを取得します。月と年が指定されていない場合は、今月のすべてのレコードを取得します。

私はDoctrineクエリビルダでBETWEEN文を使用してみましたが、DateTimeの時間部分に「今」というように作成されたPHP DateTimeオブジェクトから月と年をきれいに抽出する方法を理解できません。クエリに影響しません。また、月と年に影響を与えずに、DateTimeのDateの日の部分を01に設定する方法もありません。

これはかなり標準的な作業だと思っていましたが、解決策が見つかりませんでした。どんな助けもありがたい。

EDIT:

間の日付を取得するためのハック方法を見つけましたが、Doctrineは空の配列を返しています。次のコードを使用してください

/** 
* Returns feed for month and year 
*/ 
public function getMonthYearFeed($month, $year) 
{ 
// Create two times at the start of this month and next month 
$startDate = \DateTime::createFromFormat('d-n-Y', "01-".$month."-".$year); 
$startDate->setTime(0, 0 ,0); 

$endDate = \DateTime::createFromFormat('d-n-Y', "01-".($month+1)."-".$year); 
$endDate->setTime(0, 0, 0); 

$notes = $this->em->getRepository('AppBundle:Note')->createQueryBuilder('n')->where('n BETWEEN :start AND :end')->setParameter('start', $startDate->format('Y-m-d H:i:s'))->setParameter('end', $endDate->format('Y-m-d H:i:s'))->getQuery()->getResult(); 

return $notes; 
} 
+2

あなたが試したことを示すコードを提供してください。 –

答えて

3

このようなものをリポジトリに配置すると、開始する必要があります。私はそれを正常に動作するように思われる'last day of this month'ビットを除いてテストしていない。

/** 
* @param int $month 
* @param int $year 
* 
* @return object[] 
*/ 
public function findByDate($year = null, $month = null) 
{ 
    if ($month === null) { 
     $month = (int) date('m'); 
    } 

    if ($year === null) { 
     $year = (int) date('Y'); 
    } 


    $startDate = new \DateTimeImmutable("$year-$month-01T00:00:00"); 
    $endDate = $startDate->modify('last day of this month')->setTime(23, 59, 59); 

    $qb = $this->createQueryBuilder('object'); 
    $qb->where('object.date BETWEEN :start AND :end'); 
    $qb->setParameter('start', $startDate->format('‌​Y-m-d H:i:s')); 
    $qb->setParameter('end', $endDate->format('‌​Y-m-d H:i:s')); 

    return $qb->getQuery()->getResult(); 
} 
+0

DateTime modifyはオブジェクトを変更し、$ startDateと$ endDateが同じになるように新しいインスタンスを作成しません。 '$ endDate =(クローン$ startDate) - >変更( '今月の最後の日')はうまくいくはずです。または、2つ目のインスタンスを作成します。 – Joe

+0

コード使用時にこれらのエラーが発生しました。 'n0_.id AS AS0、n0_.slug AS slug1、n0_.content AS content2、n0_.timestamp AS timestamp3、n0_.date AS date4、n0_.in_reply_to AS in_reply_to5 FROM note n0_どこのn0_.dateの間? AND? ' params [{"date": "2016-08-01 00:00:00.000000"、 "timezone_type":3、 "timezone": "Europe \/London"}、{"date": "2016-08-31 キャッチ可能な致命的なエラー:DateTimeImmutableクラスのオブジェクトを文字列に変換できませんでした – Mrshll1001

+0

'$ qb-> setParameterを試してください。 ( 'start'、$ startDate-> format( 'Ymd H:i:s')); 'endDateの場合と同じです。 – Joe

-1

私は間違いなく日付を構成する方法を変更する必要がありますが、これは機能しているようです(機能をレポに移動する)。もともとDQLの '.date'ビットを忘れていたので、DateTimeオブジェクトをフォーマットとして出力する必要はありませんでした。

// Create two times at the start of this month and next month 
$startDate = \DateTime::createFromFormat('d-n-Y', "01-".$month."-".$year); 
$startDate->setTime(0, 0 ,0); 

$endDate = \DateTime::createFromFormat('d-n-Y', "01-".($month+1)."-".$year); 
$endDate->setTime(0, 0, 0); 

$notes = $this->em->getRepository('AppBundle:Note')->createQueryBuilder('n')->where('n.date BETWEEN :start AND :end')->setParameter('start', $startDate)->setParameter('end', $endDate)->getQuery()->getResult(); 

// $notes = $this->em->getRepository('MrshllSiteBundle:Note')->findByDate(); 
return $notes; 
0

非常にimportand瞬間。 setParameterのデータ型を使用します。

このようにsymfonyを使用してください。

use Doctrine\DBAL\Types\Type;

$query->setParameter('start', $startDate, Type::DATETIME);

関連する問題