tournasdim
5/12/2014 - 6:34 AM

Doctrine 2 One-To-Many, Bidirectional example . Replacing "Simple Annotation Reader" by "Extended Annotation Reader) . Notions are now defin

Doctrine 2 One-To-Many, Bidirectional example . Replacing "Simple Annotation Reader" by "Extended Annotation Reader) . Notions are now defined by "@ORM\xxx" instead of "@xxx" .

<?php 

require_once __DIR__.'/../src/bootstrap.php';



// create a User 

$user = new \Blog\Entity\User ; 
$user->setFirstName('Eric')
	->setLastName('Clapton')
	->setRole('editor') ; 
$entityManager->persist($user);
$entityManager->flush() ; 
echo 'done with user : ' . $user->getId() ; 



/*
// User writes a post

$user = $entityManager->getRepository('Blog\Entity\User')->findOneBy(['firstName' => 'Eric']) ; 
$post = new Blog\Entity\Post ; 
$post->setTitle("Titlename")
	->setContent('Lorem ipsum ..... this is a test')
	->setUser($user) ; 
$entityManager->persist($post) ; 
$entityManager->flush(); 
echo 'well done with postId : '. $post->getId() ; 
*/


/*
// Get UserName of a post with title .... 

$post= $entityManager->getRepository('Blog\Entity\Post')->findOneBy(['title' => 'titlename']) ; 
$user = $post->getUser() ; 
echo 'name of user is :' . $user->getLastName() ; 
*/

/*
// Find User by LastName 

$user = $entityManager->getRepository('Blog\Entity\User')->findOneByLastName('Clapton') ; 
echo $user->getFirstName() ; 
*/

/*
// find all posts belonging to User .... 

$user = $entityManager->getRepository('Blog\Entity\User')->findOneByLastName('Clapton') ; 
$posts = $entityManager->getRepository('Blog\Entity\Post')->findBy(["user" => $user->getId()]) ;
var_dump($posts) ;
foreach ($posts as $post) 
{
	echo $post->getTitle() . '<br>' ; 
	echo $post->getContent() ; 
	echo '<hr>' ; 
}
*/

/* 
// Delete a User , article belonging to this user should be deleted manualy .... 
$user = $entityManager->getRepository('Blog\Entity\User')->findOneByLastName('Clapton') ; 
$posts = $entityManager->getRepository('Blog\Entity\Post')->findBy(["user" => $user->getId()]) ;
foreach ($posts as $post) 
{
	$entityManager->remove($post);

}

$entityManager->flush() ; 

*/

/*
// Delete a User and all posts belonging to that specified user
// Autodeleting works only if : "cascade={"remove"}, orphanRemoval=true" was defined via Annotations

$user = $entityManager->getRepository('Blog\Entity\User')->findOneBy(['firstName' => 'Eric']) ; 
$entityManager->remove($user);
$entityManager->flush() ; 

*/
<?php 

namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM ;
use Doctrine\Common\Collections\Collection,
    Doctrine\Common\Collections\ArrayCollection;

/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User
{

	/**
	* @ORM\Id 
	* @ORM\Column(type="integer")
	* @ORM\GeneratedValue
	*/
	private $id;

	/** 
	* @ORM\Column(type="string", name="first_name", nullable=true) 
	*/
	private $firstName;

	/** 
	* @ORM\Column(type="string", name="last_name", nullable=true) 
	*/
	private $lastName;

	/** 
	* @ORM\Column(type="string", nullable=true) 
	*/
	private $role;

	/**
	* @ORM\OneToMany(targetEntity="\Blog\Entity\Post", mappedBy="user" , cascade={"remove"}, orphanRemoval=true )
	*/
	private $posts;

	public function __construct()
	{
		$this->posts = new ArrayCollection();
	}


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set firstName
     *
     * @param string $firstName
     * @return User
     */
    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;

        return $this;
    }

    /**
     * Get firstName
     *
     * @return string 
     */
    public function getFirstName()
    {
        return $this->firstName;
    }

    /**
     * Set lastName
     *
     * @param string $lastName
     * @return User
     */
    public function setLastName($lastName)
    {
        $this->lastName = $lastName;

        return $this;
    }

    /**
     * Get lastName
     *
     * @return string 
     */
    public function getLastName()
    {
        return $this->lastName;
    }

    /**
     * Set gender
     *
     * @param string $gender
     * @return User
     */
    public function setRole($role)
    {
        $this->role = $role;

        return $this;
    }

    /**
     * Get gender
     *
     * @return string 
     */
    public function getRole()
    {
        return $this->role;
    }

    /**
     * Add posts
     *
     * @param \Blog\Entity\Post $posts
     * @return User
     */
    public function addPost(Post $posts)
    {
        $this->posts[] = $posts;

        return $this;
    }

    /**
     * Remove posts
     *
     * @param \Blog\Entity\Post $posts
     */
    public function removePost(Post $posts)
    {
        $this->posts->removeElement($posts);
    }

    /**
     * Get posts
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getPosts()
    {
        return $this->posts;
    }
}
<?php 

namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM ;

/**
* @ORM\Entity
* @ORM\Table(name="posts")
*/
class Post
{

/**
* @ORM\Id 
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="\Blog\Entity\User", inversedBy="posts")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;

/** 
* @ORM\Column(type="string") 
*/
private $title;

/** 
* @ORM\Column(type="string") 
*/
private $content;




    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Post
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set content
     *
     * @param string $content
     * @return Post
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string 
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Set user
     *
     * @param \Blog\Entity\User $user
     * @return Post
     */
    public function setUser(User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Blog\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}
<?php

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
//use Doctrine\ORM\Mapping\Driver\AnnotationDriver ; 
//use Doctrine\Common\Annotations\AnnotationReader ; 

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../config/config.php';

$entitiesPath = array(__DIR__.'/Blog');
$config = Setup::createAnnotationMetadataConfiguration($entitiesPath, $isDevMode,null,null,false);

/*
The fifth argument in "Setup::createAnnotationMetadataConfiguration" defines "SimpleAnnotations" to false (now , Annotations should be noted as -->  @ORM\xxxx instead of @xxx).
Alternatively (instead of setting the fifth argument), assign manually the new "AnnotationReader"
$driver = new AnnotationDriver(new AnnotationReader(), $entitiesPath );
$config->setMetadataDriverImpl($driver); 
==== On stackoverflow.com ====
http://stackoverflow.com/questions/21036461/obtaining-doctrine-entity-meta-data 
http://stackoverflow.com/questions/21058689/no-mapped-doctrine-orm-entities-according-to-the-current-configuration 
!!!! the documentation uses the "simple" annoatation reader !!!!!
http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html --> 
/**
 * @Entity @Table(name="products")
 **/


*/

$entityManager = EntityManager::create($dbParams, $config);

//var_dump($config) ; // Doctrine\ORM\Configuration