-
-
Save doctrinebot/fa1743b14ca2be4542ef to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-542 - https://github.com/doctrine/doctrine2/issues/5050
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @Entity | |
*/ | |
class Ingredient | |
{ | |
/** | |
* @Id | |
* @column(type="integer", name="product_id"); | |
*/ | |
public $productId; | |
/** | |
* @Id | |
* @column(type="integer", name="recipe_id"); | |
*/ | |
public $recipeId; | |
/** | |
* @ManyToOne(targetEntity="Recipe", inversedBy="ingredients") | |
*/ | |
public $recipe; | |
/** | |
* @ManyToOne(targetEntity="FoodProduct") | |
*/ | |
private $product; | |
/** | |
* @column(type="integer"); | |
*/ | |
public $amount; | |
public function __get($name) | |
{ | |
return $this->$name; | |
} | |
public function __set($name, $value) | |
{ | |
$this->$name = $value; | |
} | |
} | |
/** | |
* @Entity | |
*/ | |
class Recipe | |
{ | |
/** | |
* @Id | |
* @Column(type="integer") | |
* @GeneratedValue | |
*/ | |
public $id; | |
/** | |
* @Column(type="string") | |
*/ | |
public $name; | |
/** | |
* @OneToMany(targetEntity="Ingredient", mappedBy="recipe") | |
*/ | |
public $ingredients; | |
} | |
/** | |
* @Entity | |
*/ | |
class FoodProduct | |
{ | |
/** | |
* @Id | |
* @Column(type="integer") | |
* @GeneratedValue | |
*/ | |
public $id; | |
/** | |
* @Column(type="string") | |
*/ | |
public $name; | |
} | |
require_once "/home/benny/code/php/wsnetbeans/doctrine2/lib/Doctrine/Common/ClassLoader.php"; | |
$loader = new \Doctrine\Common\ClassLoader("Doctrine", "/home/benny/code/php/wsnetbeans/doctrine2/lib/"); | |
$loader->register(); | |
$connectionParams = array( | |
'dbname' => 'doctrine_tests', | |
'user' => 'benny', | |
'password' => '', | |
'host' => 'localhost', | |
'driver' => 'pdo_mysql', | |
); | |
$config = new \Doctrine\ORM\Configuration(); | |
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); | |
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache); | |
$config->setProxyDir(__DIR__ . '/_files'); | |
$config->setProxyNamespace('DoctrineExtensions\Paginate\Proxies'); | |
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(__DIR__))); | |
$em = \Doctrine\ORM\EntityManager::create($connectionParams, $config); | |
$metadata = array( | |
$em->getMetadataFactory()->getMetadataFor('Ingredient'), | |
$em->getMetadataFactory()->getMetadataFor('Recipe'), | |
$em->getMetadataFactory()->getMetadataFor('FoodProduct'), | |
); | |
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em); | |
try { | |
$schemaTool->dropSchema($metadata); | |
} catch(\Exception $e) { | |
} | |
var_dump($schemaTool->getCreateSchemaSql($metadata)); | |
try { | |
$schemaTool->createSchema($metadata); | |
} catch(\Exception $e) { | |
} | |
$noodles = new FoodProduct(); | |
$noodles->name = "Noodles"; | |
$tomatoes = new FoodProduct(); | |
$tomatoes->name = "Tomatoes"; | |
$recipe = new Recipe(); | |
$recipe->name = "Spagetthi with Tomatoes"; | |
$em->persist($noodles); | |
$em->persist($tomatoes); | |
$em->persist($recipe); | |
$em->flush(); | |
$ing1 = new Ingredient(); | |
$ing1->product = $noodles; | |
$ing1->recipe = $recipe; | |
$ing1->productId = $noodles->id; | |
$ing1->recipeId = $recipe->id; | |
$ing1->amount = 100; | |
$ing2 = new Ingredient(); | |
$ing2->product = $tomatoes; | |
$ing2->recipe = $recipe; | |
$ing2->productId = $tomatoes->id; | |
$ing2->recipeId = $recipe->id; | |
$ing2->amount = 5; | |
$em->persist($ing1); | |
$em->persist($ing2); | |
$em->flush(); | |
$em->clear(); | |
$query = $em->createQuery('SELECT r, p, i FROM Recipe r JOIN r.ingredients i JOIN i.product p'); | |
$result = $query->getResult(); | |
foreach ($result AS $recipe) { | |
echo $recipe->name."\n"; | |
foreach ($recipe->ingredients AS $ingredient) { | |
echo " " . $ingredient->product->name." " . $ingredient->amount."\n"; | |
} | |
} | |
/*$recipe = $em->find('Recipe', 1); | |
echo $recipe->name."\n"; | |
foreach ($recipe->ingredients AS $ingredient) { | |
echo " " . $ingredient->product->name." " . $ingredient->amount."\n"; | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment