hellbreak
hellbreak

Reputation: 465

Doctrine2 drive me crazy with OneToMany and ManyToOne relationship

I try to work Doctrine2 Mapping out but after 8 hours of problems I really need your help, please.

I have 3 PostgreSQL table, The first one is house

 Column  |      Type      |                     Modifiers                      
---------+----------------+----------------------------------------------------
 id      | integer        | not null default nextval('house_id_seq'::regclass)
 name    | character(255) | not null
 address | character(255) | 
 rooms   | integer        | not null
Indexes:
    "house_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "room" CONSTRAINT "room_house_id_fkey" FOREIGN KEY (house_id) REFERENCES house(id) ON UPDATE CASCADE ON DELETE CASCADE

and the second one is

Table "public.room"
  Column   |          Type          |                     Modifiers                     
-----------+------------------------+---------------------------------------------------
 id        | integer                | not null default nextval('room_id_seq'::regclass)
 name      | character varying(255) | 
 room_type | integer                | 
 vacant    | boolean                | 
 house_id  | integer                | 
Indexes:
    "room_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "room_house_id_fkey" FOREIGN KEY (house_id) REFERENCES house(id) ON UPDATE CASCADE ON DELETE CASCADE

and the last one is

 Table "public.category"
   Column   |      Type      |                       Modifiers                       
------------+----------------+-------------------------------------------------------
 id         | integer        | not null default nextval('category_id_seq'::regclass)
 name       | character(255) | not null
 min_person | integer        | not null
 max_person | integer        | not null
Indexes:
    "category_pkey" PRIMARY KEY, btree (id)

I insert a FK on room_house_id that references house.id on House table .

In my Zend Framework2 I have my entities declare as

namespace Structures\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Structures\Entity\House
 *
 * @ORM\Table(name="house")
 * @ORM\Entity(repositoryClass="Structures\Entity\Repository\HouseRepository")
 */

class House
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="house_id_seq", allocationSize=1, initialValue=1)
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;

    /**
     * @var string $address
     *
     * @ORM\Column(name="address", type="string", length=255)
     */
    private $address;

    /**
     * @var string $rooms
     *
     * @ORM\Column(name="rooms", type="integer", nullable=false)
     */
    private $rooms;

    /**
     *
     * @ORM\OneToMany(targetEntity="Structures\Entity\Room", mappedBy="house", cascade={"remove", "persist"})
     */
    protected $house_room;
}


/**
 * Structures\Entity\Room
 *
 * @ORM\Table(name="room")
 * @ORM\Entity(repositoryClass="Structures\Entity\Repository\RoomRepository")
 */

class Room {

    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Id
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255,nullable=false)
     */
    private $name;


    /**
     * @var boolean $vacant
     *
     * @ORM\Column(name="vacant", type="boolean", nullable=false)
     */
    private $vacant;


    /**
     * @var Structures\Entity\Category
     *
     * @ORM\ManyToOne(targetEntity="Structures\Entity\Category" , cascade={"persist", "remove"})
     * @ORM\JoinColumns({
     *  @ORM\JoinColumn(name="room_type", referencedColumnName="id")
     * })
     */
    private $category;


    /**
     * @ORM\ManyToOne(targetEntity="Structures\Entity\House", inversedBy="house_room", cascade={"remove", "persist"})
     *
     */
    private $house;
}

/**
 * Structures\Entity\Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="Structures\Entity\Repository\CategoryRepository")
 *
 */

class Category {

    /**
     * @var integer $id
     *
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Id
     *
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     *
     */
    private $name;

    /**
     * @var integer $min_person
     * @ORM\Column(name="min_person", type="integer" , nullable=false)
     */
    private $min_person;

    /**
     * @var integer $max_person
     * @ORM\Column(name="max_person", type="integer", nullable=false)
     */
    private $max_person;
}

The relationship with Room and Category works as well.

The one concerns Room and House Table doesn't work at all. I cannot succeed in mapping the FK house_id with the id column on House table. All edit / save operation fails and the house_id is alway sets to NULL.

Please let me understand where I've made mistakes, I'm driving crazy

UPDATE :

I try to change the relationship between House and Room. I delete the OneToMany on the House's side and change the ManyToOne in

/**
 * @var Structure\Entity\Structure
 *
 * @ORM\ManyToOne(targetEntity="Structures\Entity\House")
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="house_id", referencedColumnName="id", nullable=FALSE)
 * })
 */
private $house;

When I fetch a room with

$room = $this->roomService->getRoom($id);
var_dump($room);

I have

object(Structures\Entity\Room)#383 (4) 
{ 
    ["id":"Structures\Entity\Room":private]=> int(77)             
    ["name":"Structures\Entity\Room":private]=> string(33) "my room" 
    ["structure":"Structures\Entity\Room":private]=> object(Structures\Entity\House)#339 (4) { 
    ["id":"Structures\Entity\House":private]=> int(3) 
    ["name":"Structures\Entity\House":private]=> string(255) "hotel2b " 
    ["address":"Structures\Entity\House":private]=> string(255) "hotel2b avenue" 
    ["number_of_rooms":"Structures\Entity\House":private]=> int(20) } 
    ["house_id":"Structures\Entity\Room":private]=> int(3) 
}

If I use the OneToMany relationship var_dump($room) returns me a full page of I don't know what it means dump !

Still, when Edit a room, the column house_is is alway sets to NULL. Still working on

UPDATE : 09 March

After a couple of hours spend reading tutorial, I write everything from start. House Entity does not exits anymore. Structure Entity is the new one. My problem : Use Doctrine2 to add and edit data with OneToMany and ManyToOne relationship.

My solution :

My Entities are namespace Structures\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Structures\Entity\Structure
 *
 * @ORM\Table(name="room")
 * @ORM\Entity(repositoryClass="Structures\Entity\Repository\RoomRepository")
 */

class Room {

/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Id
 */
private $id;

/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=255,nullable=false)
 *
 */
private $name;

/**
 * @var integer structure_id
 * @ORM\Column(name="structure_id", type="integer", nullable=false)
 */
private $structure_id;

/**
 * @var Structure\Entity\Structure
 *
 * @ORM\ManyToOne(targetEntity="Structure", inversedBy="rooms")
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="structure_id", referencedColumnName="id")
 * })
 */
private $structure;




public function __construct()
{
  //  $this->structure = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * @param $data
 */
public function exchangeArray($data)

{
    $this->fillWith($data);
}


/**
 * @param $data
 * @return Room
 */
public static function createFromArray($data)
{
    $room = new Room();
    $room->fillWith($data);
    return $room;
}

public function updateFromArray($room)
{
    $structure_id = $room->getStructureId();
    $structure = $this->getStructure($structure_id);
    $room->setStructure($structure);
    return $room;


}




/**
 * Sets id
 * @param $i_id
 */
private function set_Id($i_id) {
    $this->id = $i_id;
}
/**
 * Gets id
 * @return integer
 *
 */
public function getId() {
    return $this->id;
}

/**
 * Set name
 * @param string name
 *
 */
public function setName($name) {
    $this->name = $name;
}
/**
 * Get name
 * @return string
 *
 */
public function getName() {
    return $this->name;
}
public function getStructureId()
{
    return $this->structure_id;
}

/**
 *
 * @return \Doctrine\Common\Collections\ArrayCollection|Structure\Entity\Structure
 */
public function getStructure() {
    return $this->structure;
}

public function setStructure($structure) {
    $this->structure = $structure;
}

And Structure

namespace Structures\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Structures\Entity\Structure
 *
 * @ORM\Table(name="structure")
 * @ORM\Entity(repositoryClass="Structures\Entity\Repository\StructureRepository")
 */

class Structure {


/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 * @ORM\SequenceGenerator(sequenceName="structure_id_seq", allocationSize=1, initialValue=1)

 *
 */
private $id;

/**
 *  @var string address
 *
 * @ORM\Column(name="name", type="string", length=255,nullable=false)
 */
private $name;

/**
 * @var string address
 *
 * @ORM\Column(name="address", type="string", length=255,nullable=false)
 *
 */
private $address;


/**
 * @var integer $rooms
 *
 * @ORM\Column(name="number_of_rooms", type="integer",  nullable=false)
 */
private $number_of_rooms;


/**
 * @ORM\OneToMany(targetEntity="Room", mappedBy="structure")
 */
private $rooms;


public function __construct() {
    $this->rooms = new \Doctrine\Common\Collections\ArrayCollection();
}






public function exchangeArray($data) {
    $this->fillWith($data);
}
public static function createFromArray($data)
{
    $structure = new Structure();
    $structure->fillWith($data);
    return $structure;
}
private function set_Id($i_id) {
    $this->id = $i_id;
}
/**
 * Get id
 * @return integer
 *
 */
public function getId() {
    return $this->id;
}

/**
 * Set name
 * @param string name
 *
 */
public function setName($name) {
    $this->name = $name;
}
/**
 * Get name
 * @return string
 *
 */
public function getName() {
    return $this->name;
}

/**
 * Set address
 * @param string address
 *
 */
public function setAddress($address) {
    $this->address = $address;
}

/**
 * Get address
 * @return string
 *
 */
public function getAddress() {
    return $this->address;
}
/**
 * Set rooms
 * @params integer rooms
 *
 */
public function setRoom($room) {
    $this->rooms = $room;
}

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

/**
 * @param $rooms
 */
public function setNumberOfRooms($number_of_rooms) {
    $this->number_of_rooms = $number_of_rooms;
}
/**
 * @return int
 */
public function getNumberOfRooms() {
    return $this->number_of_rooms;
}

When I create a new Room I call a function to process my Zend\Form\Form data

public function processForm() {
  if ($this->request->isPost()) {
        // get the post data
        $post = $this->request->getPost()->toArray();
        // fill form
        $this->form->setData($post);
        // check if form is valid
        if (!$this->form->isValid()) {
            //prepare view
            $view = new ViewModel(array('form' => $this->form,
                                        'title' => 'Process Form : 
              Some errors during rooms processing'));
            $view->setTemplate('structures/rooms/create');
                return $view;
        }
        /* here I set the structure's Id from $structure obj gets from 
          $this->params('id') 
        */
        $structure = $this->getStructureFromQuerystring();
        $post['structure_id'] = $structure->getId();

        /* here I call a service method to create a Room instance and fills it 
           with data from $this->form
        */
        $return = $this->roomService->insertRoomFromArray($post);

        if ($return ) {
            $this->flashMessenger()->setNamespace('rooms')->addMessage
            ('Operation executed correctly');
            return $this->redirect()->toRoute('rooms/index');

        } 
     }

Service Method does

public function insertRoomFromArray(array $am_formData) {

    /* here I get the structure with the id stores in $am_formData*/
    $structure = $this->getStructure($am_formData['structure_id']);
    /* here I copy the $structure obj in $am_formData array */
    $am_formData['structure'] = $structure;
    /* here I call the Room Entity method createFromArray to create the Room */
    $I_event = Room::createFromArray($am_formData);
    /* here I call the mapper method to persist data */
    $this->I_mapper->saveRoom($I_event);

    return $I_event;

}

Room Entity

public static function createFromArray($data)
{
    /* create new Room obj */
    $room = new Room();
    /* fill with data */
    $room->fillWith($data);
    return $room;
}
public function fillWith($data) {

    $this->id = (isset($data['id'])) ? $data['id'] : null;
    $this->name = (isset($data['name'])) ? $data['name'] : null;
    /* here I copy the structure obj for relationship */
    $this->structure = (isset($data['structure'])) ? $data['structure'] : null;
} 

Mapper Entity public function saveRoom(\Structures\Entity\Room $room) {

    $this->entityManager->persist($room);
    $this->entityManager->flush();

}

It works and the new room has the correct structure's id sets.

What when I have to edit a room ? My solution

From the controller

public function editAction() {

   /* Gets the room obj by $this->params('id') */

   $room = $this->getRoomFromQueryString();


    $this->form->bind($room);

    $view = $this->processEditForm($room);

    if (!$view instanceof ViewModel) {

        $view = new ViewModel(array('form' => $this->form,
            'title' => 'Edit Room: ' . $room->getName() , 
            'structure' => $room->getStructure()));
        $view->setTemplate('structures/rooms/create');

    }

    return $view;

}

private function processEditForm(Room $room)
{
    if ($this->request->isPost()) {
        $post = $this->request->getPost()->toArray();

       /* here I put structure_id in $post array. */

        $post['structure_id'] = $room->getStructure()->getId();

        /* After this , my previous room obj is update with the data come from the
           From, but the $structure entity is NULL. I don't know why. I will use the id
           stores in $post['structure_id'] to fetch the Structure obj*/ 
        $this->form->setData($post);


        if (!$this->form->isValid()) {
            $view = new ViewModel(array('form' => $this->form,
            'title' => 'Some erros during rooms processing'));
            $view->setTemplate('structures/rooms/create');
            return $view;
        }

         $return = $this->roomService->updateRoomFromArray($room);

        if ($return ) {
            $this->flashMessenger()->setNamespace('rooms')->addMessage('Operation executed correctly');
            return $this->redirect()->toRoute('rooms/index');
        }
        else {
            echo "Errore durante Editing della room";
        }

    }


}

The Service Method to update data public function updateRoomFromArray(Room $room) { /* I call the Room Entity method to update my room / $I_event = Room::updateFromArray($room); / persist data */ $this->I_mapper->updateRoom($room); }

public function updateFromArray($room)
{
    /* here I fetch the Structure's relationship my Room obj belongs to */
    $structure_id = $room->getStructureId();
    $structure = $this->getStructure($structure_id);
    /* here I set the link*/
    $room->setStructure($structure);
    return $room;


}
public function updateRoom(\Structures\Entity\Room $room)
{
    $this->entityManager->merge($room);
    $this->entityManager->flush();
}

I'd like to know your opinion about my code, please. Is the right way to work with Doctrine2 ? thanks in advance

Upvotes: 2

Views: 647

Answers (1)

BADAOUI Mohamed
BADAOUI Mohamed

Reputation: 2214

The syntax of your annotation is not correct.

Your ManyToOne association must have this following simple format:

@ManyToOne(targetEntity="Structures\Entity\House", inversedBy="features") @JoinColumn(name="house_id", referencedColumnName="id")

Take a look at the documentation for more informations: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional

Hope this helps.

Upvotes: 0

Related Questions