Guest User

Untitled

a guest
Jul 12th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3. require 'config.php';
  4.  
  5. use Entities\Product, Entities\Feature;
  6.  
  7. header('Content-Type: text/plain');
  8.  
  9. class Debug
  10. {
  11. public static function dump($entity, $maxDepth = 1)
  12. {
  13. $reflClass = new ReflectionClass(get_class($entity));
  14. $arr = array();
  15.  
  16. foreach ($reflClass->getProperties() as $reflProperty) {
  17. $reflProperty->setAccessible(true);
  18. $value = $reflProperty->getValue($entity);
  19.  
  20. if ($maxDepth) {
  21. if (is_object($value) && in_array('Doctrine\Common\Collections\Collection', class_implements($value))) {
  22. $value = $value->toArray();
  23.  
  24. foreach ($value as $k => $v) {
  25. $value[$k] = Debug::dump($v, $maxDepth - 1);
  26. }
  27. }
  28. } else {
  29. $value = is_object($value) ? get_class($value) : $value;
  30. }
  31.  
  32. $arr[$reflProperty->getName()] = $value;
  33. }
  34.  
  35. return $arr;
  36. }
  37. }
  38.  
  39.  
  40. try
  41. {
  42. $f1 = new Feature;
  43. $f1->setName('AC-3');
  44.  
  45. $f2 = new Feature;
  46. $f2->setName('DTS');
  47.  
  48. $p = new Product;
  49. $p->setName('Test');
  50. $p->addFeature($f1);
  51. $p->addFeature($f2);
  52. $em->persist($p);
  53.  
  54. $em->flush();
  55.  
  56. $f3 = new Feature;
  57. $f3->setName('XVID');
  58.  
  59. $p->addFeature($f3);
  60.  
  61. $q = $em->createQuery('
  62. SELECT p, f
  63. FROM Entities\Product p
  64. LEFT JOIN p.features f
  65. ');
  66. $res = $q->getResult();
  67.  
  68. //foreach ($res as $product)
  69. // var_dump(Debug::dump($product, 1));
  70.  
  71. foreach ($res as $product)
  72. {
  73. printf("Product: %d %s\n", $product->getId(), $product->getName());
  74.  
  75. var_dump(get_class($product->getFeatures()));
  76.  
  77. foreach ($product->getFeatures() as $feature)
  78. {
  79. printf(" Feature: %d %s\n",
  80. $feature->getId(),
  81. $feature->getName()
  82. );
  83. }
  84. }
  85.  
  86. }
  87. catch (Exception $e)
  88. {
  89. echo ':-( ' . $e->getMessage();
  90. }
Add Comment
Please, Sign In to add comment