festevall
festevall

Reputation: 21

Doctrine 2 Relations Mistake

I have a problem with inserting record into my DB. I have 3 tables named notifications: id, id_event, event_id, is_readed comments: id, comment, receiver, sender, created_at messages id, subject, message, receiver, sender, created_at

I want to relate comments and messages id for notifications.id_event with many-to-many relation, which looks like

oneToOne:
    comment:
        targetEntity: Comments
        joinColumn:
            name: id_event
            referencedColumnName: id

    message:
        targetEntity: Messages
        joinColumn:
            name: id_event
            referencedColumnName: id

When I'm trying to insert a record I have the following exception:

Found entity of type Doctrine\Common\Collections\ArrayCollection on association Privatmarket\BusinessBundle\Entity\Notifications#message, but expecting Privatmarket\BusinessBundle\Entity\Messages

And realy don't understand what to do. Maybe I make the mistake in relation choosing ?

The insert code is:

        $comment = new Comments();
        $comment->setSender(rand(1, 10));
        $comment->setReceiver(rand(11, 20));
        $comment->setComment("Lorem ipsum dolor sit amet {$i}, olor sit amet {$i}, psum dolor sit amet {$i}, olor sit amet {$i}, ");
        $em->persist($comment);
        $em->flush();

        $notification = new Notifications();
        $id = $comment->getId();
        $notification->setComment($em->getRepository("PrivatmarketBusinessBundle:Comments")->find($id));
        $notification->setEventId(2);
        $em->persist($notification);
        $em->flush();


        $message = new Messages();
        $message->setSender(rand(1, 10));
        $message->setReceiver(rand(11, 20));
        $message->setSubject("Lorem ipsu{$i}.");
        $message->setMessage("Lorem ipsum dolor sit amet {$i}, olor sit amet {$i}, psum dolor sit amet {$i}, olor sit amet {$i}, Lorem ipsum dolor sit amet {$i}, olor sit amet {$i}, psum dolor sit amet {$i}, olor sit amet {$i}, ");
        $em->persist($message);
        $em->flush();

        $notification = new Notifications();
        $id = $message->getId();
        $notification->setComment($em->getRepository("PrivatmarketBusinessBundle:Messages")->find($id));
        $notification->setEventId(1);
        $em->persist($notification);
        $em->flush();

Upvotes: 0

Views: 45

Answers (1)

john Smith
john Smith

Reputation: 17906

well the error you get reads like change

message:
    targetEntity: Messages
    joinColumn:
        name: id_event
        referencedColumnName: id

to

message:
    targetEntity: Message
    joinColumn:
        name: id_event
        referencedColumnName: id

mind that an doctrine expects an entityName as singular so if its a collection it auto-pluralizes getter and setters, maybe this is a bit mixed up

Upvotes: 0

Related Questions