How to implement NotifyPropertyChanged method onPropertyChanged? - symfony

How to implement NotifyPropertyChanged for retrieving entity changes (in Symfony 2.8 project with doctrine)?
I have found several examples, but they are not full:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/change-tracking-policies.html
http://www.boostr.in/26896/is-there-a-built-in-way-to-get-all-of-the-changed-updated-fields-in-a-doctrine-2%23googleads
For me it is not clear if i have to implement the own onPropertyChanged function, how ? I would like to retrive the property changes naming the last and the current properties in the controller and format them as a message for the user.
When i search in the documentation, NotifyPropertyChanged is not described or described minimally:
http://symfony2-document.readthedocs.io/en/latest/search.html?q=NotifyPropertyChanged&check_keywords=yes&area=default
http://api.symfony.com/2.8/search.html?search=NotifyPropertyChanged
http://docs.w3cub.com/symfony~2.8/
http://phpdox.de/demo/Symfony2/interfaces/Doctrine_Common_NotifyPropertyChanged.xhtml
It is written that addPropertyChangedListener() — Adds a listener that wants to be notified about property changes. I do not understand how i should put the custom code in this listener and how to execute this listener in such a way, that i could retrieve the entity changes to the controller action.
// src\MeetingBundle\Entity\EventLisObject.php
<?php
namespace MeetingBundle\Entity;
use Doctrine\Common\NotifyPropertyChanged;
use Doctrine\Common\PropertyChangedListener;
abstract class EventLisObject implements NotifyPropertyChanged
{
private $listeners = array();
public function addPropertyChangedListener(PropertyChangedListener $listener) {
$this->listeners[] = $listener;
}
/** Notifies listeners of a change. */
protected function onPropertyChanged($propName, $oldValue, $newValue) {
if ($this->listeners) {
foreach ($this->listeners as $listener) {
$listener->propertyChanged($this, $propName, $oldValue, $newValue);
}
}
}
}
//C src\MeetingBundle\Entity\Event.php
<?php
namespace MeetingBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Event
*
* #ORM\Table(name="tevent", indexes={#ORM\Index(columns={"keywords"}, flags={"fulltext"})})
* #ORM\Entity(repositoryClass="MeetingBundle\Repository\EventRepository" )
*/
class Event extends EventLisObject
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
* #Assert\NotBlank()
*/
private $title;
/**
* Set title
*
* #param string $title
*
* #return Event
*/
public function setTitle($title) {
// $this->title = $title;
if( $title != $this->title ) {
$this->_onPropertyChanged("title", $this->title, $title);
$this->title = $title; }
return $this;
}
// src\MeetingBundle\Controller\EventMapControllerACL.php
....
/**
* Event controller.
*
* #Route("/eventmapacl")
*/
class EventMapControllerACL extends Controller
{
/**
* #Route("/edit/{id}", name="event_jsMap_edit")
* #Method("GET|POST")
* #Template("MeetingBundle:Event:ev_jsMap_edit.html.twig")
*/
public function editAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MeetingBundle:Event')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
// $deleteForm = $this->createDeleteForm($id);
if ($editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
// here or before flush or before persist i would like to estimate the changes and generate the message to user informing about them.
return $this->redirect($this->generateUrl('event_jsMap_edit', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
);
}

Related

Updating the form data in different Controller Action in Symfony

via EditController I let the user change his item. then I want to handle the update procedure in another Controller (UpdateController). But it does not work! He creates a new Item instead of updating the Item. In UpdateController I used dd (...) I found out that Id and Profile are not being forwarded to UpdateController.
Hopefully you can help me with it.
This is ItemEntity relationship met Profile
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Profile", inversedBy="items")
* #ORM\JoinColumn(nullable=false)
*/
private $profile;
This is ProfileEntity relationship met Items
/**
* #ORM\OneToMany(targetEntity="App\Entity\Items", mappedBy="profile")
*/
private $items;
This is EditController
/**
* #IsGranted("ROLE_USER")
* Class EditController
* #package App\Controller
*/
final class EditController extends AbstractController
{
/**
* #Route("/item/edit/{items}", name="edit_item")
*
* #param Items $items
* #return Response
*/
public function edit(Items $items)
{
$form = $this->createForm(CreateTodoFormType::class, $items, ['action' => $this->generateUrl('update_item')]);
return $this->render('item/edit.html.twig', [
'todoForm' => $form->createView(),
'viewData' => $form->getViewData()
]);
}
}
This is UpdateController
/**
* #IsGranted("ROLE_USER")
* Class UpdateController
* #package App\Controller\Item
*/
final class UpdateController extends AbstractController
{
/**
* #Route("/item/update", name="update_item", methods={"POST"})
*/
public function update(EntityManagerInterface $em, Request $request, TranslatorInterface $translator)
{
$form = $this->createForm(CreateTodoFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$item = $form->getData();
$profile = $em->getRepository(Profile::class)->findProfileById($this->getUser()->getId());
$item->setProfile($profile);
$em->persist($item);
$em->flush();
$this->addFlash('success', $translator->trans('The new ToDo is updated.'));
return $this->redirectToRoute('item');
}
return $this->render('item/edit.html.twig', [
'todoForm' => $form->createView(),
]);
}
}

How can I filter on entities owned by current User

I'm very new to Symfony.
As a User I only want to see the entities I've created.
A User may login and create games but I only want to see the game entities I've created.
It's a ManyToMany relation for User and Games, where User is the owning side.
Example: the game "Game for user Joop1" can only be viewed by user
Joop1. The game with title "Game for Joop2" may only be viewed by
Joop2.
If Joop1 is logged as the current user, how can I only show the games he has created with DQL/querybuilder?
I use FOSUserBundle.
Update: I've changed the relation to:
User->Game: many-to-many.
Question: what is the dql/querybuilder script for this sql statement:
SELECT * FROM game,users_games WHERE users_games.user_id = 1
This is my current querybuilder script based on Manuel DUVERNON answer:
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository( Game::class )->createQueryBuilder( 'game_t' );
$qb->join( 'users_games.user_id', 'ug' )
->where( 'ug.user_id = :userId' )
->setParameter( 'userId', 1);
return $qb->getQuery()->getResult();
But this is not working because I get the following error:
[Semantical Error] line 0, col 70 near '.user_id ug WHERE': Error:
Identification Variable users_games used in join path expression but
was not defined before.
GameController
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\Game;
use AppBundle\Entity\PlayLog;
use AppBundle\Entity\User;
use AppBundle\Form\GameType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\HttpFoundation\Request;
/**
* Game controller.
*
* #Route("game")
*/
class GameController extends Controller
{
/**
* Lists all game entities.
*
* #Route("/", name="game_index")
* #Method("GET")
*/
public function indexAction(Request $request)
{
//get user_id
//
$usr = $this->getUser();
$id = $usr->getId();
//
// $em = $this->getDoctrine()->getManager();
//// $dql="SELECT g FROM AppBundle\Entity\Game g Join g.users u WHERE u.id := user_id";
// $dql = "SELECT game FROM AppBundle:Game game ";
// $query = $em->createQuery($dql);
// $query->setParameter('user_id', $id);
// $current_games = $query->getResult();
// $paginator = $this->get('knp_paginator');
// $result = $paginator->paginate(
//
// $request->query->getInt('page', 1),
// $request->query->getInt('limit', 25)
// );
// dump(get_class($paginator));
// $em = $this->getDoctrine()->getManager();
//
// $current_games = $em->getRepository('AppBundle:Game')->findAll();
// $em = $this->getDoctrine()->getManager();
//
// $current_games = $em->getRepository('AppBundle:Game')->findAllOrdered($id);
//
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository( Game::class )->createQueryBuilder( 'game_t' );
$qb->join( 'users_games.user_id', 'ug' )
->where( 'ug.user_id = :userId' )
->setParameter( 'userId', $id);
return $qb->getQuery()->getResult();
return $this->render('game/index.html.twig', array(
'games' => $current_games,
'max_limit_error' => 25
));
}
/**
* Creates a new game entity.
*
* #Route("/new", name="game_new")
* #Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$game = new Game();
$form = $this->createForm('AppBundle\Form\GameType', $game);
$form->handleRequest($request);
$game->setUser($this->getUser());
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($game);
$em->flush($game);
return $this->redirectToRoute('game_show', array('id' => $game->getId()));
}
return $this->render('game/new.html.twig', array(
'game' => $game,
'form' => $form->createView(),
));
}
/**
* Finds and displays a game entity.
*
* #Route("/{id}", name="game_show")
* #Method("GET")
*/
public function showAction(Game $game)
{
$deleteForm = $this->createDeleteForm($game);
return $this->render('game/show.html.twig', array(
'game' => $game,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing game entity.
*
* #Route("/{id}/edit", name="game_edit")
* #Method({"GET", "POST"})
*/
public function editAction(Request $request, Game $game)
{
$deleteForm = $this->createDeleteForm($game);
$editForm = $this->createForm('AppBundle\Form\GameType', $game);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('game_show', array('id' => $game->getId()));
}
return $this->render('game/edit.html.twig', array(
'game' => $game,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing game entity.
*
* #Route("/{id}/log", name="game_log")
* #Method({"GET", "POST"})
*/
public function addLogAction(Request $request, Game $game)
{
$playlog = new PlayLog();
$form = $this->createForm(GameType::class, $game);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
//Save playLog
$em = $this->getDoctrine()->getManager();
$em->persist($playlog);
$em->flush();
}
// Render / return view incl. formulier.
return $this->render('game/log.html.twig', array(
'game' => $game,
'form' => $form->createView(),
));
}
/**
* Deletes a game entity.
*
* #Route("/{id}", name="game_delete")
* #Method("DELETE")
*/
public function deleteAction(Request $request, Game $game)
{
$form = $this->createDeleteForm($game);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($game);
$em->flush($game);
}
return $this->redirectToRoute('game_index');
}
/**
* Creates a form to delete a game entity.
*
* #param Game $game The game entity
*
* #return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(Game $game)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('game_delete', array('id' => $game->getId())))
->setMethod('DELETE')
->getForm()
;
}
}
Game Entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Game
*
* #ORM\Table(name="game")
* #ORM\Entity(repositoryClass="AppBundle\Repository\GameRepository")
*/
class Game
{
/**
* #ORM\OneToMany(targetEntity="PlayLog", mappedBy="game")
* #ORM\OrderBy({"date" = "DESC"})
*
*/
private $playlogs;
public function __construct()
{
$this->playlogs = new ArrayCollection();
}
/**
* #ORM\ManyToOne(targetEntity="Type", inversedBy="games")
* #ORM\JoinColumn(name="type_id", referencedColumnName="id")
*/
private $type;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="games")
*/
private $user;
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
* #Assert\Length(
* min = "3",
* max = "100"
* )
* #ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* #param mixed $user
*/
public function setUser($user)
{
$this->user = $user;
}
/**
* Set name
*
* #param string $name
*
* #return Game
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #return mixed
*/
public function getType()
{
return $this->type;
}
/**
* #param mixed $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* #return mixed
*/
public function getPlaylogs()
{
return $this->playlogs;
}
/**
* #param mixed $playlogs
*/
public function setPlaylogs($playlogs)
{
$this->playlogs = $playlogs;
}
public function addPlayLog(PlayLog $playlog)
{
$this->playlog->add($playlog);
$playlog->setPlayLogs($this);
}
}
You can just get the current logged user, then request your dql query using that user id .
Take a look :
$usr= $this->getUser();
$id=$usr->getId();
$em=$this->getDoctrine()->getManager();
$dql="SELECT * games FROM Game WHERE Game.user_id := user_id";
$query = $em->createQuery($dql);
$query->setParameter('user_id',$id);
$current_games = $query->getResult();

symfony object could not be converted to string

i'm newbie in symfony and I'm trying to implement a Data Transformer. I followed the documentation and looked different solutions posted in here. But I haven't found what is wrong with my code.
I'm getting this error:
Catchable Fatal Error: Object of class AppBundle\Entity\Todo could not be converted to string
If someone knows another post with the solution, please tell me know where I could look for it.
Thank you in advance.
So, these are my Entities. Todo class
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Todo
*
* #ORM\Table(name="todo")
* #ORM\Entity(repositoryClass="AppBundle\Repository\TodoRepository")
*/
class Todo
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=20)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="category", type="string", length=20)
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=10)
*/
private $description;
/**
* #var string
*
* #ORM\Column(name="priority", type="string", length=10)
*/
private $priority;
/**
*
* #ORM\ManyToOne(
* targetEntity="AppBundle\Entity\User",
* inversedBy="todos"
* )
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $creatorId;
//... getters and setters
User class:
<?php
// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* #ORM\OneToMany(
* targetEntity="AppBundle\Entity\Todo",
* mappedBy="creatorId"
* )
*/
private $todos;
}
In the User class, I don't have getter/setters
My TodoType
class TodoType extends AbstractType{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'label' => 'Name',
'required' => true
)
)
->add('category', TextType::class, array(
'label' => 'Category'
)
)
->add('priority', EntityType::class, array(
'class' => 'AppBundle:Todo',
'choice_label' => 'priority',
)
)
->add('creatorId', TextType::class, array(
'label' => 'creator Id:',
));
$builder->get('creatorId')
->addModelTransformer(new IssueToNumberTransformer($this->manager));
}
public function getName()
{
return 'todo';
}
}
Transformer
<?php
namespace FOS\UserBundle\Form\DataTransformer;
use AppBundle\Entity\User;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class IssueToNumberTransformer implements DataTransformerInterface
{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
/**
* Transforms an object (creatorId) to a string (number).
*
* #param creatorId|null $creatorId
* #return string
*/
public function transform($creatorId)
{
if (null === $creatorId) {
return '';
}
return $creatorId->getId();
}
/**
* Transforms a string (number) to an object (creatorId).
*
* #param string $creatorId
* #return creatorId|null
* #throws TransformationFailedException if object (creatorId) is not found.
*/
public function reverseTransform($creatorId)
{
// no issue number? It's optional, so that's ok
if (!$creatorId) {
return;
}
$creatorId = $this->manager
->getRepository('AppBundle:User')
// query for the issue with this id
->find($creatorId);
if (null === $creatorId) {
throw new TransformationFailedException(sprintf(
'A user with number "%s" does not exist!',
$creatorId
));
}
return $creatorId;
}
}
Controller (function)
public function createAction(Request $request){
$em = $this->getDoctrine()->getManager();
// 1) build the form
$todo = new Todo();
$form = $this->createForm(new TodoType($em), $todo);
// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// 3) save the Todo!
$em->persist($todo);
$em->flush();
return $this->redirectToRoute('todo_create');
}
return $this->render('todo/create.html.twig', array(
'todo'=>$todo,
'form'=>$form->createView()
));
}
It's at this part:
return $this->render('todo/create.html.twig', array(
'todo'=>$todo,
'form'=>$form->createView()
));
Because you are trying to pass the $todo object to your Twig template. You can't do that. What do you need? If you just need the Todo name and you have created getters/setter, you could instead do like this:
return $this->render('todo/create.html.twig', array(
'todo_name' => $todo->getName(),
'form' => $form->createView()
));
Hopefully that makes sense.
EDIT #2 based on comments.
Also this line is wrong in your controller:
$form = $this->createForm(new TodoType($em), $todo);
Should be like this:
$form = $this->createForm(TodoType::class, $todo);

Symfony2 FOSUserBundle Invitation : only work on owning side associations

Entity/User
namespace My\SampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends \FOS\UserBundle\Entity\User
{
/** #ORM\Id #ORM\Column(type="integer") #ORM\GeneratedValue(strategy="AUTO") */
protected $id;
/**
* #ORM\OneToOne(targetEntity="Invitation", inversedBy="user")
* #ORM\JoinColumn(referencedColumnName="code")
* #Assert\NotNull(message="Your invitation is wrong")
*/
protected $invitation;
public function setInvitation(Invitation $invitation)
{
$this->invitation = $invitation;
}
public function getInvitation()
{
return $this->invitation;
}
}
Entity/Invitation
namespace My\SampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** #ORM\Entity */
class Invitation
{
/** #ORM\OneToOne(targetEntity="User", mappedBy="invitation", cascade={"persist", "merge"}) */
protected $user;
/** #ORM\Id #ORM\Column(type="string", length=6) */
protected $code;
/** #ORM\Column(type="string", length=256) */
protected $email;
/**
* When sending invitation be sure to set this value to `true`
*
* It can prevent invitations from being sent twice
*
* #ORM\Column(type="boolean")
*/
protected $sent = false;
public function __construct()
{
// generate identifier only once, here a 6 characters length code
$this->code = substr(md5(uniqid(rand(), true)), 0, 6);
}
public function getCode()
{
return $this->code;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function isSent()
{
return $this->sent;
}
public function send()
{
$this->sent = true;
}
public function getUser()
{
return $this->user;
}
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Set code
*
* #param string $code
* #return Invitation
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Set sent
*
* #param boolean $sent
* #return Invitation
*/
public function setSent($sent)
{
$this->sent = $sent;
return $this;
}
/**
* Get sent
*
* #return boolean
*/
public function getSent()
{
return $this->sent;
}
}
Error
You cannot search for the association field
'My\SampleBundle\Entity\Invitation#user', because it is the inverse
side of an association. Find methods only work on owning side
associations. 500 Internal Server Error - ORMException
I am the stage which just performed the documentation.
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/adding_invitation_registration.md
The display of the bundle is normal. However, It has become an error if I submit registration form.
Any help?
EDIT:
Actually, I had set up 'inversedBy' at first.
A pre-question.
Symfony2 FOSUserBundle Invitation : 'inversedBy' mapping errors
On the surface, it does work. However, mapping errors is displayed by profiler.
My\SampleBundle\Entity\Invitation# does not contain the required
'inversedBy' attribute.
so, I changed it in response to advice.
I don't know what to think of it.
It's just as the error says. Instead of mappedBy, you should use inversedBy on the Invitation entity and use mappedBy on the User entity for $invitation.
/** #ORM\OneToOne(targetEntity="User", inversedBy="invitation", cascade={"persist", "merge"}) */
protected $user;
You can also overcome this problem by creating a custom repository method to find user based on invitation.
There was solution.
https://github.com/FriendsOfSymfony/FOSUserBundle/issues/800
public function reverseTransform($value)
{
// ...
return $this->entityManager
->getRepository('My\SampleBundle\Entity\Invitation')
->findOneBy(array(
'code' => $value,
'user' => null, <= Removing 'user' solves the issue
));
}
I ended up modifying my transformer to the following:
public function reverseTransform($value)
{
if (null === $value || '' === $value) {
return null;
}
if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}
$invitation = $this->entityManager
->getRepository('SixString\PearBundle\Entity\Invitation')
->findOneBy(array(
'code' => $value,
));
if($this->entityManager->getRepository('SixString\PearBundle\Entity\User')->findOneBy(array("invitation" => $invitation))){
return null;
}
return $invitation;
}
I stripped out the 'user' => null but added a check to see if the invitation has already been used

Catchable Fatal Error: Argument 1 passed to ? Symfony2

I am stuck and frustrated with the bellow error message:
Catchable Fatal Error: Argument 1 passed to Medicine\UserBundle\Entity\User
::setUsertype() must be an instance of Medicine\UserBundle\Entity\Usertype,
instance of Doctrine\Common\Collections\ArrayCollection given, called in
/opt/lampp/htdocs/drugs/vendor/symfony/src/Symfony/Component/Form/Util
/PropertyPath.php on line 347 and defined in /opt/lampp/htdocs/drugs/src/
Medicine/UserBundle/Entity/User.php line 224
What I think this error is due to use of manytoone field in my entity, I even tried with keeping onetomany in another entity.
I have a user entity and a usertype entity, the usertype_id is a manytoone field in user table. here is the code for both the entities:-
User
namespace Medicine\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UserRepository")
* #ORM\Table(name="user")
* #ORM\HasLifecycleCallbacks()
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\HasLifecycleCallbacks()
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $username;
/**
* #ORM\ManyToOne(targetEntity="Usertype", inversedBy="users")
* #ORM\JoinColumn(name="usertype_id", referencedColumnName="id")
*/
protected $usertype;
/**
* #ORM\Column(type="string")
*/
protected $image;
/**
* Set usertype
*
* #param Medicine\UserBundle\Entity\Usertype $usertype
*/
public function setUsertype(\Medicine\UserBundle\Entity\Usertype $usertype)
{
$this->usertype = $usertype;
}
/**
* Get usertype
*
* #return Medicine\UserBundle\Entity\Usertype
*/
public function getUsertype()
{
return $this->usertype;
}
}
I am just showing the concerned code, i have all the getter and setter methods for the above code.
UserType
namespace Medicine\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UsertypeRepository")
* #ORM\Table(name="usertype")
* #ORM\HasLifecycleCallbacks()
*/
class Usertype
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\HasLifecycleCallbacks()
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $name;
/**
* #ORM\OneToMany(targetEntity="User", mappedBy="usertype")
*/
protected $users;
public function __construct()
{
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add users
*
* #param Medicine\UserBundle\Entity\User $users
*/
public function addUser(\Medicine\UserBundle\Entity\User $users)
{
$this->users[] = $users;
}
/**
* Get users
*
* #return Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
Controller
This Executes when a user wants to login. He will fill in the username password and a UserType:
public function indexAction()
{
$entity = new User();
$form = $this->createForm(new LoginForm(), $entity);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
echo "<pre>"; print_r($entity->getUsertype()); exit;
$em = $this->getDoctrine()
->getEntityManager();
$em->persist($entity);
$userrepository = $em->getRepository('MedicineUserBundle:User');
echo "<pre>"; print_r($entity->getUsertype()); exit;
$all = $userrepository->findOneBy(array('login' => $entity->getLogin(), 'password' => $entity->getPassword()));
if($all)
{
return $this->redirect($this->generateUrl('MedicineUserBundle_login'));
}
}
}
return $this->render('MedicineUserBundle:User:loginpage.html.twig',array(
'form' => $form->createView()
));
}
Login Form
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('login', 'text', array('label' => 'Username',))
->add('password','password')
->add('usertype', 'entity', array('class' => 'MedicineUserBundle:Usertype', 'property'=>'name', 'multiple' => true, ))
;
}
The 'multiple' => true in combination with your entity association definition is causing this problem.
You should find that if you change multiple to false (and as such can only select one UserType for your User), things work properly.
If you want multiple UserTypes for one User, you have a Many-to-Many association - one user can have many UserTypes, and one UserType can have many Users. See Doctrine's ManyToMany association type to implement this. Documentation Here.
Hope this helps.

Resources