Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- File bootstrapDoctrineTest.php
- */
- $autoloader= require_once('vendor/autoload.php');
- use Doctrine\ORM\Configuration;
- use Doctrine\ORM\Tools\Setup;
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
- use Doctrine\Common\Annotations\AnnotationReader;
- use Doctrine\Common\Annotations\AnnotationRegistry;
- $paths= array(
- realpath(__DIR__.'/src/DoctrineTest')
- );
- $config = Setup::createConfiguration($isDevMode= TRUE);
- $driver = new AnnotationDriver(new AnnotationReader(), $paths);
- // registering noop annotation autoloader - allow all annotations by default
- AnnotationRegistry::registerLoader('class_exists');
- $config->setMetadataDriverImpl($driver);
- $dbParams = array(
- 'driver' => 'pdo_mysql',
- 'user' => 'root',
- 'password' => 'root',
- 'dbname' => 'test',
- 'host' => 'localhost',
- );
- $entityManager = EntityManager::create($dbParams, $config);
- return $entityManager;
- ?>
- <?php
- /*
- * File: testDoctrine.php
- */
- /* @var $entityManager EntityManager */
- $entityManager= require_once "bootstrapDoctrineTest.php";
- use \DoctrineTest\Game;
- use \DoctrineTest\Publisher;
- use \DoctrineTest\GamePublisherMapping;
- /* @var $repos \GamePublisherMapping */
- $repos= $entityManager->getRepository('\DoctrineTest\GamePublisherMapping');
- $mapping= $repos->getPublisherMappingByGameSearchdate('testgame', new \DateTime('2014-06-05 15:00'));
- if (!$mapping) {
- die("nothing found");
- }
- echo $mapping->getGame()->getName(). " belongs to publisher ".$mapping->getPublisher()->getName()." since ".$mapping->getValidFrom()->format('c')."\n";
- $mapping= $repos->getPublisherMappingByGameSearchdate('testgame', new \DateTime(''));
- if (!$mapping) {
- die("nothing found");
- }
- echo $mapping->getGame()->getName(). " belongs to publisher ".$mapping->getPublisher()->getName()." since ".$mapping->getValidFrom()->format('c')."\n";?>
- ?>
- <?php
- /*
- * File: src/DoctrineTest/Game.php
- */
- namespace DoctrineTest;
- use Doctrine\ORM\Mapping as ORM;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\Common\Collections\ArrayCollection;
- /**
- * Game
- *
- * Testdata:
- * INSERT INTO game (name) VALUES ('testgame');
- *
- * @ORM\Entity()
- * @ORM\Table(
- * name="game",
- * uniqueConstraints={@ORM\UniqueConstraint(name="u_game_1", columns={"name"})}
- * )
- */
- class Game {
- /**
- *
- * @ORM\Id
- * @ORM\GeneratedValue()
- * @ORM\Column(type="bigint", name="game_id")
- * @var integer
- */
- protected $id;
- /**
- * The name of the game
- * @ORM\Column(type="string", name="name",length=255)
- * @var string
- */
- protected $name;
- /**
- *
- * @ORM\OneToMany(targetEntity="\DoctrineTest\GamePublisherMapping", mappedBy="game")
- */
- protected $game_publisher_mapping;
- function __construct() {
- $this->game_publisher_mapping= new ArrayCollection();
- }
- /**
- * Returns the primary key / Id of this entry
- * @return integer
- */
- public function getId() {
- return $this->id;
- }
- /**
- * The name of the game
- * @return string
- */
- public function getName() {
- return $this->name;
- }
- /**
- * Sets the name of the game
- * @param string $name
- * @return \Game
- */
- public function setName($name) {
- $this->name = $name;
- return $this;
- }
- /**
- * Get game_publisher_mapping
- *
- * @return \Doctrine\Common\Collections\Collection
- */
- public function getGamePublisherMapping()
- {
- return $this->game_publisher_mapping;
- }
- }
- ?>
- <?php
- /*
- * File: src/DoctrineTest/Publisher.php
- */
- namespace DoctrineTest;
- use Doctrine\ORM\Mapping as ORM;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\Common\Collections\ArrayCollection;
- /**
- * Entity for the publisher-Table
- *
- * Testdata:
- * INSERT INTO publisher (name) VALUES ('publisher_A'), ('publisher_B');
- *
- * @ORM\Entity()
- * @ORM\Table(
- * name="publisher",
- * uniqueConstraints={@ORM\UniqueConstraint(name="u_publisher_1", columns={"name"})}
- * )
- */
- class Publisher {
- /**
- *
- * @ORM\Id
- * @ORM\GeneratedValue()
- * @ORM\Column(type="bigint", name="publisher_id")
- * @var integer
- */
- protected $id;
- /**
- *
- * @ORM\Column(type="string", name="name", length=255)
- * @var string
- */
- protected $name;
- /**
- * @ORM\Column(type="string", name="description", length=255)
- * @var string
- */
- protected $description;
- /**
- *
- * @var \Doctrine\Common\Collections\Collection
- * @ORM\OneToMany(targetEntity="\DoctrineTest\GamePublisherMapping", mappedBy="publisher")
- */
- protected $game_publisher_mapping;
- /**
- * Constructor
- */
- function __construct() {
- $this->game_publisher_mapping= new ArrayCollection();
- }
- /**
- * Returns the primary key / payment_client_id
- * @return int
- */
- public function getId() {
- return $this->id;
- }
- /**
- * Returns the Name of the publisher
- * @return string
- */
- public function getName() {
- return $this->name;
- }
- /**
- * Returns the Description of the publisher
- * @return string
- */
- public function getDescription() {
- return $this->description;
- }
- /**
- * Sets the Name of the publisher
- * @param string name
- */
- public function setName($name) {
- $this->name = $name;
- }
- /**
- * Sets the Description of the publisher
- * @return string description
- */
- public function setDescription($description) {
- $this->description = $description;
- }
- }
- ?>
- <?php
- /*
- * File src/DoctrineTest/GamePublisherMapping.php
- */
- namespace DoctrineTest;
- use Doctrine\ORM\Mapping as ORM;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\Common\Collections\ArrayCollection;
- /**
- * GamePublisherMapping
- *
- * Testdata:
- * INSERT INTO game_publisher_mapping (game_id, publisher_id, valid_from) VALUES (1, 1, '2014-06-01 00:00'), (1, 2, NOW());
- *
- * @ORM\Table(
- * name="game_publisher_mapping"
- * ,indexes={
- * @ORM\Index(columns={"publisher_id"})
- * ,@ORM\Index(columns={"game_id"})
- * }
- * ,uniqueConstraints={@ORM\UniqueConstraint(columns={"game_id", "publisher_id", "valid_from"})}
- * )
- * @ORM\Entity(repositoryClass="\DoctrineTest\GamePublisherRepository")
- */
- class GamePublisherMapping
- {
- /**
- * @var integer
- *
- * @ORM\Column(name="game_publisher_mapping_id", type="bigint")
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- **/
- /**
- *
- * @ORM\ManyToOne(targetEntity="\DoctrineTest\Game", inversedBy="game_publisher_mapping")
- * @ORM\JoinColumn(name="game_id", referencedColumnName="game_id")
- * @var \DoctrineTest\Game
- */
- protected $game;
- /**
- *
- * @ORM\ManyToOne(targetEntity="\DoctrineTest\Publisher", inversedBy="game_publisher_mapping")
- * @ORM\JoinColumn(name="publisher_id", referencedColumnName="publisher_id")
- * @var \DoctrineTest\Publisher
- */
- protected $publisher;
- /**
- * @ORM\Column(name="valid_from", type="datetime")
- * @var \DateTime
- */
- protected $validFrom;
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set Game
- *
- * @param \DoctrineTest\Game $game
- * @return \DoctrineTest\GamePublisherMapping
- */
- public function setGame(\DoctrineTest\Game $game = null)
- {
- $this->game = $game;
- return $this;
- }
- /**
- * Get game
- *
- * @return \DoctrineTest\Game
- */
- public function getGame()
- {
- return $this->game;
- }
- /**
- * Set publisher
- *
- * @param \DoctrineTest\Publisher $publisher
- * @return \DoctrineTest\GamePublisherMapping
- */
- public function setPublisher(\DoctrineTest\Publisher $publisher= null)
- {
- $this->publisher = $publisher;
- return $this;
- }
- /**
- * Get publisher
- *
- * @return \DoctrineTest\Publisher
- */
- public function getPublisher()
- {
- return $this->publisher;
- }
- /**
- * @return \DateTime
- */
- public function getValidFrom() {
- return $this->validFrom;
- }
- }
- ?>
- <?php
- /*
- * File: src/DoctrineTest/GamePublisherRepository.php
- */
- namespace DoctrineTest;
- use Doctrine\ORM\EntityRepository;
- use Doctrine\ORM\Query;
- use Doctrine\ORM\Query\ResultSetMapping;
- /**
- *
- */
- class GamePublisherRepository extends EntityRepository {
- /**
- * Retrieves the mappings for the given game and searchDate
- *
- * @param mixed $game either the name or the Game-Entity
- * @param \DateTime searchDate
- * @return \DoctrineTest\GamePublisherMapping[]
- * @throws \InvalidArgumentException e.g. if the game-parameter is neither string nor Object
- */
- public function getPublisherMappingByGameSearchdate($game, \DateTime $searchDate) {
- $sql= "SELECT
- gpm.game_publisher_mapping_id
- ,gpm.publisher_id
- ,p.publisher_id as 'p_publisher_id' /* remove this line to provoke this error: PHP Notice: Undefined index: id in /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 2487 */
- ,p.name as p_name
- ,p.description as p_description
- ,game_id
- ,valid_from
- ,g.game_id as g_game_id
- ,g.name as g_name
- FROM game_publisher_mapping AS gpm
- JOIN publisher AS p USING (publisher_id)
- JOIN game AS g using (game_id)
- WHERE g.name = :searchGame AND valid_from <= :searchDate
- ORDER BY valid_from DESC
- LIMIT 1";
- $rsm= new ResultSetMapping();
- // add Entity, give it an alias
- $rsm->addEntityResult('\DoctrineTest\GamePublisherMapping', 'gpm');
- // map the fields to the entity
- $rsm->addFieldResult('gpm', 'game_publisher_mapping_id', 'id');
- $rsm->addFieldResult('gpm', 'valid_from', 'validFrom');
- // map the joined entity: paymentclient
- $rsm->addJoinedEntityResult('\DoctrineTest\Publisher', 'p', 'gpm', 'publisher');
- $rsm->addFieldResult('p', 'p_publisher_id', 'id');
- $rsm->addFieldResult('p', 'p_name', 'name');
- $rsm->addFieldResult('p', 'p_description', 'description');
- // map the joined entity: game
- $rsm->addJoinedEntityResult('\DoctrineTest\Game', 'g', 'gpm', 'game');
- $rsm->addFieldResult('g', 'g_game_id', 'id');
- $rsm->addFieldResult('g', 'g_name', 'name');
- $gamename= is_string($game) ? $game : NULL;
- if ($game instanceof \Game) {
- $gamename= $game->getName();
- }
- if (NULL === $gamename) {
- throw new \InvalidArgumentException(
- 'Value for Parameter game is an unsupported type: '. gettype($game)
- );
- }
- $query= $this->getEntityManager()->createNativeQuery($sql, $rsm);
- $query->setParameter('searchGame', $gamename, 'string');
- $query->setParameter('searchDate', $searchDate, 'datetime');
- $result= $query->getOneOrNullResult();
- if (!$result) {
- return array();
- }
- return $result;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement