Tanur / application / controllers / doctrine_test.php

Full commit

class Doctrine_test extends CI_Controller
	public function index()
		echo 'Index';
	public function createObjects()
		// create a new user object
		$user = new Entities\User;
		$user->setPassword(md5('Emma Watson'));
		$user->setCreated(new DateTime());
		// standard way in CodeIgniter to access a library in a controller: $this->library_name->member->memberFunction()
		// save the object to database, so that it can obtain its ID
		// create a new article object
		$article = new Entities\Article;
		$article->setTitle('Emma Watson is extremely talented, no?');
		$article->setContent('By talented, I mean good at lookin\' good.');
		$article->setCreated(new DateTime());
		// the user object you pass must be persisted first!
		// save the article object to the database
		echo "<b>Success!</b>";
	public function get_objects()
		// get an object by ID
		$user = $this->doctrine->em->find('Entities\User', 1);
		echo $user->getFirstName().' '.$user->getLastName().' is a real chill guy.<br />';
		// get a related object
		$article = $this->doctrine->em->find('Entities\Article', 1);
		$user = $article->getUser();
		echo $user->getFirstName().' '.$user->getLastName().' thinks CodeIgniter + Doctrine is real slick. <br />';
		// what happens when we try to get an object that doesn't exist?
		$article = $this->doctrine->em->find('Entities\Article', 9001);
			// the "find" call returns NULL!
			echo 'Dude, that article number 9001 doesn\'t even exist yet.<br />';
		// get an object by another field
		$user = $this->doctrine->em->getRepository('Entities\User')->findOneBy(array('email' => ''));
		echo $user->getFirstName().' '.$user->getLastName().' is glad he isn\'t dealing with Symfony.<br />';