Add data from other table to form + Symfony2 - php

I have a table users with different fields in it. And I also have a table players with a FK user_id in it (along other fields).
Now I have a registerForm to create a user, but I would also like to add a field for a player.
This is my form now:
$builder
->add('userFirstname', 'text', [
'label' => 'Given name',
'attr' => [
'placeholder' => 'Enter your given name.',
],
])
->add('userSurname', 'text', [
'label' => 'Family name',
'attr' => ['placeholder' => 'Enter your family name.'],
])
->add('userType', 'choice', array(
'choices' => array('E' => 'ENTHOUSIAST', 'P' => 'PLAYER', 'T' => 'TRAINER'),
'label' => 'User Type',
'attr' => array('placeholder' => 'User Type')
))
->add('player')
->add('userUsername', 'text', [
'label' => 'Username',
'attr' => ['placeholder' => 'Enter a username.'],
])
->add('userEmail', 'email', [
'label' => 'Email address',
'attr' => ['placeholder' => 'Enter your email address.'],
])
->add('userPassword', 'repeated', [
'type' => 'password',
'first_name' => 'password',
'first_options' => [
'label' => 'Password',
'attr' => ['placeholder' => 'Enter a password.'],
],
'second_name' => 'confirm',
'second_options' => [
'label' => 'Password (repeat)',
'attr' => ['placeholder' => 'Repeat the password.'],
],
'invalid_message' => 'The passwords are not identical.',
])
->add('btn_register', 'submit', [
'label' => 'Register',
])
;
In my players table I have 'player_birthyear'. How can I add this to my form? I already tried to add player.birthyear to my Form but didn't work.
UPDATE:
My Users entities class:
/**
* Users
*
* #ORM\Table(name="users", indexes={#ORM\Index(name="fk_users_roles1_idx", columns={"role_id"})})
* #ORM\Entity
*/
class Users
{
/**
* #var string
*
* #ORM\Column(name="user_username", type="string", length=45, nullable=false)
*/
private $userUsername;
/**
* #var string
*
* #ORM\Column(name="user_firstname", type="string", length=45, nullable=false)
*/
private $userFirstname;
/**
* #var string
*
* #ORM\Column(name="user_surname", type="string", length=255, nullable=false)
*/
private $userSurname;
/**
* #var string
*
* #ORM\Column(name="user_email", type="string", length=255, nullable=false)
*/
private $userEmail;
/**
* #var string
*
* #ORM\Column(name="user_type", type="string", nullable=false)
*/
private $userType;
/**
* #var string
*
* #ORM\Column(name="user_password", type="string", length=60, nullable=false)
*/
private $userPassword;
/**
* #var string
*
* #ORM\Column(name="user_salt", type="string", length=30, nullable=false)
*/
private $userSalt;
/**
* #var string
*
* #ORM\Column(name="user_token", type="string", length=45, nullable=true)
*/
private $userToken;
/**
* #var \DateTime
*
* #ORM\Column(name="user_created", type="datetime", nullable=false)
*/
private $userCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="user_modified", type="datetime", nullable=true)
*/
private $userModified;
/**
* #var \DateTime
*
* #ORM\Column(name="user_deleted", type="datetime", nullable=true)
*/
private $userDeleted;
/**
* #var \DateTime
*
* #ORM\Column(name="user_lastlogin", type="datetime", nullable=true)
*/
private $userLastlogin;
/**
* #var \DateTime
*
* #ORM\Column(name="user_confirmed", type="datetime", nullable=true)
*/
private $userConfirmed;
/**
* #var \DateTime
*
* #ORM\Column(name="user_locked", type="datetime", nullable=true)
*/
private $userLocked;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Roles
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Roles")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
* })
*/
private $role;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams", inversedBy="user")
* #ORM\JoinTable(name="user_follows_teams",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* }
* )
*/
private $team;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Competitions", inversedBy="user")
* #ORM\JoinTable(name="user_follows_competitions",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="competition_id", referencedColumnName="competition_id")
* }
* )
*/
private $competition;
/**
* Constructor
*/
public function __construct()
{
$this->team = new \Doctrine\Common\Collections\ArrayCollection();
$this->competition = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set userUsername
*
* #param string $userUsername
* #return Users
*/
public function setUserUsername($userUsername)
{
$this->userUsername = $userUsername;
return $this;
}
/**
* Get userUsername
*
* #return string
*/
public function getUserUsername()
{
return $this->userUsername;
}
/**
* Set userFirstname
*
* #param string $userFirstname
* #return Users
*/
public function setUserFirstname($userFirstname)
{
$this->userFirstname = $userFirstname;
return $this;
}
/**
* Get userFirstname
*
* #return string
*/
public function getUserFirstname()
{
return $this->userFirstname;
}
/**
* Set userSurname
*
* #param string $userSurname
* #return Users
*/
public function setUserSurname($userSurname)
{
$this->userSurname = $userSurname;
return $this;
}
/**
* Get userSurname
*
* #return string
*/
public function getUserSurname()
{
return $this->userSurname;
}
/**
* 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 userType
*
* #param string $userType
* #return Users
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return string
*/
public function getUserType()
{
return $this->userType;
}
/**
* 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 userSalt
*
* #param string $userSalt
* #return Users
*/
public function setUserSalt($userSalt)
{
$this->userSalt = $userSalt;
return $this;
}
/**
* Get userSalt
*
* #return string
*/
public function getUserSalt()
{
return $this->userSalt;
}
/**
* Set userToken
*
* #param string $userToken
* #return Users
*/
public function setUserToken($userToken)
{
$this->userToken = $userToken;
return $this;
}
/**
* Get userToken
*
* #return string
*/
public function getUserToken()
{
return $this->userToken;
}
/**
* Set userCreated
*
* #param \DateTime $userCreated
* #return Users
*/
public function setUserCreated($userCreated)
{
$this->userCreated = $userCreated;
return $this;
}
/**
* Get userCreated
*
* #return \DateTime
*/
public function getUserCreated()
{
return $this->userCreated;
}
/**
* Set userModified
*
* #param \DateTime $userModified
* #return Users
*/
public function setUserModified($userModified)
{
$this->userModified = $userModified;
return $this;
}
/**
* Get userModified
*
* #return \DateTime
*/
public function getUserModified()
{
return $this->userModified;
}
/**
* Set userDeleted
*
* #param \DateTime $userDeleted
* #return Users
*/
public function setUserDeleted($userDeleted)
{
$this->userDeleted = $userDeleted;
return $this;
}
/**
* Get userDeleted
*
* #return \DateTime
*/
public function getUserDeleted()
{
return $this->userDeleted;
}
/**
* Set userLastlogin
*
* #param \DateTime $userLastlogin
* #return Users
*/
public function setUserLastlogin($userLastlogin)
{
$this->userLastlogin = $userLastlogin;
return $this;
}
/**
* Get userLastlogin
*
* #return \DateTime
*/
public function getUserLastlogin()
{
return $this->userLastlogin;
}
/**
* Set userConfirmed
*
* #param \DateTime $userConfirmed
* #return Users
*/
public function setUserConfirmed($userConfirmed)
{
$this->userConfirmed = $userConfirmed;
return $this;
}
/**
* Get userConfirmed
*
* #return \DateTime
*/
public function getUserConfirmed()
{
return $this->userConfirmed;
}
/**
* Set userLocked
*
* #param \DateTime $userLocked
* #return Users
*/
public function setUserLocked($userLocked)
{
$this->userLocked = $userLocked;
return $this;
}
/**
* Get userLocked
*
* #return \DateTime
*/
public function getUserLocked()
{
return $this->userLocked;
}
/**
* Get userId
*
* #return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set role
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Roles $role
* #return Users
*/
public function setRole(\VolleyScout\VolleyScoutBundle\Entity\Roles $role = null)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Roles
*/
public function getRole()
{
return $this->role;
}
/**
* Add team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Users
*/
public function addTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
{
$this->team[] = $team;
return $this;
}
/**
* Remove team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
*/
public function removeTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
{
$this->team->removeElement($team);
}
/**
* Get team
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTeam()
{
return $this->team;
}
/**
* Add competition
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
* #return Users
*/
public function addCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
{
$this->competition[] = $competition;
return $this;
}
/**
* Remove competition
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
*/
public function removeCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
{
$this->competition->removeElement($competition);
}
/**
* Get competition
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCompetition()
{
return $this->competition;
}
}
My Players entities class:
/**
* Players
*
* #ORM\Table(name="players", indexes={#ORM\Index(name="fk_players_users1_idx", columns={"user_id"}), #ORM\Index(name="fk_players_teams1_idx", columns={"team_id"}), #ORM\Index(name="fk_players_myteam1_idx", columns={"myteam_id"})})
* #ORM\Entity
*/
class Players
{
/**
* #var string
*
* #ORM\Column(name="player_name", type="string", length=255, nullable=false)
*/
private $playerName;
/**
* #var string
*
* #ORM\Column(name="player_licensenumber", type="string", length=45, nullable=false)
*/
private $playerLicensenumber;
/**
* #var string
*
* #ORM\Column(name="player_position", type="string", nullable=false)
*/
private $playerPosition;
/**
* #var \DateTime
*
* #ORM\Column(name="player_birthyear", type="datetime", nullable=true)
*/
private $playerBirthyear;
/**
* #var integer
*
* #ORM\Column(name="player_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $playerId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Myteam
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Myteam")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="myteam_id", referencedColumnName="myteam_id")
* })
*/
private $myteam;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Teams
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* })
*/
private $team;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Users
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Users")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* })
*/
private $user;
/**
* Set playerName
*
* #param string $playerName
* #return Players
*/
public function setPlayerName($playerName)
{
$this->playerName = $playerName;
return $this;
}
/**
* Get playerName
*
* #return string
*/
public function getPlayerName()
{
return $this->playerName;
}
/**
* Set playerLicensenumber
*
* #param string $playerLicensenumber
* #return Players
*/
public function setPlayerLicensenumber($playerLicensenumber)
{
$this->playerLicensenumber = $playerLicensenumber;
return $this;
}
/**
* Get playerLicensenumber
*
* #return string
*/
public function getPlayerLicensenumber()
{
return $this->playerLicensenumber;
}
/**
* Set playerPosition
*
* #param string $playerPosition
* #return Players
*/
public function setPlayerPosition($playerPosition)
{
$this->playerPosition = $playerPosition;
return $this;
}
/**
* Get playerPosition
*
* #return string
*/
public function getPlayerPosition()
{
return $this->playerPosition;
}
/**
* Set playerBirthyear
*
* #param \DateTime $playerBirthyear
* #return Players
*/
public function setPlayerBirthyear($playerBirthyear)
{
$this->playerBirthyear = $playerBirthyear;
return $this;
}
/**
* Get playerBirthyear
*
* #return \DateTime
*/
public function getPlayerBirthyear()
{
return $this->playerBirthyear;
}
/**
* Get playerId
*
* #return integer
*/
public function getPlayerId()
{
return $this->playerId;
}
/**
* Set myteam
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam
* #return Players
*/
public function setMyteam(\VolleyScout\VolleyScoutBundle\Entity\Myteam $myteam = null)
{
$this->myteam = $myteam;
return $this;
}
/**
* Get myteam
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Myteam
*/
public function getMyteam()
{
return $this->myteam;
}
/**
* Set team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Players
*/
public function setTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team = null)
{
$this->team = $team;
return $this;
}
/**
* Get team
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Teams
*/
public function getTeam()
{
return $this->team;
}
/**
* Set user
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Users $user
* #return Players
*/
public function setUser(\VolleyScout\VolleyScoutBundle\Entity\Users $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Users
*/
public function getUser()
{
return $this->user;
}
}
When I try add a player field I get this error:
Neither the property "player" nor one of the methods "getPlayer()", "isPlayer()", "hasPlayer()", "__get()" exist and have public access in class "VolleyScout\VolleyScoutBundle\Entity\Users".
UPDATE 2:
And I also have a FK in my players table 'team_id'. So when I create a user I would like to also select a team. So what I want is a list from all the teams in the database and set is as a choice field type in my Form. Do you know how I can place that in my formtype?
UPDATE 3:
I've added the following to my Users entity class:
private $player;
/**
* Get player
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Players
*/
public function getPlayer() {
return $this->player;
}
/**
* Set player
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Players $player
* #return Users
*/
public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player){
$this->player = $player;
}
In my formtype I've added:
->add('player')
Now in my form I want to do the following:
{{ form_label(registerForm.player.playerLicensenumber, label|default(),
Because in my Players entity class I have playerLicensenumber but I always get this error:
Method "playerLicensenumber" for object "Symfony\Component\Form\FormView" does not exist in VolleyScoutBundle:User:register.html.twig at line 57
UPDATE 4:
Is this a good solution:
Added this to my register form:
->add('player', new PlayerType())
And in my playertype I have the fields from player table.

You have to add a setter and a getter for your "player" field in your User entity.
public function getPlayer() {
return $this->player;
}
public function setPlayer(\Path\to\player\entity $player){
$this->player = $player;
}
For the team list in your form, you have to add an "entity" field type. Here are the docs for more info : http://symfony.com/doc/current/reference/forms/types/entity.html

Related

The option "filters" does not have a callable "setFilters" ("setfilters") setter method

Environment:
"require": {
"php": "5.6",
"zendframework/zendframework": "2.5.0",
"zendframework/zend-servicemanager": "2.5.1",
"zendframework/zend-developer-tools": "1.0.0",
"doctrine/doctrine-orm-module": "0.9.2",
"acelaya/zf2-acmailer": "4.*",
"tasmaniski/zend-params-helper": "^1.0"
}
Fatal error: Uncaught exception 'Zend\Stdlib\Exception\BadMethodCallException' with message 'The option "filters" does not have a callable "setFilters" ("setfilters") setter method which must be defined' in C:\OSPanel\domains\csmCrew_github\vendor\zendframework\zend-servicemanager\src\ServiceManager.php on line 1135
I am trying to add SQL filter using this DareDevels tutorial
my interface: VacanciesFilterInterface.php
namespace Common\DoctrineFilter;
Interface VacanciesFilterInterface
{
public function setFilters($filters);
}
my filter class: VacanciesFilter.php
namespace Common\DoctrineFilter;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
class VacanciesFilter extends SQLFilter
{
/**
* Gets the SQL query part to add to a query.
*
* #param ClassMetaData $targetEntity
* #param string $targetTableAlias
*
* #return string The constraint SQL if there is available, empty string otherwise.
*/
public function addFilterConstraint (ClassMetadata $targetEntity, $targetTableAlias)
{
// Check if the entity implements the LocalAware interface
if (!$targetEntity->reflClass->implementsInterface('\Common\DoctrineFilter\VacanciesFilterInterface')) {
return '';
}
return $targetTableAlias.'.Vacancies = ' . $this->getParameter('Vacancies'); // getParameter applies quoting automatically
}
my module.config.php:
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'common_entity' => array(
'class' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class,
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Common/Entity',
__DIR__ . '/../src/Common/Entity/Repository',
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `My\Namespace`
'Common\Entity' => 'common_entity',
),
'filters' => array(
'vacancies' => 'Common\DoctrineFilter\VacanciesFilter',
),
),
),
),
I add filter in my controller using these methods:
public function enableVacanciesFilter()
{
$em = $this->getEntityManager();
$filter = $em->getFilters()->enable('vacancies');
$filter->setParameter('vacancies', '41');
}
/**
* Disable locale filter
*/
public function disableVacanciesFilter()
{
$em = $this->getEntityManager();
$filter = $em->getFilters()->disable('vacancies');
}
So the question is: how can I implement that callable setFilters setter method ?
I have tried to implement Interface in my Entity:
<?php
namespace Common\Entity;
use Common\DoctrineFilter\VacanciesFilterInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* Vacancies
*
* #ORM\Table(name="vacancies", indexes={#ORM\Index(name="RankId_fk", columns={"rank_id"}), #ORM\Index(name="id", columns={"id"}), #ORM\Index(name="FK_vacancies_crewing_companies", columns={"company_id"}), #ORM\Index(name="FK_vacancies_vessels", columns={"vessel_id"}), #ORM\Index(name="FK_vacancies_vacancy_positions", columns={"position"}), #ORM\Index(name="status_id", columns={"status_id"})})
* #ORM\Entity
*/
class Vacancies implements VacanciesFilterInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=128, nullable=false)
*/
private $title = '';
/**
* #var string
*
* #ORM\Column(name="additional_info", type="string", length=255, nullable=false)
*/
private $additionalInfo = '';
/**
* #var integer
*
* #ORM\Column(name="duration_of_contract_in_months", type="smallint", nullable=false)
*/
private $durationOfContractInMonths;
/**
* #var \DateTime
*
* #ORM\Column(name="joining_date", type="date", nullable=false)
*/
private $joiningDate;
/**
* #var integer
*
* #ORM\Column(name="salary_per_month", type="integer", nullable=false)
*/
private $salaryPerMonth;
/**
* #var boolean
*
* #ORM\Column(name="required_age_from", type="boolean", nullable=false)
*/
private $requiredAgeFrom;
/**
* #var boolean
*
* #ORM\Column(name="required_age_to", type="boolean", nullable=false)
*/
private $requiredAgeTo;
/**
* #var \DateTime
*
* #ORM\Column(name="creation_date", type="datetime", nullable=false)
*/
private $creationDate;
/**
* #var integer
*
* #ORM\Column(name="views_count", type="integer", nullable=true)
*/
private $viewsCount = '0';
/**
* #var integer
*
* #ORM\Column(name="job_seekers_applied", type="integer", nullable=true)
*/
private $jobSeekersApplied = '0';
/**
* #var boolean
*
* #ORM\Column(name="is_published", type="boolean", nullable=false)
*/
private $isPublished = '0';
/**
* #var \Common\Entity\CrewingCompanies
*
* #ORM\ManyToOne(targetEntity="Common\Entity\CrewingCompanies")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
* })
*/
private $company;
/**
* #var \Common\Entity\VacancyPositions
*
* #ORM\ManyToOne(targetEntity="Common\Entity\VacancyPositions")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="position", referencedColumnName="id")
* })
*/
private $position;
/**
* #var \Common\Entity\Vessels
*
* #ORM\ManyToOne(targetEntity="Common\Entity\Vessels")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vessel_id", referencedColumnName="id")
* })
*/
private $vessel;
/**
* #var \Common\Entity\Ranks
*
* #ORM\ManyToOne(targetEntity="Common\Entity\Ranks")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="rank_id", referencedColumnName="id")
* })
*/
private $rank;
/**
* #var \Common\Entity\VacancyStatuses
*
* #ORM\ManyToOne(targetEntity="Common\Entity\VacancyStatuses")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="status_id", referencedColumnName="id")
* })
*/
private $status;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Vacancies
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set additionalInfo
*
* #param string $additionalInfo
*
* #return Vacancies
*/
public function setAdditionalInfo($additionalInfo)
{
$this->additionalInfo = $additionalInfo;
return $this;
}
/**
* Get additionalInfo
*
* #return string
*/
public function getAdditionalInfo()
{
return $this->additionalInfo;
}
/**
* Set durationOfContractInMonths
*
* #param integer $durationOfContractInMonths
*
* #return Vacancies
*/
public function setDurationOfContractInMonths($durationOfContractInMonths)
{
$this->durationOfContractInMonths = $durationOfContractInMonths;
return $this;
}
/**
* Get durationOfContractInMonths
*
* #return integer
*/
public function getDurationOfContractInMonths()
{
return $this->durationOfContractInMonths;
}
/**
* Set joiningDate
*
* #param \DateTime $joiningDate
*
* #return Vacancies
*/
public function setJoiningDate($joiningDate)
{
$this->joiningDate = $joiningDate;
return $this;
}
/**
* Get joiningDate
*
* #return \DateTime
*/
public function getJoiningDate()
{
return $this->joiningDate;
}
/**
* Set salaryPerMonth
*
* #param integer $salaryPerMonth
*
* #return Vacancies
*/
public function setSalaryPerMonth($salaryPerMonth)
{
$this->salaryPerMonth = $salaryPerMonth;
return $this;
}
/**
* Get salaryPerMonth
*
* #return integer
*/
public function getSalaryPerMonth()
{
return $this->salaryPerMonth;
}
/**
* Set requiredAgeFrom
*
* #param boolean $requiredAgeFrom
*
* #return Vacancies
*/
public function setRequiredAgeFrom($requiredAgeFrom)
{
$this->requiredAgeFrom = $requiredAgeFrom;
return $this;
}
/**
* Get requiredAgeFrom
*
* #return boolean
*/
public function getRequiredAgeFrom()
{
return $this->requiredAgeFrom;
}
/**
* Set requiredAgeTo
*
* #param boolean $requiredAgeTo
*
* #return Vacancies
*/
public function setRequiredAgeTo($requiredAgeTo)
{
$this->requiredAgeTo = $requiredAgeTo;
return $this;
}
/**
* Get requiredAgeTo
*
* #return boolean
*/
public function getRequiredAgeTo()
{
return $this->requiredAgeTo;
}
/**
* Set creationDate
*
* #param \DateTime $creationDate
*
* #return Vacancies
*/
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
return $this;
}
/**
* Get creationDate
*
* #return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set viewsCount
*
* #param integer $viewsCount
*
* #return Vacancies
*/
public function setViewsCount($viewsCount)
{
$this->viewsCount = $viewsCount;
return $this;
}
/**
* Get viewsCount
*
* #return integer
*/
public function getViewsCount()
{
return $this->viewsCount;
}
/**
* Set jobSeekersApplied
*
* #param integer $jobSeekersApplied
*
* #return Vacancies
*/
public function setJobSeekersApplied($jobSeekersApplied)
{
$this->jobSeekersApplied = $jobSeekersApplied;
return $this;
}
/**
* Get jobSeekersApplied
*
* #return integer
*/
public function getJobSeekersApplied()
{
return $this->jobSeekersApplied;
}
/**
* Set isPublished
*
* #param boolean $isPublished
*
* #return Vacancies
*/
public function setIsPublished($isPublished)
{
$this->isPublished = $isPublished;
return $this;
}
/**
* Get isPublished
*
* #return boolean
*/
public function getIsPublished()
{
return $this->isPublished;
}
/**
* Set company
*
* #param \Common\Entity\CrewingCompanies $company
*
* #return Vacancies
*/
public function setCompany(\Common\Entity\CrewingCompanies $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* #return \Common\Entity\CrewingCompanies
*/
public function getCompany()
{
return $this->company;
}
/**
* Set position
*
* #param \Common\Entity\VacancyPositions $position
*
* #return Vacancies
*/
public function setPosition(\Common\Entity\VacancyPositions $position = null)
{
$this->position = $position;
return $this;
}
/**
* Get position
*
* #return \Common\Entity\VacancyPositions
*/
public function getPosition()
{
return $this->position;
}
/**
* Set vessel
*
* #param \Common\Entity\Vessels $vessel
*
* #return Vacancies
*/
public function setVessel(\Common\Entity\Vessels $vessel = null)
{
$this->vessel = $vessel;
return $this;
}
/**
* Get vessel
*
* #return \Common\Entity\Vessels
*/
public function getVessel()
{
return $this->vessel;
}
/**
* Set rank
*
* #param \Common\Entity\Ranks $rank
*
* #return Vacancies
*/
public function setRank(\Common\Entity\Ranks $rank = null)
{
$this->rank = $rank;
return $this;
}
/**
* Get rank
*
* #return \Common\Entity\Ranks
*/
public function getRank()
{
return $this->rank;
}
/**
* Set status
*
* #param \Common\Entity\VacancyStatuses $status
*
* #return Vacancies
*/
public function setStatus(\Common\Entity\VacancyStatuses $status = null)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return \Common\Entity\VacancyStatuses
*/
public function getStatus()
{
return $this->status;
}
public function setFilters ($filters)
{
$this->filters = $filters;
// TODO: Implement setFilters() method.
}
}
But nothing helps to deal with that error.

How to handle multiple fields with the same name in symfony2

I have to submit a form (sell) with multiple fields, some of them are from an entity and the others are customed fields that i can duplicate (products) with JQuery.
the problem is that in the createAction i get just one of each duplicated field (the last one).
This is my form type :
$builder
->add('montant')
->add('dateDebut', 'date', array( 'input' => 'datetime','widget' => 'single_text'))
->add('dateFin', 'date', array('input' => 'datetime', 'widget' => 'single_text'))
->add('statutVente', 'choice', array('choices' => array('Oui' => 'Oui','A finaliser' => 'A finaliser', 'A encaisser' => 'A encaisser', 'Non' => 'Non' ),'choices_as_values' => true))
->add('objet', 'choice', array('choices' => array('Entrant' => 'Entrant','Prospection' => 'Prospection', 'Renouvellement ' => 'Renouvellement' ),'choices_as_values' => true))
->add('moyenPaiement', 'choice', array('choices' => array('CB' => 'CB','Chèque' => 'Chèque', 'Virement ' => 'Virement', 'Espèces' => 'Espèces', 'Paypal' => 'Paypal', 'Autre' => 'Autre' ),'choices_as_values' => true))
->add('numeroCommande')
->add('devise', 'choice', array('choices' => array('MAD' => 'MAD','EURO' => 'EURO', 'DOLLAR ' => 'DOLLAR' ),'choices_as_values' => true))
->add('nomFacturation')
->add('tva', 'choice', array('choices' => array('OUI' => 'OUI','NON' => 'NON'),'choices_as_values' => true))
->add('reduction')
->add('adresseFacturation')
->add('cpFacturation')
->add('villeFacturation')
->add('raison')
->add('produit', 'entity', array('class' => 'VENTEBundle\\Entity\\Produit', 'mapped' => false, 'attr' => array('class'=>'add_form') ))
->add('compte', 'text', array('mapped'=> false))
->add('Sujet', null, array('mapped' => false))
->add('sujetappel', null, array('mapped' => false))
->add('Note', 'textarea', array( 'attr' => array( 'placeholder'=>""),'mapped' => false))
->add('remindertime', 'datetime', array('date_widget' => 'single_text','time_widget' => 'single_text', 'format' => 'dd-MM-yyyy HH:mm','mapped' => false,
'attr' => array( 'data-provide' => 'datepicker',
'data-date-format' => 'dd-mm-yyyy' )))
->add('dateappel', 'datetime', array('date_widget' => 'single_text','time_widget' => 'single_text', 'format' => 'dd-MM-yyyy','mapped' => false,
'attr' => array( 'data-provide' => 'datepicker',
'data-date-format' => 'dd-mm-yyyy' )))
->add('quantite', 'text', array('mapped' => false))
->add('montant_total', 'text', array('mapped' => false))
->add('nbr_personne', 'text', array('mapped' => false))
->add('duree', 'text', array('mapped' => false))
->add('description', 'textarea', array('mapped' => false));
}
And this is the jquery function that duplicate the fields :
$("#add_product").on('click', function(){
var i=1;
var original = document.getElementById('form_container_child');
var clone = original.cloneNode(true); // "deep" clone
var elements = document.getElementsByTagName('div');
clone.id = "form_container_child" + ++i;
original.parentNode.append(clone);
})
This is my sell entity (vente)
<?php
namespace VENTEBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Vente
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="VENTEBundle\Entity\VenteRepository")
*/
class Vente
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var float
*
* #ORM\Column(name="montant", type="float")
*/
private $montant;
/**
* #var \DateTime
*
* #ORM\Column(name="date_debut", type="date")
*/
private $dateDebut;
/**
* #var \DateTime
*
* #ORM\Column(name="date_fin", type="date")
*/
private $dateFin;
/**
* #var \DateTime
*
* #ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* #var string
*
* #ORM\Column(name="statut_vente", type="string", length=100)
*/
private $statutVente;
/**
* #var string
*
* #ORM\Column(name="objet", type="string", length=100)
*/
private $objet;
/**
* #var string
*
* #ORM\Column(name="moyen_paiement", type="string", length=100)
*/
private $moyenPaiement;
/**
* #var string
*
* #ORM\Column(name="numero_commande", type="string", length=120)
*/
private $numeroCommande;
/**
* #var string
*
* #ORM\Column(name="devise", type="string", length=20)
*/
private $devise;
/**
* #var string
*
* #ORM\Column(name="nom_facturation", type="string", length=255)
*/
private $nomFacturation;
/**
* #var string
*
* #ORM\Column(name="tva", type="string", length=255)
*/
private $tva;
/**
* #var float
*
* #ORM\Column(name="reduction", type="float")
*/
private $reduction;
/**
* #var string
*
* #ORM\Column(name="adresse_facturation", type="string", length=500)
*/
private $adresseFacturation;
/**
* #var string
*
* #ORM\Column(name="cpfacturation", type="string", length=20)
*/
private $cpFacturation;
/**
* #var string
*
* #ORM\Column(name="ville_facturation", type="string", length=40)
*/
private $villeFacturation;
/**
* #ORM\ManyToOne(targetEntity="Raison", inversedBy="ventes")
* #ORM\JoinColumn(name="raison_id", referencedColumnName="id")
*/
private $raison;
/**
* #ORM\ManyToOne(targetEntity="COMPTEBundle\Entity\Compte", inversedBy="ventes")
* #ORM\JoinColumn(name="compte_id", referencedColumnName="id")
*/
private $compte;
/**
* #ORM\OneToMany(targetEntity="Venteproduit", mappedBy="vente")
*/
private $venteproduits;
/**
* #ORM\OneToMany(targetEntity="Ventedocument", mappedBy="vente")
*/
private $ventedocuments;
/**
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="ventes")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* Constructor
*/
public function __construct()
{
$this->venteproduits = new \Doctrine\Common\Collections\ArrayCollection();
$this->ventedocuments = new \Doctrine\Common\Collections\ArrayCollection();
$this->dateCreation = new \Datetime();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set montant
*
* #param float $montant
* #return Vente
*/
public function setMontant($montant)
{
$this->montant = $montant;
return $this;
}
/**
* Get montant
*
* #return float
*/
public function getMontant()
{
return $this->montant;
}
/**
* Set dateDebut
*
* #param \DateTime $dateDebut
* #return Vente
*/
public function setDateDebut($dateDebut)
{
$this->dateDebut = $dateDebut;
return $this;
}
/**
* Get dateDebut
*
* #return \DateTime
*/
public function getDateDebut()
{
return $this->dateDebut;
}
/**
* Set dateFin
*
* #param \DateTime $dateFin
* #return Vente
*/
public function setDateFin($dateFin)
{
$this->dateFin = $dateFin;
return $this;
}
/**
* Get dateFin
*
* #return \DateTime
*/
public function getDateFin()
{
return $this->dateFin;
}
/**
* Set dateCreation
*
* #param \DateTime $dateCreation
* #return Vente
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set statutVente
*
* #param string $statutVente
* #return Vente
*/
public function setStatutVente($statutVente)
{
$this->statutVente = $statutVente;
return $this;
}
/**
* Get statutVente
*
* #return string
*/
public function getStatutVente()
{
return $this->statutVente;
}
/**
* Set objet
*
* #param string $objet
* #return Vente
*/
public function setObjet($objet)
{
$this->objet = $objet;
return $this;
}
/**
* Get objet
*
* #return string
*/
public function getObjet()
{
return $this->objet;
}
/**
* Set moyenPaiement
*
* #param string $moyenPaiement
* #return Vente
*/
public function setMoyenPaiement($moyenPaiement)
{
$this->moyenPaiement = $moyenPaiement;
return $this;
}
/**
* Get moyenPaiement
*
* #return string
*/
public function getMoyenPaiement()
{
return $this->moyenPaiement;
}
/**
* Set numeroCommande
*
* #param string $numeroCommande
* #return Vente
*/
public function setNumeroCommande($numeroCommande)
{
$this->numeroCommande = $numeroCommande;
return $this;
}
/**
* Get numeroCommande
*
* #return string
*/
public function getNumeroCommande()
{
return $this->numeroCommande;
}
/**
* Set devise
*
* #param string $devise
* #return Vente
*/
public function setDevise($devise)
{
$this->devise = $devise;
return $this;
}
/**
* Get devise
*
* #return string
*/
public function getDevise()
{
return $this->devise;
}
/**
* Set nomFacturation
*
* #param string $nomFacturation
* #return Vente
*/
public function setNomFacturation($nomFacturation)
{
$this->nomFacturation = $nomFacturation;
return $this;
}
/**
* Get nomFacturation
*
* #return string
*/
public function getNomFacturation()
{
return $this->nomFacturation;
}
/**
* Set tva
*
* #param string $tva
* #return Vente
*/
public function setTva($tva)
{
$this->tva = $tva;
return $this;
}
/**
* Get tva
*
* #return string
*/
public function getTva()
{
return $this->tva;
}
/**
* Set reduction
*
* #param float $reduction
* #return Vente
*/
public function setReduction($reduction)
{
$this->reduction = $reduction;
return $this;
}
/**
* Get reduction
*
* #return float
*/
public function getReduction()
{
return $this->reduction;
}
/**
* Set adresseFacturation
*
* #param string $adresseFacturation
* #return Vente
*/
public function setAdresseFacturation($adresseFacturation)
{
$this->adresseFacturation = $adresseFacturation;
return $this;
}
/**
* Get adresseFacturation
*
* #return string
*/
public function getAdresseFacturation()
{
return $this->adresseFacturation;
}
/**
* Set cpFacturation
*
* #param string $cpFacturation
* #return Vente
*/
public function setCpFacturation($cpFacturation)
{
$this->cpFacturation = $cpFacturation;
return $this;
}
/**
* Get cpFacturation
*
* #return string
*/
public function getCpFacturation()
{
return $this->cpFacturation;
}
/**
* Set villeFacturation
*
* #param string $villeFacturation
* #return Vente
*/
public function setVilleFacturation($villeFacturation)
{
$this->villeFacturation = $villeFacturation;
return $this;
}
/**
* Get villeFacturation
*
* #return string
*/
public function getVilleFacturation()
{
return $this->villeFacturation;
}
/**
* Set raison
*
* #param \VENTEBundle\Entity\Raison $raison
* #return Vente
*/
public function setRaison(\VENTEBundle\Entity\Raison $raison = null)
{
$this->raison = $raison;
return $this;
}
/**
* Get raison
*
* #return \VENTEBundle\Entity\Raison
*/
public function getRaison()
{
return $this->raison;
}
/**
* Set compte
*
* #param \COMPTEBundle\Entity\Compte $compte
* #return Vente
*/
public function setCompte(\COMPTEBundle\Entity\Compte $compte = null)
{
$this->compte = $compte;
return $this;
}
/**
* Get compte
*
* #return \COMPTEBundle\Entity\Compte
*/
public function getCompte()
{
return $this->compte;
}
/**
* Add venteproduits
*
* #param \VENTEBundle\Entity\Venteproduit $venteproduits
* #return Vente
*/
public function addVenteproduit(\VENTEBundle\Entity\Venteproduit $venteproduits)
{
$this->venteproduits[] = $venteproduits;
return $this;
}
/**
* Remove venteproduits
*
* #param \VENTEBundle\Entity\Venteproduit $venteproduits
*/
public function removeVenteproduit(\VENTEBundle\Entity\Venteproduit $venteproduits)
{
$this->venteproduits->removeElement($venteproduits);
}
/**
* Get venteproduits
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getVenteproduits()
{
return $this->venteproduits;
}
/**
* Add ventedocuments
*
* #param \VENTEBundle\Entity\Ventedocument $ventedocuments
* #return Vente
*/
public function addVentedocument(\VENTEBundle\Entity\Ventedocument $ventedocuments)
{
$this->ventedocuments[] = $ventedocuments;
return $this;
}
/**
* Remove ventedocuments
*
* #param \VENTEBundle\Entity\Ventedocument $ventedocuments
*/
public function removeVentedocument(\VENTEBundle\Entity\Ventedocument $ventedocuments)
{
$this->ventedocuments->removeElement($ventedocuments);
}
/**
* Get ventedocuments
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getVentedocuments()
{
return $this->ventedocuments;
}
/**
* Set user
*
* #param \UserBundle\Entity\User $user
* #return Vente
*/
public function setUser(\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
And this is the products entity (produit)
<?php
namespace VENTEBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Venteproduit
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="VENTEBundle\Entity\VenteproduitRepository")
*/
class Venteproduit
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="quantite", type="integer")
*/
private $quantite;
/**
* #var float
*
* #ORM\Column(name="montant_total", type="float")
*/
private $montantTotal;
/**
* #var integer
*
* #ORM\Column(name="nbr_personne", type="integer")
*/
private $nbrPersonne;
/**
* #var string
*
* #ORM\Column(name="duree", type="string", length=50)
*/
private $duree;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=500)
*/
private $description;
/**
* #ORM\ManyToOne(targetEntity="Vente", inversedBy="venteproduits")
* #ORM\JoinColumn(name="vente_id", referencedColumnName="id")
*/
private $vente;
/**
* #ORM\ManyToOne(targetEntity="Produit", inversedBy="venteproduits")
* #ORM\JoinColumn(name="produit_id", referencedColumnName="id")
*/
private $produit;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set quantite
*
* #param integer $quantite
* #return Venteproduit
*/
public function setQuantite($quantite)
{
$this->quantite = $quantite;
return $this;
}
/**
* Get quantite
*
* #return integer
*/
public function getQuantite()
{
return $this->quantite;
}
/**
* Set montantTotal
*
* #param float $montantTotal
* #return Venteproduit
*/
public function setMontantTotal($montantTotal)
{
$this->montantTotal = $montantTotal;
return $this;
}
/**
* Get montantTotal
*
* #return float
*/
public function getMontantTotal()
{
return $this->montantTotal;
}
/**
* Set nbrPersonne
*
* #param integer $nbrPersonne
* #return Venteproduit
*/
public function setNbrPersonne($nbrPersonne)
{
$this->nbrPersonne = $nbrPersonne;
return $this;
}
/**
* Get nbrPersonne
*
* #return integer
*/
public function getNbrPersonne()
{
return $this->nbrPersonne;
}
/**
* Set duree
*
* #param string $duree
* #return Venteproduit
*/
public function setDuree($duree)
{
$this->duree = $duree;
return $this;
}
/**
* Get duree
*
* #return string
*/
public function getDuree()
{
return $this->duree;
}
/**
* Set description
*
* #param string $description
* #return Venteproduit
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set vente
*
* #param \VENTEBundle\Entity\Vente $vente
* #return Venteproduit
*/
public function setVente(\VENTEBundle\Entity\Vente $vente = null)
{
$this->vente = $vente;
return $this;
}
/**
* Get vente
*
* #return \VENTEBundle\Entity\Vente
*/
public function getVente()
{
return $this->vente;
}
/**
* Set produit
*
* #param \VENTEBundle\Entity\Produit $produit
* #return Venteproduit
*/
public function setProduit(\VENTEBundle\Entity\Produit $produit = null)
{
$this->produit = $produit;
return $this;
}
/**
* Get produit
*
* #return \VENTEBundle\Entity\Produit
*/
public function getProduit()
{
return $this->produit;
}
}

PHP error: Call to a member function format() on string

i have a problem, i don't know where is the problem come from. When i save my data to server, i've got error Call to a member function format() on string.
This is my Controller code:
public function newAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_HRM_SALARY_ADJUSTMENT_CREATE');
$corp = $this->getSelectedCorporationEntity();
$entity = new SalaryAdjustment();
$entity->setCorporation($corp);
if ($request->isMethod('POST')) {
$data = json_decode($request->getContent());
dump($data);
//$this->validationBeforeProgress('new', $data, $data->level, '', $data->docType);
$employeeEntity = $this->getDoctrine()->getRepository(Employee::class)->find($data->employee);
$entity->setEmployee($employeeEntity);
$entity->setNumber($data->number);
$entity->setEffectiveDate($data->effectiveDate);
$entity->setReference($data->reference);
$entity->setReferenceNumber($data->referenceNumber);
$entity->setRemarks($data->remarks);
$entity->setApprove(false);
$entity->setVoid(false);
$entity->setCreatedBy($this->getCurrentUser()->getUsername());
$entity->setCreatedDate(new \DateTime());
$em = $this->getEM();
$this->validateEntity($entity);
$em->persist($entity);
$newEntityDetail = $data->detail;
if (count($newEntityDetail) > 0) {
foreach ($newEntityDetail as $item) {
dump($item);
if ($item->newValue > 0 || $item->oldValue > 0){
$entityDetail = new SalaryAdjustmentDetail();
$lookupSalaryEntity = $this->getDoctrine()->getRepository(LookupSalary::class)->find($item->lookupSalaryId);
$entityDetail->setLookupSalary($lookupSalaryEntity);
if ($item->oldEffectiveDate != null) {
$entityDetail->setOldEffectiveDate($item->oldEffectiveDate);
}
$entityDetail->setOldValue($item->oldValue);
$entityDetail->setNewValue($item->newValue);
$entityDetail->setSalaryAdjustment($entity);
$this->validateEntity($entityDetail);
$em->persist($entityDetail);
}
}
} else {
throw new \Exception('Detail must be have minimal 1 row');
}
$em->flush();
}
return new JsonResponse(['data' => 'Done']);
}
the error come from when the code $em->flush(). I feel the problem come from when the code $entityDetail->setOldValue($item->oldValue); and entityDetail->setNewValue($item->newValue); because i'm using decimal type in entity for the first time.
i use 2 entity for save my data, the entity is SalaryAdjustment and SalaryAdjustmentDetail. There is the code:
class SalaryAdjustment
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="SalaryAdjustmentDetail", mappedBy="salaryAdjustment", cascade={"persist", "remove"})
*/
private $salaryAdjustmentDetail;
public function __construct() {
$this->salaryAdjustmentDetail = new ArrayCollection();
}
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\Employee\Employee")
* #ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
private $employee;
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAdministrationBundle\Entity\Corporation")
* #ORM\JoinColumn(name="corporation_id", referencedColumnName="id", nullable=false)
*/
private $corporation;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=20)
*/
private $number;
/**
* #var \DateTime
*
* #ORM\Column(name="effective_date", type="date")
*/
private $effectiveDate;
/**
* #var string
*
* #ORM\Column(name="reference", type="string", length=50, nullable=true)
*/
private $reference;
/**
* #var string
*
* #ORM\Column(name="reference_number", type="string", length=100, nullable=true)
*/
private $referenceNumber;
/**
* #var string
*
* #ORM\Column(name="remarks", type="string", length=300, nullable=true)
*/
private $remarks;
/**
* #var bool
*
* #ORM\Column(name="approve", type="boolean")
*/
private $approve;
/**
* #var bool
*
* #ORM\Column(name="void", type="boolean")
*/
private $void;
/**
* #var string
*
* #ORM\Column(name="created_by", type="string", length=50)
*/
private $createdBy;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime")
*/
private $createdDate;
/**
* #var string
*
* #ORM\Column(name="modified_by", type="string", length=50, nullable=true)
*/
private $modifiedBy;
/**
* #var \DateTime
*
* #ORM\Column(name="modified_date", type="datetime", nullable=true)
*/
private $modifiedDate;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Add salaryAdjustmentDetail
*
* #param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail
*
* #return SalaryAdjustment
*/
public function addSalaryAdjustmentDetail(SalaryAdjustmentDetail $salaryAdjustmentDetail)
{
$this->salaryAdjustmentDetail[] = $salaryAdjustmentDetail;
return $this;
}
/**
* Remove salaryAdjustmentDetail
*
* #param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail
*/
public function removeSalaryAdjustmentDetail(\Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail)
{
$this->salaryAdjustmentDetail->removeElement($salaryAdjustmentDetail);
}
/**
* Set employee
*
* #param \Dsp\DspAppsBundle\Entity\HRM\Employee\Employee $employee
*
* #return SalaryAdjustment
*/
public function setEmployee(\Dsp\DspAppsBundle\Entity\HRM\Employee\Employee $employee)
{
$this->employee = $employee;
return $this;
}
/**
* Get employee
*
* #return \Dsp\DspAppsBundle\Entity\HRM\Employee\Employee
*/
public function getEmployee()
{
return $this->employee;
}
/**
* Set corporation
*
* #param \Dsp\DspAdministrationBundle\Entity\Corporation $corporation
*
* #return SalaryAdjustment
*/
public function setCorporation(\Dsp\DspAdministrationBundle\Entity\Corporation $corporation)
{
$this->corporation = $corporation;
return $this;
}
/**
* Get corporation
*
* #return \Dsp\DspAdministrationBundle\Entity\Corporation
*/
public function getCorporation()
{
return $this->corporation;
}
/**
* Set number
*
* #param string $number
*
* #return SalaryAdjustment
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Set effectiveDate
*
* #param \DateTime $effectiveDate
*
* #return SalaryAdjustment
*/
public function setEffectiveDate($effectiveDate)
{
$this->effectiveDate = $effectiveDate;
return $this;
}
/**
* Get effectiveDate
*
* #return \DateTime
*/
public function getEffectiveDate()
{
return $this->effectiveDate;
}
/**
* Set reference
*
* #param string $reference
*
* #return SalaryAdjustment
*/
public function setReference($reference)
{
$this->reference = $reference;
return $this;
}
/**
* Get reference
*
* #return string
*/
public function getReference()
{
return $this->reference;
}
/**
* Set referenceNumber
*
* #param string $referenceNumber
*
* #return SalaryAdjustment
*/
public function setReferenceNumber($referenceNumber)
{
$this->referenceNumber = $referenceNumber;
return $this;
}
/**
* Get referenceNumber
*
* #return string
*/
public function getReferenceNumber()
{
return $this->referenceNumber;
}
/**
* Set remarks
*
* #param string $remarks
*
* #return SalaryAdjustment
*/
public function setRemarks($remarks)
{
$this->remarks = $remarks;
return $this;
}
/**
* Get remarks
*
* #return string
*/
public function getRemarks()
{
return $this->remarks;
}
/**
* Set approve
*
* #param boolean $approve
*
* #return SalaryAdjustment
*/
public function setApprove($approve)
{
$this->approve = $approve;
return $this;
}
/**
* Get approve
*
* #return bool
*/
public function getApprove()
{
return $this->approve;
}
/**
* Set void
*
* #param boolean $void
*
* #return SalaryAdjustment
*/
public function setVoid($void)
{
$this->void = $void;
return $this;
}
/**
* Get void
*
* #return bool
*/
public function getVoid()
{
return $this->void;
}
/**
* Set createdBy
*
* #param string $createdBy
*
* #return SalaryAdjustment
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Get createdBy
*
* #return string
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
*
* #return SalaryAdjustment
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set modifiedBy
*
* #param string $modifiedBy
*
* #return SalaryAdjustment
*/
public function setModifiedBy($modifiedBy)
{
$this->modifiedBy = $modifiedBy;
return $this;
}
/**
* Get modifiedBy
*
* #return string
*/
public function getModifiedBy()
{
return $this->modifiedBy;
}
/**
* Set modifiedDate
*
* #param \DateTime $modifiedDate
*
* #return SalaryAdjustment
*/
public function setModifiedDate($modifiedDate)
{
$this->modifiedDate = $modifiedDate;
return $this;
}
/**
* Get modifiedDate
*
* #return \DateTime
*/
public function getModifiedDate()
{
return $this->modifiedDate;
}
}
And this is my SalaryAdjustmentDetail:
class SalaryAdjustmentDetail
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary")
* #ORM\JoinColumn(name="lookup_salary_id", referencedColumnName="id", nullable=false)
*/
private $lookupSalary;
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment", inversedBy="SalaryAdjustmentDetail")
* #ORM\JoinColumn(name="salary_adjustment_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
private $salaryAdjustment;
/**
* #var \DateTime
*
* #ORM\Column(name="old_effective_date", type="date", nullable=true)
*/
private $oldEffectiveDate;
/**
* #var string
*
* #ORM\Column(name="old_value", type="decimal", precision=20, scale=4)
*/
private $oldValue;
/**
* #var string
*
* #ORM\Column(name="new_value", type="decimal", precision=20, scale=4)
*/
private $newValue;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set lookupSalary
*
* #param \Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary $lookupSalary
*
* #return SalaryAdjustmentDetail
*/
public function setLookupSalary(\Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary $lookupSalary)
{
$this->lookupSalary = $lookupSalary;
return $this;
}
/**
* Get lookupSalary
*
* #return \Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary
*/
public function getLookupSalary()
{
return $this->lookupSalary;
}
/**
* Set salaryAdjustment
*
* #param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment $salaryAdjustment
*
* #return SalaryAdjustmentDetail
*/
public function setSalaryAdjustment(\Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment $salaryAdjustment)
{
$this->salaryAdjustment = $salaryAdjustment;
return $this;
}
/**
* Get salaryAdjustment
*
* #return \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment
*/
public function getSalaryAdjustment()
{
return $this->salaryAdjustment;
}
/**
* Set oldEffectiveDate
*
* #param \DateTime $oldEffectiveDate
*
* #return SalaryAdjustmentDetail
*/
public function setOldEffectiveDate($oldEffectiveDate)
{
$this->oldEffectiveDate = $oldEffectiveDate;
return $this;
}
/**
* Get oldEffectiveDate
*
* #return \DateTime
*/
public function getOldEffectiveDate()
{
return $this->oldEffectiveDate;
}
/**
* Set oldValue
*
* #param string $oldValue
*
* #return SalaryAdjustmentDetail
*/
public function setOldValue($oldValue)
{
$this->oldValue = $oldValue;
return $this;
}
/**
* Get oldValue
*
* #return string
*/
public function getOldValue()
{
return $this->oldValue;
}
/**
* Set newValue
*
* #param string $newValue
*
* #return SalaryAdjustmentDetail
*/
public function setNewValue($newValue)
{
$this->newValue = $newValue;
return $this;
}
/**
* Get newValue
*
* #return string
*/
public function getNewValue()
{
return $this->newValue;
}
}
Please help me for resolve my problem :')

Symfony2 / Doctrine, A new entity was found through the relationship

trying to fix this over 48 hours. I have an entity BookingRequest.php which content is:
<?php
namespace Kutiwa\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BookingRequest
*
* #ORM\Table(name="booking_request")
* #ORM\Entity(repositoryClass="Kutiwa\PlatformBundle\Repository\BookingRequestRepository")
*/
class BookingRequest
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="code", type="string", length=255, unique=true)
*/
private $code;
/**
* #var string
*
* #ORM\Column(name="arrival", type="string", length=255)
*/
private $arrival;
/**
* #var string
*
* #ORM\Column(name="departure", type="string", length=255)
*/
private $departure;
/**
* #var string
*
* #ORM\Column(name="requestDate", type="string", length=255)
*/
private $requestDate;
/**
* #var int
*
* #ORM\Column(name="rooms", type="integer")
*/
private $rooms;
/**
* #var string
*
* #ORM\Column(name="minPricing", type="string", length=255)
*/
private $minPricing;
/**
* #var string
*
* #ORM\Column(name="maxPricing", type="string", length=255)
*/
private $maxPricing;
/**
* #var string
*
* #ORM\Column(name="customerName", type="string", length=255)
*/
private $customerName;
/**
* #var string
*
* #ORM\Column(name="customerEmail", type="string", length=255, nullable=true)
*/
private $customerEmail;
/**
* #var string
*
* #ORM\Column(name="customerPhone", type="string", length=255)
*/
private $customerPhone;
/**
* #ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\City")
* #ORM\JoinColumn(nullable=false)
*/
private $destinationCity;
/**
* #ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\City")
* #ORM\JoinColumn(nullable=false)
*/
private $customerCity;
/**
* #ORM\OneToOne(targetEntity="Kutiwa\PlatformBundle\Entity\MissionConfig", cascade={"persist"})
*/
private $missionConfig;
/**
* #ORM\OneToOne(targetEntity="Kutiwa\PlatformBundle\Entity\TourismConfig", cascade={"persist"})
*/
private $tourismConfig;
/**
* #ORM\OneToMany(targetEntity="Kutiwa\PlatformBundle\Entity\RoomsConfig", mappedBy="bookingRequest")
*/
private $roomsConfigs;
public function __construct() {
$this->setRequestDate(date("d/m/Y"));
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set code
*
* #param string $code
*
* #return BookingRequest
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code
*
* #return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set arrival
*
* #param string $arrival
*
* #return BookingRequest
*/
public function setArrival($arrival)
{
$this->arrival = $arrival;
return $this;
}
/**
* Get arrival
*
* #return string
*/
public function getArrival()
{
return $this->arrival;
}
/**
* Set departure
*
* #param string $departure
*
* #return BookingRequest
*/
public function setDeparture($departure)
{
$this->departure = $departure;
return $this;
}
/**
* Get departure
*
* #return string
*/
public function getDeparture()
{
return $this->departure;
}
/**
* Set requestDate
*
* #param string $requestDate
*
* #return BookingRequest
*/
public function setRequestDate($requestDate)
{
$this->requestDate = $requestDate;
return $this;
}
/**
* Get requestDate
*
* #return string
*/
public function getRequestDate()
{
return $this->requestDate;
}
/**
* Set rooms
*
* #param integer $rooms
*
* #return BookingRequest
*/
public function setRooms($rooms)
{
$this->rooms = $rooms;
return $this;
}
/**
* Get rooms
*
* #return integer
*/
public function getRooms()
{
return $this->rooms;
}
/**
* Set minPricing
*
* #param string $minPricing
*
* #return BookingRequest
*/
public function setMinPricing($minPricing)
{
$this->minPricing = $minPricing;
return $this;
}
/**
* Get minPricing
*
* #return string
*/
public function getMinPricing()
{
return $this->minPricing;
}
/**
* Set maxPricing
*
* #param string $maxPricing
*
* #return BookingRequest
*/
public function setMaxPricing($maxPricing)
{
$this->maxPricing = $maxPricing;
return $this;
}
/**
* Get maxPricing
*
* #return string
*/
public function getMaxPricing()
{
return $this->maxPricing;
}
/**
* Set customerName
*
* #param string $customerName
*
* #return BookingRequest
*/
public function setCustomerName($customerName)
{
$this->customerName = $customerName;
return $this;
}
/**
* Get customerName
*
* #return string
*/
public function getCustomerName()
{
return $this->customerName;
}
/**
* Set customerEmail
*
* #param string $customerEmail
*
* #return BookingRequest
*/
public function setCustomerEmail($customerEmail)
{
$this->customerEmail = $customerEmail;
return $this;
}
/**
* Get customerEmail
*
* #return string
*/
public function getCustomerEmail()
{
return $this->customerEmail;
}
/**
* Set customerPhone
*
* #param string $customerPhone
*
* #return BookingRequest
*/
public function setCustomerPhone($customerPhone)
{
$this->customerPhone = $customerPhone;
return $this;
}
/**
* Get customerPhone
*
* #return string
*/
public function getCustomerPhone()
{
return $this->customerPhone;
}
/**
* Set destinationCity
*
* #param \Kutiwa\PlatformBundle\Entity\City $destinationCity
*
* #return BookingRequest
*/
public function setDestinationCity(\Kutiwa\PlatformBundle\Entity\City $destinationCity)
{
$this->destinationCity = $destinationCity;
return $this;
}
/**
* Get destinationCity
*
* #return \Kutiwa\PlatformBundle\Entity\City
*/
public function getDestinationCity()
{
return $this->destinationCity;
}
/**
* Set customerCity
*
* #param \Kutiwa\PlatformBundle\Entity\City $customerCity
*
* #return BookingRequest
*/
public function setCustomerCity(\Kutiwa\PlatformBundle\Entity\City $customerCity)
{
$this->customerCity = $customerCity;
return $this;
}
/**
* Get customerCity
*
* #return \Kutiwa\PlatformBundle\Entity\City
*/
public function getCustomerCity()
{
return $this->customerCity;
}
/**
* Set missionConfig
*
* #param \Kutiwa\PlatformBundle\Entity\MissionConfig $missionConfig
*
* #return BookingRequest
*/
public function setMissionConfig(\Kutiwa\PlatformBundle\Entity\MissionConfig $missionConfig = null)
{
$this->missionConfig = $missionConfig;
return $this;
}
/**
* Get missionConfig
*
* #return \Kutiwa\PlatformBundle\Entity\MissionConfig
*/
public function getMissionConfig()
{
return $this->missionConfig;
}
/**
* Set tourismConfig
*
* #param \Kutiwa\PlatformBundle\Entity\TourismConfig $tourismConfig
*
* #return BookingRequest
*/
public function setTourismConfig(\Kutiwa\PlatformBundle\Entity\TourismConfig $tourismConfig = null)
{
$this->tourismConfig = $tourismConfig;
return $this;
}
/**
* Get tourismConfig
*
* #return \Kutiwa\PlatformBundle\Entity\TourismConfig
*/
public function getTourismConfig()
{
return $this->tourismConfig;
}
/**
* Add roomsConfig
*
* #param \Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig
*
* #return BookingRequest
*/
public function addRoomsConfig(\Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig)
{
$this->roomsConfigs[] = $roomsConfig;
return $this;
}
/**
* Remove roomsConfig
*
* #param \Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig
*/
public function removeRoomsConfig(\Kutiwa\PlatformBundle\Entity\RoomsConfig $roomsConfig)
{
$this->roomsConfigs->removeElement($roomsConfig);
}
/**
* Get roomsConfigs
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getRoomsConfigs()
{
return $this->roomsConfigs;
}
}
and another entity RoomsConfig.php
<?php
namespace Kutiwa\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RoomsConfig
*
* #ORM\Table(name="rooms_config")
* #ORM\Entity(repositoryClass="Kutiwa\PlatformBundle\Repository\RoomsConfigRepository")
*/
class RoomsConfig
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var bool
*
* #ORM\Column(name="airConditionner", type="boolean")
*/
private $airConditionner;
/**
* #var bool
*
* #ORM\Column(name="wifi", type="boolean")
*/
private $wifi;
/**
* #var bool
*
* #ORM\Column(name="balcony", type="boolean")
*/
private $balcony;
/**
* #var string
*
* #ORM\Column(name="tv", type="boolean")
*/
private $tv;
/**
* #ORM\ManyToOne(targetEntity="Kutiwa\PlatformBundle\Entity\BookingRequest", inversedBy="roomsConfigs", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $bookingRequest;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set airConditionner
*
* #param boolean $airConditionner
*
* #return RoomsConfig
*/
public function setAirConditionner($airConditionner)
{
$this->airConditionner = $airConditionner;
return $this;
}
/**
* Get airConditionner
*
* #return boolean
*/
public function getAirConditionner()
{
return $this->airConditionner;
}
/**
* Set wifi
*
* #param boolean $wifi
*
* #return RoomsConfig
*/
public function setWifi($wifi)
{
$this->wifi = $wifi;
return $this;
}
/**
* Get wifi
*
* #return boolean
*/
public function getWifi()
{
return $this->wifi;
}
/**
* Set balcony
*
* #param boolean $balcony
*
* #return RoomsConfig
*/
public function setBalcony($balcony)
{
$this->balcony = $balcony;
return $this;
}
/**
* Get balcony
*
* #return boolean
*/
public function getBalcony()
{
return $this->balcony;
}
/**
* Set tv
*
* #param boolean $tv
*
* #return RoomsConfig
*/
public function setTv($tv)
{
$this->tv = $tv;
return $this;
}
/**
* Get tv
*
* #return boolean
*/
public function getTv()
{
return $this->tv;
}
/**
* Set bookingRequest
*
* #param \Kutiwa\PlatformBundle\Entity\BookingRequest $bookingRequest
*
* #return RoomsConfig
*/
public function setBookingRequest(\Kutiwa\PlatformBundle\Entity\BookingRequest $bookingRequest)
{
$this->bookingRequest = $bookingRequest;
$bookingRequest->addRoomsConfig($this);
return $this;
}
/**
* Get bookingRequest
*
* #return \Kutiwa\PlatformBundle\Entity\BookingRequest
*/
public function getBookingRequest()
{
return $this->bookingRequest;
}
}
The problem is, when I try to persist a RoomsConfig entity, I got an error
A new entity was found through the relationship
'Kutiwa\PlatformBundle\Entity\BookingRequest#destinationCity' that was
not configured to cascade persist operations for entity: TIKO. To
solve this issue: Either explicitly call EntityManager#persist() on
this unknown entity or configure cascade persist this association in
the mapping for example #ManyToOne(..,cascade={"persist"})
I don't need a cascade effect on City and BookingRequest relation, here is my BookingRequestType.php
<?php
namespace Kutiwa\PlatformBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BookingRequestType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('arrival', TextType::class)
->add('departure', TextType::class)
->add('rooms', IntegerType::class)
->add('customerName', TextType::class)
->add('customerEmail', TextType::class, array('required' => false))
->add('customerPhone', TextType::class)
->add('destinationCity', EntityType::class, array(
'choice_label' => 'name',
'class' => 'KutiwaPlatformBundle:City'
))
->add('customerCity', EntityType::class, array(
'choice_label' => 'name',
'class' => 'KutiwaPlatformBundle:City'
));
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Kutiwa\PlatformBundle\Entity\BookingRequest'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'kutiwa_platformbundle_bookingrequest';
}
}
If I try to persist only BookingRequest entity, there is no problem, but when I persist RoomsConfig entity, I got that error. Help please. Thks.
Before persist your RoomsConfig, try to use findOneBy to find BookingRequest, or whatever you use to find the entity, and then set the found entity with setBookingRequest.

Use data from database in base view (twig) + symfony2

I have a basic view that I use on every page. In my basic view I have:
<div class="container">
{% block body %}
{% endblock %}
</div>
Where the body of every page comes. In my base view I have also a navigation. Now I would like to show a button depending on some data of the user logged in.
This is my case:
I have a table players with a FK user_id and a FK team_id. The user_id refers to my user table where the username, password, ... is saved. Now I would like to show the button depending on team_id in players table is NULL or not.
So I would need something like this:
if(is_null($user->getPlayer()->getTeam())
{
// DON'T SHOW BUTTON
}
else{
// SHOW BUTTON
}
But how I can use that data in my base view? (It's rendered on every page)
UPDATE:
My Users Entity:
<?php
namespace VolleyScout\VolleyScoutBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* Users
*
* #ORM\Table(name="users", indexes={#ORM\Index(name="fk_users_roles1_idx", columns={"role_id"})})
* #ORM\Entity
*/
class Users implements AdvancedUserInterface
{
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=45, nullable=false)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=60, nullable=false)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="salt", type="string", length=30, nullable=false)
*/
private $salt;
/**
* #var string
*
* #ORM\Column(name="user_firstname", type="string", length=45, nullable=false)
*/
private $userFirstname;
/**
* #var string
*
* #ORM\Column(name="user_surname", type="string", length=255, nullable=false)
*/
private $userSurname;
/**
* #var string
*
* #ORM\Column(name="user_email", type="string", length=255, nullable=false)
*/
private $userEmail;
/**
* #var string
*
* #ORM\Column(name="user_type", type="string", nullable=false)
*/
private $userType;
/**
* #var string
*
* #ORM\Column(name="user_token", type="string", length=45, nullable=true)
*/
private $userToken;
/**
* #var \DateTime
*
* #ORM\Column(name="user_created", type="datetime", nullable=false)
*/
private $userCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="user_modified", type="datetime", nullable=true)
*/
private $userModified;
/**
* #var \DateTime
*
* #ORM\Column(name="user_deleted", type="datetime", nullable=true)
*/
private $userDeleted;
/**
* #var \DateTime
*
* #ORM\Column(name="user_lastlogin", type="datetime", nullable=true)
*/
private $userLastlogin;
/**
* #var \DateTime
*
* #ORM\Column(name="user_confirmed", type="datetime", nullable=true)
*/
private $userConfirmed;
/**
* #var \DateTime
*
* #ORM\Column(name="user_locked", type="datetime", nullable=true)
*/
private $userLocked;
/**
* #var integer
*
* #ORM\Column(name="user_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* #var \VolleyScout\VolleyScoutBundle\Entity\Roles
*
* #ORM\ManyToOne(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Roles")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="role_id", referencedColumnName="role_id")
* })
*/
protected $role;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Teams", inversedBy="user")
* #ORM\JoinTable(name="user_follows_teams",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="team_id", referencedColumnName="team_id")
* }
* )
*/
private $team;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="VolleyScout\VolleyScoutBundle\Entity\Competitions", inversedBy="user")
* #ORM\JoinTable(name="user_follows_competitions",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="competition_id", referencedColumnName="competition_id")
* }
* )
*/
private $competition;
private $plainPassword;
/**
* Constructor
*/
public function __construct()
{
$this->team = new \Doctrine\Common\Collections\ArrayCollection();
$this->competition = new \Doctrine\Common\Collections\ArrayCollection();
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
}
/**
* Set username
*
* #param string $username
* #return Users
*/
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 Users
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* #param string $salt
* #return Users
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* #return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set userFirstname
*
* #param string $userFirstname
* #return Users
*/
public function setUserFirstname($userFirstname)
{
$this->userFirstname = $userFirstname;
return $this;
}
/**
* Get userFirstname
*
* #return string
*/
public function getUserFirstname()
{
return $this->userFirstname;
}
/**
* Set userSurname
*
* #param string $userSurname
* #return Users
*/
public function setUserSurname($userSurname)
{
$this->userSurname = $userSurname;
return $this;
}
/**
* Get userSurname
*
* #return string
*/
public function getUserSurname()
{
return $this->userSurname;
}
/**
* 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 userType
*
* #param string $userType
* #return Users
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return string
*/
public function getUserType()
{
return $this->userType;
}
/**
* Set userToken
*
* #param string $userToken
* #return Users
*/
public function setUserToken($userToken)
{
$this->userToken = $userToken;
return $this;
}
/**
* Get userToken
*
* #return string
*/
public function getUserToken()
{
return $this->userToken;
}
/**
* Set userCreated
*
* #param \DateTime $userCreated
* #return Users
*/
public function setUserCreated($userCreated)
{
$this->userCreated = $userCreated;
return $this;
}
/**
* Get userCreated
*
* #return \DateTime
*/
public function getUserCreated()
{
return $this->userCreated;
}
/**
* Set userModified
*
* #param \DateTime $userModified
* #return Users
*/
public function setUserModified($userModified)
{
$this->userModified = $userModified;
return $this;
}
/**
* Get userModified
*
* #return \DateTime
*/
public function getUserModified()
{
return $this->userModified;
}
/**
* Set userDeleted
*
* #param \DateTime $userDeleted
* #return Users
*/
public function setUserDeleted($userDeleted)
{
$this->userDeleted = $userDeleted;
return $this;
}
/**
* Get userDeleted
*
* #return \DateTime
*/
public function getUserDeleted()
{
return $this->userDeleted;
}
/**
* Set userLastlogin
*
* #param \DateTime $userLastlogin
* #return Users
*/
public function setUserLastlogin($userLastlogin)
{
$this->userLastlogin = $userLastlogin;
return $this;
}
/**
* Get userLastlogin
*
* #return \DateTime
*/
public function getUserLastlogin()
{
return $this->userLastlogin;
}
/**
* Set userConfirmed
*
* #param \DateTime $userConfirmed
* #return Users
*/
public function setUserConfirmed($userConfirmed)
{
$this->userConfirmed = $userConfirmed;
return $this;
}
/**
* Get userConfirmed
*
* #return \DateTime
*/
public function getUserConfirmed()
{
return $this->userConfirmed;
}
/**
* Set userLocked
*
* #param \DateTime $userLocked
* #return Users
*/
public function setUserLocked($userLocked)
{
$this->userLocked = $userLocked;
return $this;
}
/**
* Get userLocked
*
* #return \DateTime
*/
public function getUserLocked()
{
return $this->userLocked;
}
/**
* Get userId
*
* #return integer
*/
public function getUserId()
{
return $this->userId;
}
/**
* Set role
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Roles $role
* #return Users
*/
public function setRoles(\VolleyScout\VolleyScoutBundle\Entity\Roles $role = null)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Roles
*/
public function getRoles()
{
return array($this->role->getRoleName());
}
/**
* Add team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
* #return Users
*/
public function addTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
{
$this->team[] = $team;
return $this;
}
/**
* Remove team
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Teams $team
*/
public function removeTeam(\VolleyScout\VolleyScoutBundle\Entity\Teams $team)
{
$this->team->removeElement($team);
}
/**
* Get team
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTeam()
{
return $this->team;
}
/**
* Add competition
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
* #return Users
*/
public function addCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
{
$this->competition[] = $competition;
return $this;
}
/**
* Remove competition
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Competitions $competition
*/
public function removeCompetition(\VolleyScout\VolleyScoutBundle\Entity\Competitions $competition)
{
$this->competition->removeElement($competition);
}
/**
* Get competition
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCompetition()
{
return $this->competition;
}
//**********************************
// HAD TO IMPLEMENT THESE BY MESELF
//**********************************//
private $player;
/**
* Get player
*
* #return \VolleyScout\VolleyScoutBundle\Entity\Players
*/
public function getPlayer() {
return $this->player;
}
/**
* Set player
*
* #param \VolleyScout\VolleyScoutBundle\Entity\Players $player
* #return Users
*/
public function setPlayer(\VolleyScout\VolleyScoutBundle\Entity\Players $player = null){
$this->player = $player;
return $this;
}
public function eraseCredentials()
{
$this->setPlainPassword(null);
}
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
}
/**
* Implementation of AdvancedUserInterface method
*
* #return boolean
*/
public function isAccountNonExpired()
{
return true;
}
/**
* Implementation of AdvancedUserInterface method
*
* #return boolean
*/
public function isAccountNonLocked()
{
return true;
}
/**
* Implementation of AdvancedUserInterface method
*
* #return boolean
*/
public function isCredentialsNonExpired()
{
return true;
}
/**
* Implementation of AdvancedUserInterface method
*
* #return boolean
*/
public function isEnabled()
{
// CHECK IF $this->confirmed is not null
if($this->userConfirmed != null){
return true;
}
}
}
The current logged in user is stored in the twig global variable app.user
{% if app.user.player.team %}
show button
{% else %}
not
{% endif %}

Categories