Advertisement
Guest User

PHP-Doctrine-Warning

a guest
Jun 6th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.52 KB | None | 0 0
  1. <?php
  2. /*
  3. File bootstrapDoctrineTest.php
  4. */
  5. $autoloader= require_once('vendor/autoload.php');
  6.  
  7. use Doctrine\ORM\Configuration;
  8. use Doctrine\ORM\Tools\Setup;
  9. use Doctrine\ORM\EntityManager;
  10. use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use Doctrine\Common\Annotations\AnnotationRegistry;
  13.  
  14.  
  15. $paths= array(
  16.   realpath(__DIR__.'/src/DoctrineTest')
  17. );
  18. $config = Setup::createConfiguration($isDevMode= TRUE);
  19. $driver = new AnnotationDriver(new AnnotationReader(), $paths);
  20.  
  21. // registering noop annotation autoloader - allow all annotations by default
  22. AnnotationRegistry::registerLoader('class_exists');
  23. $config->setMetadataDriverImpl($driver);
  24.  
  25. $dbParams = array(
  26.     'driver'   => 'pdo_mysql',
  27.     'user'     => 'root',
  28.     'password' => 'root',
  29.     'dbname'   => 'test',
  30.     'host'   => 'localhost',
  31. );
  32.  
  33. $entityManager = EntityManager::create($dbParams, $config);
  34. return $entityManager;
  35. ?>
  36.  
  37. <?php
  38. /*
  39.  * File: testDoctrine.php
  40.  */
  41. /* @var $entityManager EntityManager */
  42. $entityManager= require_once "bootstrapDoctrineTest.php";
  43.  
  44. use \DoctrineTest\Game;
  45. use \DoctrineTest\Publisher;
  46. use \DoctrineTest\GamePublisherMapping;
  47.  
  48. /* @var $repos \GamePublisherMapping */
  49. $repos= $entityManager->getRepository('\DoctrineTest\GamePublisherMapping');
  50.  
  51. $mapping= $repos->getPublisherMappingByGameSearchdate('testgame', new \DateTime('2014-06-05 15:00'));
  52. if (!$mapping) {
  53.   die("nothing found");
  54. }
  55. echo $mapping->getGame()->getName(). " belongs to publisher ".$mapping->getPublisher()->getName()." since ".$mapping->getValidFrom()->format('c')."\n";
  56.  
  57. $mapping= $repos->getPublisherMappingByGameSearchdate('testgame', new \DateTime(''));
  58. if (!$mapping) {
  59.   die("nothing found");
  60. }
  61. echo $mapping->getGame()->getName(). " belongs to publisher ".$mapping->getPublisher()->getName()." since ".$mapping->getValidFrom()->format('c')."\n";?>
  62.  
  63. ?>
  64.  
  65. <?php
  66. /*
  67.  * File: src/DoctrineTest/Game.php
  68. */
  69.  
  70. namespace DoctrineTest;
  71.  
  72. use Doctrine\ORM\Mapping as ORM;
  73. use Doctrine\Common\Collections\Collection;
  74. use Doctrine\Common\Collections\ArrayCollection;
  75.  
  76. /**
  77.  * Game
  78.  *
  79.  * Testdata:
  80.  * INSERT INTO game (name) VALUES ('testgame');
  81.  *
  82.  * @ORM\Entity()
  83.  * @ORM\Table(
  84.  *  name="game",
  85.  *  uniqueConstraints={@ORM\UniqueConstraint(name="u_game_1", columns={"name"})}
  86.  * )
  87.  */
  88. class Game {
  89.     /**
  90.      *
  91.      * @ORM\Id
  92.      * @ORM\GeneratedValue()
  93.      * @ORM\Column(type="bigint", name="game_id")
  94.      * @var integer
  95.      */
  96.     protected $id;
  97.  
  98.     /**
  99.      * The name of the game
  100.      * @ORM\Column(type="string", name="name",length=255)
  101.      * @var string
  102.      */
  103.     protected $name;
  104.  
  105.     /**
  106.      *
  107.      * @ORM\OneToMany(targetEntity="\DoctrineTest\GamePublisherMapping", mappedBy="game")
  108.     */
  109.     protected $game_publisher_mapping;
  110.    
  111.     function __construct() {
  112.         $this->game_publisher_mapping= new ArrayCollection();
  113.     }
  114.  
  115.     /**
  116.      * Returns the primary key / Id of this entry
  117.      * @return integer
  118.      */
  119.     public function getId() {
  120.         return $this->id;
  121.     }
  122.      
  123.     /**
  124.      * The name of the game
  125.      * @return string
  126.      */
  127.     public function getName() {
  128.         return $this->name;
  129.     }
  130.  
  131.     /**
  132.      * Sets the name of the game
  133.      * @param string $name
  134.      * @return \Game
  135.      */
  136.     public function setName($name) {
  137.         $this->name = $name;
  138.         return $this;
  139.     }
  140.    
  141.     /**
  142.      * Get game_publisher_mapping
  143.      *
  144.      * @return \Doctrine\Common\Collections\Collection
  145.      */
  146.     public function getGamePublisherMapping()
  147.     {
  148.         return $this->game_publisher_mapping;
  149.     }
  150. }
  151. ?>
  152.  
  153. <?php
  154. /*
  155.  * File: src/DoctrineTest/Publisher.php
  156. */
  157.  
  158. namespace DoctrineTest;
  159.  
  160. use Doctrine\ORM\Mapping as ORM;
  161. use Doctrine\Common\Collections\Collection;
  162. use Doctrine\Common\Collections\ArrayCollection;
  163.  
  164. /**
  165.  * Entity for the publisher-Table
  166.  *
  167.  * Testdata:
  168.  * INSERT INTO publisher (name) VALUES ('publisher_A'), ('publisher_B');
  169.  *
  170.  * @ORM\Entity()
  171.  * @ORM\Table(
  172.  *  name="publisher",
  173.  *  uniqueConstraints={@ORM\UniqueConstraint(name="u_publisher_1", columns={"name"})}
  174.  * )
  175.  */
  176. class Publisher {
  177.  
  178.     /**
  179.      *
  180.      * @ORM\Id
  181.      * @ORM\GeneratedValue()
  182.      * @ORM\Column(type="bigint", name="publisher_id")
  183.      * @var integer
  184.      */
  185.     protected $id;
  186.  
  187.     /**
  188.      *
  189.      * @ORM\Column(type="string", name="name", length=255)
  190.      * @var string
  191.      */
  192.     protected $name;
  193.  
  194.     /**
  195.      * @ORM\Column(type="string", name="description", length=255)
  196.      * @var string
  197.      */
  198.     protected $description;
  199.  
  200.     /**
  201.      *
  202.      * @var \Doctrine\Common\Collections\Collection
  203.      * @ORM\OneToMany(targetEntity="\DoctrineTest\GamePublisherMapping", mappedBy="publisher")
  204.      */
  205.     protected $game_publisher_mapping;
  206.  
  207.     /**
  208.      * Constructor
  209.      */
  210.     function __construct() {
  211.         $this->game_publisher_mapping= new ArrayCollection();
  212.     }
  213.    
  214.     /**
  215.      * Returns the primary key / payment_client_id
  216.      * @return int
  217.      */
  218.     public function getId() {
  219.         return $this->id;
  220.     }
  221.  
  222.     /**
  223.      * Returns the Name of the publisher
  224.      * @return string
  225.      */
  226.     public function getName() {
  227.         return $this->name;
  228.     }
  229.  
  230.     /**
  231.      * Returns the Description of the publisher
  232.      * @return string
  233.      */
  234.     public function getDescription() {
  235.         return $this->description;
  236.     }
  237.  
  238.     /**
  239.      * Sets the Name of the publisher
  240.      * @param string name
  241.      */
  242.     public function setName($name) {
  243.         $this->name = $name;
  244.     }
  245.  
  246.     /**
  247.      * Sets the Description of the publisher
  248.      * @return string description
  249.      */
  250.     public function setDescription($description) {
  251.         $this->description = $description;
  252.     }
  253. }
  254. ?>
  255.  
  256. <?php
  257. /*
  258. * File src/DoctrineTest/GamePublisherMapping.php
  259. */
  260.  
  261. namespace DoctrineTest;
  262.  
  263. use Doctrine\ORM\Mapping as ORM;
  264. use Doctrine\Common\Collections\Collection;
  265. use Doctrine\Common\Collections\ArrayCollection;
  266.  
  267. /**
  268.  * GamePublisherMapping
  269.  *
  270.  * Testdata:
  271.  * INSERT INTO game_publisher_mapping (game_id, publisher_id, valid_from) VALUES (1, 1, '2014-06-01 00:00'), (1, 2, NOW());
  272.  *
  273.  * @ORM\Table(
  274.  *  name="game_publisher_mapping"
  275.  *  ,indexes={
  276.  *      @ORM\Index(columns={"publisher_id"})
  277.  *      ,@ORM\Index(columns={"game_id"})
  278.  *  }
  279.  *  ,uniqueConstraints={@ORM\UniqueConstraint(columns={"game_id", "publisher_id", "valid_from"})}
  280.  * )
  281.  * @ORM\Entity(repositoryClass="\DoctrineTest\GamePublisherRepository")
  282.  */
  283. class GamePublisherMapping
  284. {
  285.     /**
  286.      * @var integer
  287.      *
  288.      * @ORM\Column(name="game_publisher_mapping_id", type="bigint")
  289.      * @ORM\Id
  290.      * @ORM\GeneratedValue(strategy="AUTO")
  291.      */
  292.     protected $id;
  293.     /**
  294.      **/
  295.  
  296.     /**
  297.      *
  298.      * @ORM\ManyToOne(targetEntity="\DoctrineTest\Game", inversedBy="game_publisher_mapping")
  299.      * @ORM\JoinColumn(name="game_id", referencedColumnName="game_id")
  300.      * @var \DoctrineTest\Game
  301.      */
  302.     protected $game;
  303.  
  304.     /**
  305.      *
  306.      * @ORM\ManyToOne(targetEntity="\DoctrineTest\Publisher", inversedBy="game_publisher_mapping")
  307.      * @ORM\JoinColumn(name="publisher_id", referencedColumnName="publisher_id")
  308.      * @var \DoctrineTest\Publisher
  309.      */
  310.     protected $publisher;
  311.    
  312.     /**
  313.      * @ORM\Column(name="valid_from", type="datetime")
  314.      * @var \DateTime
  315.      */
  316.     protected $validFrom;
  317.  
  318.     /**
  319.      * Get id
  320.      *
  321.      * @return integer
  322.      */
  323.     public function getId()
  324.     {
  325.         return $this->id;
  326.     }
  327.  
  328.     /**
  329.      * Set Game
  330.      *
  331.      * @param \DoctrineTest\Game $game
  332.      * @return \DoctrineTest\GamePublisherMapping
  333.      */
  334.     public function setGame(\DoctrineTest\Game $game = null)
  335.     {
  336.         $this->game = $game;
  337.  
  338.         return $this;
  339.     }
  340.  
  341.     /**
  342.      * Get game
  343.      *
  344.      * @return \DoctrineTest\Game
  345.      */
  346.     public function getGame()
  347.     {
  348.         return $this->game;
  349.     }
  350.  
  351.     /**
  352.      * Set publisher
  353.      *
  354.      * @param \DoctrineTest\Publisher $publisher
  355.      * @return \DoctrineTest\GamePublisherMapping
  356.      */
  357.     public function setPublisher(\DoctrineTest\Publisher $publisher= null)
  358.     {
  359.         $this->publisher = $publisher;
  360.  
  361.         return $this;
  362.     }
  363.  
  364.     /**
  365.      * Get publisher
  366.      *
  367.      * @return \DoctrineTest\Publisher
  368.      */
  369.     public function getPublisher()
  370.     {
  371.         return $this->publisher;
  372.     }
  373.    
  374.     /**
  375.      * @return \DateTime
  376.      */
  377.     public function getValidFrom() {
  378.       return $this->validFrom;
  379.     }
  380. }
  381. ?>
  382.  
  383. <?php
  384. /*
  385.  * File: src/DoctrineTest/GamePublisherRepository.php
  386. */
  387. namespace DoctrineTest;
  388.  
  389. use Doctrine\ORM\EntityRepository;
  390. use Doctrine\ORM\Query;
  391. use Doctrine\ORM\Query\ResultSetMapping;
  392.  
  393. /**
  394.  *
  395.  */
  396. class GamePublisherRepository extends EntityRepository {
  397.  
  398.     /**
  399.      * Retrieves the mappings for the given game and searchDate
  400.      *
  401.      * @param mixed $game either the name or the Game-Entity
  402.      * @param \DateTime searchDate
  403.      * @return \DoctrineTest\GamePublisherMapping[]
  404.      * @throws \InvalidArgumentException e.g. if the game-parameter is neither string nor Object
  405.      */
  406.     public function getPublisherMappingByGameSearchdate($game, \DateTime $searchDate) {
  407.         $sql= "SELECT
  408.        gpm.game_publisher_mapping_id
  409.        ,gpm.publisher_id
  410.        ,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 */
  411.        ,p.name as p_name
  412.        ,p.description as p_description
  413.        ,game_id
  414.        ,valid_from
  415.        ,g.game_id as g_game_id
  416.        ,g.name as g_name
  417.        FROM game_publisher_mapping AS gpm
  418.        JOIN publisher AS p USING (publisher_id)
  419.        JOIN game AS g using (game_id)
  420.        WHERE g.name = :searchGame AND valid_from <= :searchDate
  421.        ORDER BY valid_from DESC
  422.        LIMIT 1";
  423.  
  424.         $rsm= new ResultSetMapping();
  425.         // add Entity, give it an alias
  426.         $rsm->addEntityResult('\DoctrineTest\GamePublisherMapping', 'gpm');
  427.         // map the fields to the entity
  428.         $rsm->addFieldResult('gpm', 'game_publisher_mapping_id', 'id');
  429.         $rsm->addFieldResult('gpm', 'valid_from', 'validFrom');
  430.        
  431.         // map the joined entity: paymentclient
  432.         $rsm->addJoinedEntityResult('\DoctrineTest\Publisher', 'p', 'gpm', 'publisher');
  433.         $rsm->addFieldResult('p', 'p_publisher_id', 'id');
  434.         $rsm->addFieldResult('p', 'p_name', 'name');
  435.         $rsm->addFieldResult('p', 'p_description', 'description');
  436.         // map the joined entity: game
  437.         $rsm->addJoinedEntityResult('\DoctrineTest\Game', 'g', 'gpm', 'game');
  438.         $rsm->addFieldResult('g', 'g_game_id', 'id');
  439.         $rsm->addFieldResult('g', 'g_name', 'name');
  440.        
  441.         $gamename= is_string($game) ? $game : NULL;
  442.         if ($game instanceof \Game) {
  443.             $gamename= $game->getName();
  444.         }
  445.        
  446.         if (NULL === $gamename) {
  447.             throw new \InvalidArgumentException(
  448.                 'Value for Parameter game is an unsupported type: '.  gettype($game)
  449.             );
  450.         }
  451.        
  452.         $query= $this->getEntityManager()->createNativeQuery($sql, $rsm);
  453.         $query->setParameter('searchGame', $gamename, 'string');
  454.         $query->setParameter('searchDate', $searchDate, 'datetime');
  455.        
  456.         $result= $query->getOneOrNullResult();
  457.         if (!$result) {
  458.           return array();
  459.         }
  460.  
  461.         return $result;
  462.     }
  463. }
  464. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement