Created
March 25, 2011 19:33
-
-
Save Ocramius/887463 to your computer and use it in GitHub Desktop.
Doctrine 2 "Hello World" sample
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 | |
/* ORM CONFIG HERE, LIKE IN SAMPLE BOOTSTRAP */ | |
/** @var $em \Doctrine\ORM\EntityManager */ | |
$em = Doctrine\ORM\EntityManager::create($options, $config); | |
$greeting = new Greeting(); | |
$greeting->setGreeting('Hello World!'); | |
$em->persist($greeting); | |
$greeting2 = new Greeting(); | |
$greeting2->setGreeting('Hello again! This is another greeting!'); | |
$em->persist($greeting2); | |
$em->flush(); | |
$allGreetings = $em->createQuery('SELECT g FROM Greeting g')->getResult(); | |
foreach($allGreetings as $greetMe) { | |
echo $greetMe->getGreeting() . '<br/>'; | |
} |
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 | |
/** | |
* This class is somewhere in your library | |
* @Entity | |
* @Table(name="greetings") | |
*/ | |
class Greeting { | |
/** | |
* @var int | |
* @Id | |
* @Column(type="integer",name="id") | |
* @GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @var string | |
* @Column(type="string", length=60, name="greeting") | |
*/ | |
protected $greeting; | |
public function getGreeting() { | |
return $this->greeting; | |
} | |
public function setGreeting($greeting) { | |
$this->greeting = (string) $greeting; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment