add role in zfcuser form registration - zfcuser

i am using zfcuser, bjyauthorize et roleuserbridge.
i want to add a field in the form registration. I followed this tutorial step :
http://resoftsol.com/adding-custom-fields-to-zfcuser-register-form/
in the module front i have added :
- the directory entity with files user et userinterface:
namespace Front\Entity;
interface UserInterface
{
/**
* Get id.
*
* #return int
*/
public function getId();
/**
* Set id.
*
* #param int $id
* #return UserInterface
*/
public function setId($id);
/**
* Get username.
*
* #return string
*/
public function getUsername();
/**
* Set username.
*
* #param string $username
* #return UserInterface
*/
public function setUsername($username);
/**
* Get email.
*
* #return string
*/
public function getEmail();
/**
* Set email.
*
* #param string $email
* #return UserInterface
*/
public function setEmail($email);
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName();
/**
* Set displayName.
*
* #param string $displayName
* #return UserInterface
*/
public function setDisplayName($displayName);
/**
* Get password.
*
* #return string password
*/
public function getPassword();
/**
* Set password.
*
* #param string $password
* #return UserInterface
*/
public function setPassword($password);
/**
* Get state.
*
* #return int
*/
public function getState();
/**
* Set state.
*
* #param int $state
* #return UserInterface
*/
public function setState($state);
/**
* Get role.
*
* #return string
*/
public function getRole();
/**
* Set role.
*
* #param string $role
* #return UserInterface
*/
public function setRole($role);
}
++++++++++++++++++++
namespace Font\Entity;
class User implements UserInterface
{
/**
* #var int
*/
protected $id;
/**
* #var string
*/
protected $username;
/**
* #var string
*/
protected $email;
/**
* #var string
*/
protected $displayName;
/**
* #var string
*/
protected $password;
/**
* #var int
*/
protected $state;
/**
* #var string
*/
protected $role;
/**
* Get id.
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id.
*
* #param int $id
* #return UserInterface
*/
public function setId($id)
{
$this->id = (int) $id;
return $this;
}
/**
* Get username.
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username.
*
* #param string $username
* #return UserInterface
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get email.
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email.
*
* #param string $email
* #return UserInterface
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get displayName.
*
* #return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* Set displayName.
*
* #param string $displayName
* #return UserInterface
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
return $this;
}
/**
* Get password.
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set password.
*
* #param string $password
* #return UserInterface
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get state.
*
* #return int
*/
public function getState()
{
return $this->state;
}
/**
* Set state.
*
* #param int $state
* #return UserInterface
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get role.
*
* #return string
*/
public function getRole()
{
return $this->role;
}
/**
* Set role.
*
* #param string $role
* #return UserInterface
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
}
++++++++++++++++++++++++++
also i have add the mapp directory.
i had the following error :
Catchable fatal error: Argument 1 passed to ZfcUser\Validator\AbstractRecord::setMapper()
must be an instance of ZfcUser\Mapper\UserInterface, instance of Front\Mapper\User given,
called in C:\wamppp\www\projet\vendor\zendframework\zendframework\library\Zend\Validator
\AbstractValidator.php on line 139 and defined in C:\wamppp\www\projet\vendor\zf-commons
\zfc-user\src\ZfcUser\Validator\AbstractRecord.php on line 65

I just had the problem myself and was able to solve it... finally.
Copy the ZfcUser\Validator folder into your module.
Adapt the namespace and change the expected Object type of the method AbstractRecord::setMapper to your user entity / interface, whatever you have there now.
Also, in the code you provided the namespaces arent identical. Yout got "Front" and "Font" there.
Edit: forgot the important part xD
After you have done that you need the following code in the config file (my module is called User):
'zfcuser_register_form' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$form = new ZfcUser\Form\Register(null, $options);
//$form->setCaptchaElement($sm->get('zfcuser_captcha_element'));
$form->setInputFilter(new ZfcUser\Form\RegisterFilter(
new User\Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'email'
)),
new User\Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'username'
)),
$options
));
return $form;
},
I hope this helps.

You should not create your own interface(and if you do, your new interface should extend ZfcUser\Entity\UserInterface), instead just make your user entity extend the ZfcUser\Entity\UserInterface.

Related

Symfony 2 Doctrine ODM returning errors on existing fields

I have a simple symfony 2 setup with Doctrine ORM and a db with some underscore seperated field names (for instance "error_page"). Querying this never gives a result (getTitle does give a result, getErrorPage is always empty) and symfony gives me an error:
Method "error_page" for object "My\CmsBundle\Document\Website" does not exist in MyCmsBundle:Default:dashboard.html.twig at line 5
I can't figure out why... My Document looks like this:
<?php
// src/My/CmsBundle/Document/Website.php
namespace My\CmsBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\Document(
* collection="websites"
* )
*/
class Website
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $slug;
/**
* #MongoDB\Field(type="string", name="error_page")
*/
protected $error_page = "";
/**
* #MongoDB\String
*/
protected $title;
/**
* #MongoDB\String(name="seo_title")
*/
protected $seo_title;
/**
* #MongoDB\String
*/
protected $seo_description;
/**
* #MongoDB\Collection
*/
protected $url = array();
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set slug
*
* #param string $slug
* #return self
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string $slug
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set title
*
* #param string $title
* #return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string $title
*/
public function getTitle()
{
return $this->title;
}
/**
* Set errorPage
*
* #param string $errorPage
* #return self
*/
public function setErrorPage($errorPage)
{
$this->error_page = $errorPage;
return $this;
}
/**
* Get errorPage
*
* #return string $errorPage
*/
public function getErrorPage()
{
return $this->error_page;
}
/**
* Set url
*
* #param collection $url
* #return self
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* #return collection $url
*/
public function getUrl()
{
return $this->url;
}
/**
* Set seoTitle
*
* #param string $seoTitle
* #return self
*/
public function setSeoTitle($seoTitle)
{
$this->seo_title = $seoTitle;
return $this;
}
/**
* Get seoTitle
*
* #return string $seoTitle
*/
public function getSeoTitle()
{
return $this->seo_title;
}
/**
* Set seoDescription
*
* #param string $seoDescription
* #return self
*/
public function setSeoDescription($seoDescription)
{
$this->seo_description = $seoDescription;
return $this;
}
/**
* Get seoDescription
*
* #return string $seoDescription
*/
public function getSeoDescription()
{
return $this->seo_description;
}
}
Document creation via this document works fine by the way. The field name is also set to error_page as expected... I'm at a loss here :S
Actually, Doctrine+Symfony2 assume camel case variable naming. Twig using the getter method names should be obvious, how should it access protected/private variables? It needs a name for something public : the getter. You're probably wondering why "get" is ignored; it is a simplification for designers as they normally shouldnt know about what "getters" are and the difference between methods and variables.
so in your twig file ,change :
{{document.error_page}}
to
{{document.errorPage}}
this would helpful.

Fill my form with the data that I pick up from a table - Symfony2

My problem is this : I have an entity that contains user data , such as:
age
name
surname
address
phone
...
My goal is to make a form where using a query can bring all this data and fill my form with the data , so that the user can modify it if you ever change the cell for example, or changing the direction of his house.
The company and I have made the entity​​ and the form, and consulting the table will do. My problem is how to fill the form fields with the data that I get from the table.
My entity (DatosEntity):
<?php
namespace Proyecto\LavocBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Datos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Proyecto\LavocBundle\Entity\DatosRepository")
*/
class Datos
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="empresa", type="string", length=50)
*/
private $empresa;
/**
* #var integer
*
* #ORM\Column(name="cuit", type="integer")
*/
private $cuit;
/**
* #var string
*
* #ORM\Column(name="localidad", type="string", length=50)
*/
private $localidad;
/**
* #var string
*
* #ORM\Column(name="calle", type="string", length=40)
*/
private $calle;
/**
* #var integer
*
* #ORM\Column(name="altura", type="integer")
*/
private $altura;
/**
* #var integer
*
* #ORM\Column(name="areaTel", type="integer")
*/
private $areaTel;
/**
* #var integer
*
* #ORM\Column(name="telefono", type="integer")
*/
private $telefono;
/**
* #var integer
*
* #ORM\Column(name="areaCel", type="integer")
*/
private $areaCel;
/**
* #var integer
*
* #ORM\Column(name="celular", type="integer")
*/
private $celular;
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="datos")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $personales;
/**
* #var string
*
* #ORM\Column(name="email", type="string")
*/
private $email;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set empresa
*
* #param string $empresa
* #return Datos
*/
public function setEmpresa($empresa)
{
$this->empresa = $empresa;
return $this;
}
/**
* Get empresa
*
* #return string
*/
public function getEmpresa()
{
return $this->empresa;
}
/**
* Set cuit
*
* #param integer $cuit
* #return Datos
*/
public function setCuit($cuit)
{
$this->cuit = $cuit;
return $this;
}
/**
* Get cuit
*
* #return integer
*/
public function getCuit()
{
return $this->cuit;
}
/**
* Set localidad
*
* #param string $localidad
* #return Datos
*/
public function setLocalidad($localidad)
{
$this->localidad = $localidad;
return $this;
}
/**
* Get localidad
*
* #return string
*/
public function getLocalidad()
{
return $this->localidad;
}
/**
* Set calle
*
* #param string $calle
* #return Datos
*/
public function setCalle($calle)
{
$this->calle = $calle;
return $this;
}
/**
* Get calle
*
* #return string
*/
public function getCalle()
{
return $this->calle;
}
/**
* Set altura
*
* #param integer $altura
* #return Datos
*/
public function setAltura($altura)
{
$this->altura = $altura;
return $this;
}
/**
* Get altura
*
* #return integer
*/
public function getAltura()
{
return $this->altura;
}
/**
* Set telefono
*
* #param integer $telefono
* #return Datos
*/
public function setTelefono($telefono)
{
$this->telefono = $telefono;
return $this;
}
/**
* Get telefono
*
* #return integer
*/
public function getTelefono()
{
return $this->telefono;
}
/**
* Set area
*
* #param integer $area
* #return Datos
*/
public function setAreaTel($areaTel)
{
$this->areaTel = $areaTel;
return $this;
}
/**
* Get area
*
* #return integer
*/
public function getAreaTel()
{
return $this->areaTel;
}
/**
* Set celular
*
* #param integer $celular
* #return Datos
*/
public function setCelular($celular)
{
$this->celular = $celular;
return $this;
}
/**
* Get celular
*
* #return integer
*/
public function getCelular()
{
return $this->celular;
}
/**
* Set areaCel
*
* #param integer $areaCel
* #return Datos
*/
public function setAreaCel($areaCel)
{
$this->areaCel = $areaCel;
return $this;
}
/**
* Get areaCel
*
* #return integer
*/
public function getAreaCel()
{
return $this->areaCel;
}
/**
* Set email
*
* #param integer $email
* #return Datos
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return integer
*/
public function getEmail()
{
return $this->email;
}
/**
* Set personales
*
* #param string $personales
* #return Datos
*/
public function setPersonales($personales)
{
$this->personales = $personales;
return $this;
}
/**
* Get personales
*
* #return string
*/
public function getPersonales()
{
return $this->personales;
}
}
My DatosType:
<?php
namespace Proyecto\LavocBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class DatosType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('empresa');
$builder->add('cuit');
$builder->add('localidad');
$builder->add('calle');
$builder->add('altura');
$builder->add('areaTel');
$builder->add('telefono');
$builder->add('areaCel');
$builder->add('celular');
}
public function getName()
{
return 'datos_form';
}
}
The consultation on the controller but did not know how to do it. Now , do not like taking the data I receive and dump them in the form.
Thank you very much and sorry for the trouble
I think what you want to do can be done by following these steps :
Start by collecting your User data in a variable we will call $user
Then, give this $userto be hydrated by your form, like this :
$form = $this->createForm(new UserType(), $user);
Treat your form like you would normaly do. The values found in the entity should be automaticaly filled in your form.
Hope this helps.
Your action could be something like this:
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('LavocBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$editForm = $this->createForm(new UserType(), $entity);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
);
}

Lifecycle Callbacks not triggering in my embedded form

It would appear my lifecycle callbacks are not triggered when the form to upload my file is embedded in another form. If the upload form is on its own the lifecycle callbacks are triggered.
I have a form that creates a 'user' entity, for this user I have a one-on-one relationship with the 'ProfilePicture' entity which has the lifecycle callbacks, I want to upload the profilepicture file on the same form. I followed the "How to handle File Uploads" cookbook, but it doesn't explain how to handle embedded forms.
UserType
<?php
namespace CashBack\AdminBundle\Form\Type;
use CashBack\DefaultBundle\Entity\ProfilePicture;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* additional fields that should not be saved in this object need ->add('agreeWithTerms', null,array('mapped' => false))*/
$builder
->add('id','hidden',array('mapped' => false))
->add('username')
->add('password')
->add('firstname')
->add('lastname')
->add('email')
->add('gender')
->add('dateOfBirth','birthday')
->add('customrole')
->add('profilepicture', new ProfilePictureType())
->add('save', 'submit');
}
/* Identifier */
public function getName()
{
return 'User';
}
/* Makes sure the form doesn't need to guess the date type */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CashBack\DefaultBundle\Entity\User',
'cascade_validation' => true,
));
}
}
ProfilePictureType
<?php
namespace CashBack\AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProfilePictureType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* additional fields that should not be saved in this object need ->add('agreeWithTerms', null,array('mapped' => false))*/
$builder
->add('id','hidden',array('mapped' => false))
->add('file','file');
}
/* Identifier */
public function getName()
{
return 'ProfilePicture';
}
/* Makes sure the form doesn't need to guess the date type */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CashBack\DefaultBundle\Entity\ProfilePicture',
));
}
}
ProfilePicture Entity
<?php
namespace CashBack\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class ProfilePicture
{
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
private $temp;
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename . '.' . $this->getFile()->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir() . '/' . $this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* #param string $path
*
* #return ProfilePicture
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="profilepicture")
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
*/
protected $user;
/**
* Set user
*
* #param \CashBack\DefaultBundle\Entity\User $user
*
* #return ProfilePicture
*/
public function setUser(\CashBack\DefaultBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \CashBack\DefaultBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
User Entity
<?php
namespace CashBack\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User
{
/**
* #var string
*
* #ORM\Column(name="FirstName", type="string", length=10, nullable=true)
*/
private $firstname;
/**
* #var string
*
* #ORM\Column(name="LastName", type="string", length=20, nullable=true)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="Gender", type="string", length=1, nullable=true)
*/
private $gender;
/**
* #var string
*
* #ORM\Column(name="Email", type="string", length=50, nullable=false)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="DateOfBirth", type="date", nullable=false)
*/
private $dateofbirth;
/**
* #var string
*
* #ORM\Column(name="Username", type="string", length=50, nullable=false)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="Password", type="string", length=100, nullable=false)
*/
private $password;
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Tag", inversedBy="user")
* #ORM\JoinTable(name="user_tag",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="tag_id", referencedColumnName="Id")
* }
* )
*/
private $tag;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Customrole", inversedBy="user")
* #ORM\JoinTable(name="user_customrole",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="customrole_id", referencedColumnName="Id")
* }
* )
*/
private $customrole;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Shop", inversedBy="user")
* #ORM\JoinTable(name="user_shop",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="shop_id", referencedColumnName="Id")
* }
* )
*/
private $shop;
/**
* Constructor
*/
public function __construct()
{
$this->tag = new \Doctrine\Common\Collections\ArrayCollection();
$this->customrole = new \Doctrine\Common\Collections\ArrayCollection();
$this->shop = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* 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 setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set email
*
* #param string $email
*
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set dateofbirth
*
* #param \DateTime $dateofbirth
*
* #return User
*/
public function setDateofbirth($dateofbirth)
{
$this->dateofbirth = $dateofbirth;
return $this;
}
/**
* Get dateofbirth
*
* #return \DateTime
*/
public function getDateofbirth()
{
return $this->dateofbirth;
}
/**
* Set username
*
* #param string $username
*
* #return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* #param string $password
*
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add tag
*
* #param \CashBack\DefaultBundle\Entity\Tag $tag
*
* #return User
*/
public function addTag(\CashBack\DefaultBundle\Entity\Tag $tag)
{
$this->tag[] = $tag;
return $this;
}
/**
* Remove tag
*
* #param \CashBack\DefaultBundle\Entity\Tag $tag
*/
public function removeTag(\CashBack\DefaultBundle\Entity\Tag $tag)
{
$this->tag->removeElement($tag);
}
/**
* Get tag
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTag()
{
return $this->tag;
}
/**
* Add customrole
*
* #param \CashBack\DefaultBundle\Entity\Customrole $customrole
*
* #return User
*/
public function addCustomrole(\CashBack\DefaultBundle\Entity\Customrole $customrole)
{
$this->customrole[] = $customrole;
return $this;
}
/**
* Remove customrole
*
* #param \CashBack\DefaultBundle\Entity\Customrole $customrole
*/
public function removeCustomrole(\CashBack\DefaultBundle\Entity\Customrole $customrole)
{
$this->customrole->removeElement($customrole);
}
/**
* Get customrole
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCustomrole()
{
return $this->customrole;
}
/**
* Add shop
*
* #param \CashBack\DefaultBundle\Entity\Shop $shop
*
* #return User
*/
public function addShop(\CashBack\DefaultBundle\Entity\Shop $shop)
{
$this->shop[] = $shop;
return $this;
}
/**
* Remove shop
*
* #param \CashBack\DefaultBundle\Entity\Shop $shop
*/
public function removeShop(\CashBack\DefaultBundle\Entity\Shop $shop)
{
$this->shop->removeElement($shop);
}
/**
* Get shop
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getShop()
{
return $this->shop;
}
/** #ORM\OneToOne(targetEntity="ProfilePicture", mappedBy="user", cascade={"persist", "all"}) */
protected $profilepicture;
/**
* Set profilepicture
*
* #param \CashBack\DefaultBundle\Entity\ProfilePicture $profilepicture
*
* #return User
*/
public function setProfilepicture(\CashBack\DefaultBundle\Entity\ProfilePicture $profilepicture)
{
$this->profilepicture = $profilepicture;
$profilepicture->setUser($this);
return $this;
}
/**
* Get profilepicture
*
* #return \CashBack\DefaultBundle\Entity\ProfilePicture
*/
public function getProfilepicture()
{
return $this->profilepicture;
}
}
User controller
//adds a new entity from data received via Ajax, no redirect
public function addAjaxAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($request);
$user = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
//prepare the response, e.g.
$response = array("code" => 100, "success" => true);
//you can return result as JSON , remember to 'use' Response!
return new Response(json_encode($response));
}
EDIT: when checking the profiler I saw that the following object is submitted in the form:
If i check the profiler I see the following object being submitted in the form: {"username":"test","password":"test","firstname":"test","lastname":"test","email":"test","gender":"t","dateOfBirth":{"month":"1","day":"1","year":"1902"},"customrole":["2"],"id":"","profilepicture":{"id":""},"_token":"YUDiZLi8dY6jtmEhZWk6ivnH3vsQIpnM_fxQ3ClJ2Gw"}
Profile picture is thus empty.

Create and save a value object in CommandController

im trying to create and save a value object in my ImportCommandController.php, but only the entity will be saved.
Let me show some code:
// Create Entity "Fewo"
$fewo = new Fewo();
$fewo->setTitle('MyFewo');
...
// Create Value Object "Period"
$period = new Period();
$period->setTitle('MyTestTitle');
...
$fewo->addPeriod($period);
$this->fewoRepository->add($fewo);
$this->persistenceManager->persistAll();
Now the Fewo is in the database, but the period-table is still empty. I can't find my mistake...
UPDATE:
This is the Period Model:
<?php
namespace TYPO3\Fewo\Domain\Model;
class Period extends \TYPO3\CMS\Extbase\DomainObject\AbstractValueObject {
/**
* Name der Saison
*
* #var \string
*/
protected $name;
/**
* Von
*
* #var \DateTime
*/
protected $begin;
/**
* Bis
*
* #var \DateTime
*/
protected $end;
/**
* rentalcharges
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge>
*/
protected $rentalcharges;
/**
* __construct
*
* #return Period
*/
public function __construct() {
//Do not remove the next line: It would break the functionality
$this->initStorageObjects();
}
/**
* Initializes all ObjectStorage properties.
*
* #return void
*/
protected function initStorageObjects() {
/**
* Do not modify this method!
* It will be rewritten on each save in the extension builder
* You may modify the constructor of this class instead
*/
$this->rentalcharges = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
/**
* Returns the name
*
* #return \string $name
*/
public function getName() {
return $this->name;
}
/**
* Sets the name
*
* #param \string $name
* #return void
*/
public function setName($name) {
$this->name = $name;
}
/**
* Returns the begin
*
* #return \DateTime $begin
*/
public function getBegin() {
return $this->begin;
}
/**
* Sets the begin
*
* #param \DateTime $begin
* #return void
*/
public function setBegin($begin) {
$this->begin = $begin;
}
/**
* Returns the end
*
* #return \DateTime $end
*/
public function getEnd() {
return $this->end;
}
/**
* Sets the end
*
* #param \DateTime $end
* #return void
*/
public function setEnd($end) {
$this->end = $end;
}
/**
* Adds a Rentalcharge
*
* #param \TYPO3\Fewo\Domain\Model\Rentalcharge $rentalcharge
* #return void
*/
public function addRentalcharge(\TYPO3\Fewo\Domain\Model\Rentalcharge $rentalcharge) {
$this->rentalcharges->attach($rentalcharge);
}
/**
* Removes a Rentalcharge
*
* #param \TYPO3\Fewo\Domain\Model\Rentalcharge $rentalchargeToRemove The Rentalcharge to be removed
* #return void
*/
public function removeRentalcharge(\TYPO3\Fewo\Domain\Model\Rentalcharge $rentalchargeToRemove) {
$this->rentalcharges->detach($rentalchargeToRemove);
}
/**
* Returns the rentalcharges
*
* #return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge> $rentalcharges
*/
public function getRentalcharges() {
return $this->rentalcharges;
}
/**
* Sets the rentalcharges
*
* #param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Rentalcharge> $rentalcharges
* #return void
*/
public function setRentalcharges(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $rentalcharges) {
$this->rentalcharges = $rentalcharges;
}
}
UPDATE2:
Tried:
class Period extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {...}
and:
$period = $this->objectManager->get('TYPO3\Fewo\Domain\Model\Period');
with no effect :(
Is Period in FeWo of the right type?
It have to be something like this:
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Fewo\Domain\Model\Fewo>

Symfony2 - Able to add data of multiple entities to database but no foreign keys are added

I got 2 tables connected with a foreign key (user_id is in the table persons). I'm able to add data to both of the tables by executing 1 form.
but on the execute i expected that the foreign key would automatically be generated in the table persons.
Here are my enities
USERS
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Users
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users
{
//Vanaf hier bijgevoegd
protected $adress;
protected $person;
//Einde zelf toegevoegde properties
/**
* #var integer $userId
*
* #ORM\Column(name="user_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* #var string $user
*
* #ORM\Column(name="user_password", type="string", length=128, nullable=true)
*/
private $userPassword;
/**
* #var string $userEmail
*
* #ORM\Column(name="user_email", type="string", length=128, nullable=true)
*/
private $userEmail;
/**
* #var string $userImage
*
* #ORM\Column(name="user_image", type="string", length=128, nullable=true)
*/
private $userImage;
/**
* #var string $userQuestion
*
* #ORM\Column(name="user_question", type="string", length=128, nullable=true)
*/
private $userQuestion;
/**
* #var string $userAnswer
*
* #ORM\Column(name="user_answer", type="string", length=128, nullable=true)
*/
private $userAnswer;
/**
* #var \DateTime $userCreateddate
*
* #ORM\Column(name="user_createddate", type="datetime", nullable=true)
*/
/**
* Get userId
*
* #return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set userPassword
*
* #param string $userPassword
* #return Users
*/
public function setUserPassword($userPassword)
{
$this->userPassword = $userPassword;
return $this;
}
/**
* Get userPassword
*
* #return string
*/
public function getUserPassword()
{
return $this->userPassword;
}
/**
* Set userEmail
*
* #param string $userEmail
* #return Users
*/
public function setUserEmail($userEmail)
{
$this->userEmail = $userEmail;
return $this;
}
/**
* Get userEmail
*
* #return string
*/
public function getUserEmail()
{
return $this->userEmail;
}
/**
* Set userImage
*
* #param string $userImage
* #return Users
*/
public function setUserImage($userImage)
{
$this->userImage = $userImage;
return $this;
}
/**
* Get userImage
*
* #return string
*/
public function getUserImage()
{
return $this->userImage;
}
/**
* Set userQuestion
*
* #param string $userQuestion
* #return Users
*/
public function setUserQuestion($userQuestion)
{
$this->userQuestion = $userQuestion;
return $this;
}
/**
* Get userQuestion
*
* #return string
*/
public function getUserQuestion()
{
return $this->userQuestion;
}
/**
* Set userAnswer
*
* #param string $userAnswer
* #return Users
*/
public function setUserAnswer($userAnswer)
{
$this->userAnswer = $userAnswer;
return $this;
}
/**
* Get userAnswer
*
* #return string
*/
public function getUserAnswer()
{
return $this->userAnswer;
}
//Zelf toegevoegde getters & setters
public function getPerson()
{
return $this->person;
}
public function setPerson(Persons $person = null)
{
$this->person = $person;
}
public function getAdress()
{
return $this->adress;
}
public function setAdress(Adresses $adress = null)
{
$this->adress = $adress;
}
}
PERSONS
namespace Geo\CityTroopersBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Geo\CityTroopersBundle\Entity\Persons
*
* #ORM\Table(name="persons")
* #ORM\Entity
*/
class Persons
{
/**
* #var integer $personId
*
* #ORM\Column(name="person_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $personId;
/**
* #var integer $adressId
*
* #ORM\Column(name="adress_id", type="integer", nullable=false)
*/
private $adressId;
/**
* #var string $personFamilyname
*
* #ORM\Column(name="person_familyname", type="string", length=64, nullable=false)
*/
private $personFamilyname;
/**
* #var string $personFirstname
*
* #ORM\Column(name="person_firstname", type="string", length=64, nullable=false)
*/
private $personFirstname;
/**
* #var boolean $personGender
*
* #ORM\Column(name="person_gender", type="boolean", nullable=false)
*/
private $personGender;
/**
* #var string $personNationality
*
* #ORM\Column(name="person_nationality", type="string", length=45, nullable=false)
*/
private $personNationality;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Get personId
*
* #return integer
*/
public function getPersonId()
{
return $this->personId;
}
/**
* Set adressId
*
* #param integer $adressId
* #return Persons
*/
public function setAdressId($adressId)
{
$this->adressId = $adressId;
return $this;
}
/**
* Get adressId
*
* #return integer
*/
public function getAdressId()
{
return $this->adressId;
}
/**
* Set personFamilyname
*
* #param string $personFamilyname
* #return Persons
*/
public function setPersonFamilyname($personFamilyname)
{
$this->personFamilyname = $personFamilyname;
return $this;
}
/**
* Get personFamilyname
*
* #return string
*/
public function getPersonFamilyname()
{
return $this->personFamilyname;
}
/**
* Set personFirstname
*
* #param string $personFirstname
* #return Persons
*/
public function setPersonFirstname($personFirstname)
{
$this->personFirstname = $personFirstname;
return $this;
}
/**
* Get personFirstname
*
* #return string
*/
public function getPersonFirstname()
{
return $this->personFirstname;
}
/**
* Set personGender
*
* #param boolean $personGender
* #return Persons
*/
public function setPersonGender($personGender)
{
$this->personGender = $personGender;
return $this;
}
/**
* Get personGender
*
* #return boolean
*/
public function getPersonGender()
{
return $this->personGender;
}
/**
* Set personNationality
*
* #param string $personNationality
* #return Persons
*/
public function setPersonNationality($personNationality)
{
$this->personNationality = $personNationality;
return $this;
}
/**
* Get personNationality
*
* #return string
*/
public function getPersonNationality()
{
return $this->personNationality;
}
/**
* Set user
*
* #param Geo\CityTroopersBundle\Entity\Users $user
* #return Persons
*/
public function setUser(\Geo\CityTroopersBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return Geo\CityTroopersBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
CONTROLLER
class UserController extends Controller
{
public function registerAction(Request $request)
{
$user = new Users();
$person = new Persons();
//$adress = new Adresses();
$form = $this->createForm(new UserType(), $user);
if ($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()){
$person = $form->getData();
//$adress = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->persist($user->getPerson());
//$em->persist($user->getAdress());
$em->flush();
return $this->redirect($this->generateUrl('indexpage'));
}
}
return $this->render('GeoCityTroopersBundle:User:register.html.twig', array(
'form' => $form->createView(),
));
}
}
USERTYPE
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('person', new PersonType());
$builder->add('userEmail');
$builder->add('userPassword', 'password');
$builder->add('userImage');
$builder->add('userQuestion');
$builder->add('userAnswer');
//$builder->add('adress', new AdressType());
}
public function getName()
{
return 'user';
}
}
PERSONTYPE
class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('personFamilyname');
$builder->add('personFirstname');
$builder->add('personNationality');
$builder->add('personGender');
//$builder->add('adressId');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Geo\CityTroopersBundle\Entity\Persons',
);
}
public function getName()
{
return 'person';
}
}
I have tried to add in the annotations of Users the following:
/**
* Bidirectional (INVERSE SIDE)
* #ORM\OneToOne(targetEntity="Persons",cascade={"persist"})
*
*/
But this didn't worked either.
I have a feeling there is something wrong with the setup of my database, the relation between persons and users is one to one.
update: Deleted unnecessary
Thanks in advance