Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL | E_STRICT);
- require 'config.php';
- use Entities\Product, Entities\Feature;
- header('Content-Type: text/plain');
- class Debug
- {
- public static function dump($entity, $maxDepth = 1)
- {
- $reflClass = new ReflectionClass(get_class($entity));
- $arr = array();
- foreach ($reflClass->getProperties() as $reflProperty) {
- $reflProperty->setAccessible(true);
- $value = $reflProperty->getValue($entity);
- if ($maxDepth) {
- if (is_object($value) && in_array('Doctrine\Common\Collections\Collection', class_implements($value))) {
- $value = $value->toArray();
- foreach ($value as $k => $v) {
- $value[$k] = Debug::dump($v, $maxDepth - 1);
- }
- }
- } else {
- $value = is_object($value) ? get_class($value) : $value;
- }
- $arr[$reflProperty->getName()] = $value;
- }
- return $arr;
- }
- }
- try
- {
- $f1 = new Feature;
- $f1->setName('AC-3');
- $f2 = new Feature;
- $f2->setName('DTS');
- $p = new Product;
- $p->setName('Test');
- $p->addFeature($f1);
- $p->addFeature($f2);
- $em->persist($p);
- $em->flush();
- $f3 = new Feature;
- $f3->setName('XVID');
- $p->addFeature($f3);
- $q = $em->createQuery('
- SELECT p, f
- FROM Entities\Product p
- LEFT JOIN p.features f
- ');
- $res = $q->getResult();
- //foreach ($res as $product)
- // var_dump(Debug::dump($product, 1));
- foreach ($res as $product)
- {
- printf("Product: %d %s\n", $product->getId(), $product->getName());
- var_dump(get_class($product->getFeatures()));
- foreach ($product->getFeatures() as $feature)
- {
- printf(" Feature: %d %s\n",
- $feature->getId(),
- $feature->getName()
- );
- }
- }
- }
- catch (Exception $e)
- {
- echo ':-( ' . $e->getMessage();
- }
Add Comment
Please, Sign In to add comment