I'm very new to symfony2 and i want to create a form. The values of fields should base on user data.
I have projects and users with a n:m-relation and now i want to embed a form in twig where I can assign users (multiple with one click) and projects. My class for user looks like:
// src/Pso/LogBundle/Entity/User.php
namespace Pso\LogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="users", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
* @ORM\Entity(repositoryClass="Pso\LogBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=32)
*/
private $salt;
/**
* @ORM\Column(type="string", length=250)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*
*/
private $roles;
The class for project looks like:
// src/Pso/ProjectBundle/Entity/Project.php
namespace Pso\ProjectBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Table(name="project", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
* @ORM\Entity(repositoryClass="Pso\ProjectBundle\Entity\ProjectRepository")
*/
class Project
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true, nullable=false)
*/
private $project_nr;
/**
* @ORM\ManyToOne(targetEntity="Pso\ProjectBundle\Entity\Mandator", inversedBy="projects")
* @ORM\JoinColumn(name="mandator_id", referencedColumnName="id")
*/
private $mandator;
/**
* @ORM\Column(type="string", length=80, nullable=false)
*/
private $project_name;
/**
* @ORM\Column(type="string", length=50)
*/
private $customer;
/**
* @ORM\Column(type="string", length=50)
*/
private $label;
/**
* @ORM\Column(type="date")
*/
private $shipping_date;
/**
* @ORM\Column(type="float")
*/
private $advance_payment;
/**
* @ORM\Column(type="integer", length=1)
*/
private $probability;
/**
* @ORM\Column(type="blob")
*/
private $special_demand;
/**
* @ORM\Column(type="string", length=4)
*/
private $currency;
/**
* @ORM\Column(type="integer", length=1, nullable=false)
*/
private $status;
/**
* @ORM\Column(type="string", length=100)
*/
private $contract_nr;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbinsert;
/**
* @ORM\Column(type="datetime", nullable=false)
*/
private $dbupdate;
/**
* @ORM\ManyToMany(targetEntity="Pso\LogBundle\Entity\User", cascade={"persist", "remove"})
*/
private $users;
public function __construct() {
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
My ProjectuserType looks like:
namespace Pso\ProjectBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityRepository;
class ProjectuserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('project','text')
;
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Pso\LogBundle\Entity\User',
'property' => 'username',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('x')->FROM('PsoLogBundle:user', 'u')
->WHERE('u.id = :id')
->setParameter('id',5)
;
// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->findnotsignedusers();
},
);
// or call a method on your repository that returns the query builder
// the $er is an instance of your UserRepository
// return $er->createOrderByFullNameQueryBuilder();
},
);
// create the field, this is similar the $builder->add()
// field name, field type, data, options
$form->add('user', 'entity', $formOptions);
}
);
}
public function getName()
{
return 'Projectuser';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => null,
));
}
}
With the Type I oriented on the post and on documentation about chapter "How to dynamically Generate Forms Based on user Data"
With my Code I always get the error "Class Pso\ProjectBundle\Form\EntityRepository does not exist". I think I doesn't get right documentation. I don't want to get the data out of my ProjectRepository. This exists but the namespace is Pso\ProjectBundle\Form. But my intention ist to get the data with the querybuilder in my EventListener. Every tipp would be appreciated.