Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Created August 16, 2012 09:15
Show Gist options
  • Save Ocramius/3368674 to your computer and use it in GitHub Desktop.
Save Ocramius/3368674 to your computer and use it in GitHub Desktop.
Example of Criteria/Collection API usage in Doctrine 2.3
<?php
/**
* Created by JetBrains PhpStorm.
* User: Marco Pivetta
* Date: 16.08.12
* Time: 11:05
* To change this template use File | Settings | File Templates.
*/
class CriteriaTest extends \PHPUnit_Framework_TestCase
{
public function testCriteria()
{
$schedule1 = new Schedule();
$schedule1->setSchedDatetime(new \DateTime("+1 day"));
$schedule2 = new Schedule();
$schedule2->setSchedDatetime(new \DateTime("-1 day"));
$sessions = new \Doctrine\Common\Collections\ArrayCollection(array(
$schedule1,
$schedule2,
));
$eb = new \Doctrine\Common\Collections\ExpressionBuilder();
$expr = $eb->lt('schedDatetime', new \DateTime());
$criteria = new \Doctrine\Common\Collections\Criteria($expr);
$found = $sessions->matching($criteria);
$this->assertCount(1, $found);
$this->assertSame($schedule2, $found->first());
}
}
/**
* Schedule
*
* @Table(name="Schedule")
* @Entity(repositoryClass="Repositories\Schedule")
*/
class Schedule
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var integer $schedulerPsnId
*
* @Column(name="schedulerPsnId", type="integer", nullable=false)
*/
protected $schedulerPsnId;
/**
* @var datetime $schedDatetime
*
* @Column(name="schedDatetime", type="datetime", nullable=false)
*/
protected $schedDatetime;
/**
* @var integer $schTypeId
*
* @Column(name="schTypeId", type="integer", nullable=false)
*/
protected $schTypeId;
/**
* @var boolean $duration
*
* @Column(name="duration", type="boolean", nullable=false)
*/
protected $duration;
/**
* @var datetime $createdDatetime
*
* @Column(name="createdDatetime", type="datetime", nullable=false)
*/
protected $createdDatetime;
// ASSOCIATIONS
/**
* @OneToOne(targetEntity="Sessions", inversedBy="schedule")
*/
protected $session;
/**
* @ManyToOne(targetEntity="Personnel", inversedBy="scheduledSessions")
* @JoinColumn(name="schedulerPsnId", referencedColumnName="id")
*/
protected $scheduler;
// Getters and Setters
public function getId()
{
return $this->id;
}
public function getSchedulerPsnId()
{
return $this->schedulerPsnId;
}
public function setSchedulerPsnId($schedulerPsnId)
{
$this->schedulerPsnId = $schedulerPsnId;
}
public function getSchedDatetime()
{
return $this->schedDatetime;
//return \BPI_Model_Helper::gmtToLocal($this->schedDatetime);
}
public function setSchedDatetime($schedDatetime)
{
$this->schedDatetime = $schedDatetime;
}
// for the raw GMT datetime--used to make SQL queries
public function getSqlSchedDatetime()
{
return $this->schedDatetime->format('Y-m-d H:i:s');
}
public function getSchTypeId()
{
return $this->schTypeId;
}
public function setSchTypeId($schTypeId)
{
$this->schTypeId = $schTypeId;
}
public function getDuration()
{
return $this->duration;
}
public function setDuration($duration)
{
$this->duration = $duration;
}
public function getCreatedDatetime()
{
return \BPI_Model_Helper::gmtToLocal($this->createdDatetime);
}
public function setCreatedDatetime($createdDatetime)
{
$this->createdDatetime = $createdDatetime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment