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

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 %}

Related

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 :')

object USER entity is null when i try to access it from twig template

I can't access to user entity by topic entity.
In the template twig i use dump to display the result query...:
{% for forum in listForums %}
{{ dump (forum.topics.last)}}
....
we can see that User entity is empty (null):
Topic {#1456 ▼
-Forum: Forum {#435 ▶}
-Posts: PersistentCollection {#1457 ▶}
-id: 65
-title: "How to go over there"
-User: User {#1294 ▼
-_entityPersister: BasicEntityPersister {#1291 ▶}
-_identifier: array:1 [▶]
+__isInitialized__: false
-Topics: null
#id: null
-pseudo: null
-name: null
-firstname: null
-website: null
-avatar: null
-signature: null
-location: null
-registration: null
-lastVisit: null
-rank: null
-nbPost: null
-nbTopic: null
#username: null
#usernameCanonical: null
#email: null
#emailCanonical: null
#enabled: null
#salt: null
#password: null
#plainPassword: null
#lastLogin: null
#confirmationToken: null
#passwordRequestedAt: null
#groups: null
#locked: null
#expired: null
#expiresAt: null
#roles: null
#credentialsExpired: null
#credentialsExpireAt: null
}
-viewCount: 23
-dateCreation: DateTime {#1455 ▶}
-replyCount: 123
-slug: "slug_sluggg"
-genre: "genre"
-lastPost: 25
-content: """
<p>test</p>\r\n
\r\n
<p>test2</p>\r\n
\r\n
<p>test3</p>
"""
}
Part of Controller
class FController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$listForums = $em->getRepository('BISSAPForumBundle:Forum')->findAllOrderByCategory();
return $this->render('BISSAPForumBundle:F:index-forum.html.twig', array('listForums' => $listForums, 'user' => $user));
}
}
Topic.php
<?php
namespace BISSAP\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/*use BISSAP\BodyConcept\Entity\Forum;
*/
/**
* Topic
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\TopicRepository")
*/
class Topic
{
/**
* #ORM\ManyToOne(targetEntity="Forum", inversedBy="Topics", cascade={"persist"})
* #ORM\JoinColumn(name="forum_id", referencedColumnName="id")
*/
private $Forum;
/**
* #var ArrayCollection $Posts
*
* #ORM\OneToMany(targetEntity="Post", mappedBy="Topic", cascade={"persist", "remove", "merge"})
*/
private $Posts;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #ORM\ManyToOne(targetEntity="BISSAP\UserBundle\Entity\User", inversedBy="Topics", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $User;
/**
* #var integer
*
* #ORM\Column(name="view_count", type="integer")
*/
private $viewCount;
/**
* #var \DateTime
*
* #ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* #var integer
*
* #ORM\Column(name="reply_count", type="integer")
*/
private $replyCount;
/**
* #var string
*
* #ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* #var string
*
* #ORM\Column(name="genre", type="string", length=255)
*/
private $genre;
/**
* #var integer
*
* #ORM\Column(name="last_post", type="integer")
*/
private $lastPost;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Topic
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set user
*
* #param integer $user
* #return Topic
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return integer
*/
public function getUser()
{
return $this->user;
}
/**
* Set viewCount
*
* #param integer $viewCount
* #return Topic
*/
public function setViewCount($viewCount)
{
$this->viewCount = $viewCount;
return $this;
}
/**
* Get viewCount
*
* #return integer
*/
public function getViewCount()
{
return $this->viewCount;
}
/**
* Set dateCreation
*
* #param \DateTime $dateCreation
* #return Topic
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set replyCount
*
* #param integer $replyCount
* #return Topic
*/
public function setReplyCount($replyCount)
{
$this->replyCount = $replyCount;
return $this;
}
/**
* Get replyCount
*
* #return integer
*/
public function getReplyCount()
{
return $this->replyCount;
}
/**
* Set slug
*
* #param string $slug
* #return Topic
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set genre
*
* #param string $genre
* #return Topic
*/
public function setGenre($genre)
{
$this->genre = $genre;
return $this;
}
/**
* Get genre
*
* #return string
*/
public function getGenre()
{
return $this->genre;
}
/**
* Set lastPost
*
* #param integer $lastPost
* #return Topic
*/
public function setLastPost($lastPost)
{
$this->lastPost = $lastPost;
return $this;
}
/**
* Get lastPost
*
* #return integer
*/
public function getLastPost()
{
return $this->lastPost;
}
/**
* Set content
*
* #param string $content
* #return Topic
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set forum
*
* #param Forum $forum
* #return Topic
*/
/*public function setForum(\BISSAP\BodyConceptBundle\Entity\Forum $forum)*/
public function setForum(Forum $forum)
{
$this->forum = $forum;
return $this;
}
/**
* Get forum
*
* #return \BISSAP\BodyConceptBundle\Entity\Forum
*/
public function getForum()
{
return $this->forum;
}
/**
* Constructor
*/
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
$this->dateCreation = new \DateTime();
}
/**
* Add posts
*
* #param \BISSAP\ForumBundle\Entity\Post $posts
* #return Topic
*/
public function addPost(\BISSAP\ForumBundle\Entity\Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* #param \BISSAP\ForumBundle\Entity\Post $posts
*/
public function removePost(\BISSAP\ForumBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
User.php
<?php
namespace BISSAP\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* BISSAP\UserBundle\Entity\User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="BISSAP\UserBundle\Entity\UserRepository")
*/
class User extends BaseUser
{
/**
* #var ArrayCollection $Topics
*
* #ORM\OneToMany(targetEntity="\BISSAP\ForumBundle\Entity\Topic", mappedBy="User", cascade={"persist", "remove", "merge"})
*/
private $Topics;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="pseudo", type="string", length=30, nullable=true)
*/
private $pseudo;
/**
* #ORM\Column(name="name", type="string", length=255)
*
* #Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* #Assert\Length(
* min=3,
* max=255,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
private $name;
/**
* #ORM\Column(name="firstname", type="string", length=255)
*
* #Assert\NotBlank(message="Please enter your firstname.", groups={"Registration", "Profile"})
* #Assert\Length(
* min=3,
* max=255,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
private $firstname;
/**
* #var string
*
* #ORM\Column(name="website", type="string", length=100, nullable=true)
*/
private $website;
/**
* #var string
*
* #ORM\Column(name="avatar", type="string", length=100, nullable=true)
*/
private $avatar;
/**
* #var string
*
* #ORM\Column(name="signature", type="string", length=200, nullable=true)
*/
private $signature;
/**
* #var string
*
* #ORM\Column(name="location", type="string", length=100, nullable=true)
*/
private $location;
/**
* #var \DateTime
*
* #ORM\Column(name="registration", type="datetime")
*/
private $registration;
/**
* #var \DateTime
*
* #ORM\Column(name="last_visit", type="datetime")
*/
private $lastVisit;
/**
* #var integer
*
* #ORM\Column(name="rank", type="integer", nullable=true)
*/
private $rank;
/**
* #var integer
*
* #ORM\Column(name="nb_post", type="integer", nullable=true)
*/
private $nbPost;
/**
* #var integer
*
* #ORM\Column(name="nb_topic", type="integer", nullable=true)
*/
private $nbTopic;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set pseudo
*
* #param string $pseudo
* #return User
*/
public function setPseudo($pseudo)
{
$this->pseudo = $pseudo;
return $this;
}
/**
* Get pseudo
*
* #return string
*/
public function getPseudo()
{
return $this->pseudo;
}
/**
* 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;
}
/**
* 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 website
*
* #param string $website
* #return User
*/
public function setWebsite($website)
{
$this->website = $website;
return $this;
}
/**
* Get website
*
* #return string
*/
public function getWebsite()
{
return $this->website;
}
/**
* Set avatar
*
* #param string $avatar
* #return User
*/
public function setAvatar($avatar)
{
$this->avatar = $avatar;
return $this;
}
/**
* Get avatar
*
* #return string
*/
public function getAvatar()
{
return $this->avatar;
}
/**
* Set signature
*
* #param string $signature
* #return User
*/
public function setSignature($signature)
{
$this->signature = $signature;
return $this;
}
/**
* Get signature
*
* #return string
*/
public function getSignature()
{
return $this->signature;
}
/**
* Set location
*
* #param string $location
* #return User
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}
/**
* Get location
*
* #return string
*/
public function getLocation()
{
return $this->location;
}
/**
* Set registration
*
* #param \DateTime $registration
* #return User
*/
public function setRegistration($registration)
{
$this->registration = $registration;
return $this;
}
/**
* Get registration
*
* #return \DateTime
*/
public function getRegistration()
{
return $this->registration;
}
/**
* Set lastVisit
*
* #param \DateTime $lastVisit
* #return User
*/
public function setLastVisit($lastVisit)
{
$this->lastVisit = $lastVisit;
return $this;
}
/**
* Get lastVisit
*
* #return \DateTime
*/
public function getLastVisit()
{
return $this->lastVisit;
}
/**
* Set rank
*
* #param integer $rank
* #return User
*/
public function setRank($rank)
{
$this->rank = $rank;
return $this;
}
/**
* Get rank
*
* #return integer
*/
public function getRank()
{
return $this->rank;
}
/**
* Set nbPost
*
* #param integer $nbPost
* #return User
*/
public function setNbPost($nbPost)
{
$this->nbPost = $nbPost;
return $this;
}
/**
* Get nbPost
*
* #return integer
*/
public function getNbPost()
{
return $this->nbPost;
}
/**
* Set nbTopic
*
* #param integer $nbTopic
* #return User
*/
public function setNbTopic($nbTopic)
{
$this->nbTopic = $nbTopic;
return $this;
}
/**
* Get nbTopic
*
* #return integer
*/
public function getNbTopic()
{
return $this->nbTopic;
}
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->Topics = new \Doctrine\Common\Collections\ArrayCollection();
$this->registration = new \DateTime();
$this->lastVisit = new \DateTime();
}
/**
* Add Topics
*
* #param \BISSAP\ForumBundle\Entity\Topic $topics
* #return User
*/
public function addTopic(\BISSAP\ForumBundle\Entity\Topic $topics)
{
$this->Topics[] = $topics;
return $this;
}
/**
* Remove Topics
*
* #param \BISSAP\ForumBundle\Entity\Topic $topics
*/
public function removeTopic(\BISSAP\ForumBundle\Entity\Topic $topics)
{
$this->Topics->removeElement($topics);
}
/**
* Get Topics
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTopics()
{
return $this->Topics;
}
/**
* Set name
*
* #param string $name
* #return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* 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;
}
}
I would like to see the request generated by Doctrine but i don't know the way to display it.
So i'm trying to access to pseudo user with {{ forum.topics.last.user.pseudo }}
I get Error:
An exception has been thrown during the rendering of a template ("Notice: Undefined property: BISSAP\ForumBundle\Entity\Topic::$user") in BISSAPForumBundle:F:index-forum.html.twig at line 53.
I moved $User by $user and i get :
Impossible to access an attribute ("user") on a boolean variable ("") in BISSAPForumBundle:F:index-forum.html.twig at line 53
UserRepository::findAllOrderByCategory() doesn't exist but
ForumRepository::findAllOrderByCategory() :
class ForumRepository extends EntityRepository
{
public function findAllOrderByCategory()
{
return $this->createQueryBuilder('p')
->leftJoin('p.Category','c')
->orderBy('c.ordre', 'desc')
->getQuery()
->getResult();
}
}
Doctrine is lazy-loading related objects by default, unless the related object is already loaded. Accessing the related object (i.e. echo-ing Topic.user.pseudo) should trigger loading the full object.
See the footnote at: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#joins
If that doesn't work, could you add a screenshot of the error message and the contents of your UserRepository::findAllOrderByCategory() method in a response?

replace the authentication layer and user entity in sylius

I have created a symfony2 project and custom Authentication Provider (http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html) is used. my new task is to create an E-commerce application in sylius which should work alongside with my current symfony2 project. i want to make my current user table in symfony2 project as user provider in sylius...is it possible to create such project..... since fosUserBundle and fosOauthServer bundel are installed in sylius how can i override these bundles with my custom authentication mechanism
My symfony2 project configurations are mentioned below
I tried the following in security.yml
security:
encoders:
AppBundle\Entity\Users:
algorithm: bcrypt
providers:
our_db_provider:
entity:
class: AppBundle:Users
api_key_user_provider:
id: api_key_user_provider
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
api:
pattern: ^/api
stateless: true
simple_preauth:
authenticator: apikey_authenticator
provider: api_key_user_provider
web:
anonymous: ~
http_basic: ~
provider: our_db_provider
form_login:
login_path: /login
check_path: /login_check
this is my user class
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Users
*
* #ORM\Table(name="users", uniqueConstraints= {#ORM\UniqueConstraint(name="users_user_name_unique", columns={"user_name"}), #ORM\UniqueConstraint(name="users_xmpp_password_unique", columns= {"xmpp_password"})})
* #ORM\Entity(repositoryClass="AppBundle\Entity\UsersRepository")
*/
class Users implements UserInterface, \Serializable {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="parent_id", type="integer", nullable=false)
*/
private $parentId;
/**
* #var string
*
* #ORM\Column(name="first_name", type="string", length=100, nullable=false)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="last_name", type="string", length=100, nullable=false)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="user_name", type="string", length=120, nullable=false)
*/
private $userName;
/**
* #var string
*
* #ORM\Column(name="reg_type", type="string", length=20, nullable=false)
*/
private $regType;
/**
* #var string
*
* #ORM\Column(name="oauth_uid", type="string", length=255, nullable=false)
*/
private $oauthUid;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean", nullable=false)
*/
private $active;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=100, nullable=false)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=150, nullable=false)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="xmpp_password", type="string", length=20, nullable=false)
*/
private $xmppPassword;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt = '0000-00-00 00:00:00';
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt = '0000-00-00 00:00:00';
/**
* #var \DateTime
*
* #ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* #var string
*
* #ORM\Column(name="activation_code", type="string", length=50, nullable=true)
*/
private $activationCode;
/**
* #var string
*
* #ORM\Column(name="user_profile_pic", type="string", length=200, nullable=false)
*/
private $userProfilePic = 'uploads/defaults/user/profile_pic.jpg';
/**
* #var string
*
* #ORM\Column(name="user_timeline_pic", type="string", length=200, nullable=false)
*/
private $userTimelinePic = 'uploads/defaults/user/timeline_pic.jpg';
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=50, nullable=false)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=50, nullable=false)
*/
private $state;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=50, nullable=false)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="hobbies", type="string", length=100, nullable=false)
*/
private $hobbies;
/**
* #var string
*
* #ORM\Column(name="interests", type="string", length=100, nullable=false)
*/
private $interests;
/**
* #var string
*
* #ORM\Column(name="about", type="string", length=500, nullable=false)
*/
private $about;
/**
* #var boolean
*
* #ORM\Column(name="gender", type="boolean", nullable=false)
*/
private $gender;
/**
* #var \DateTime
*
* #ORM\Column(name="dob", type="date", nullable=false)
*/
private $dob;
/**
* #var integer
*
* #ORM\Column(name="quickblox_id", type="integer", nullable=false)
*/
private $quickbloxId;
/**
* #var boolean
*
* #ORM\Column(name="privacy", type="boolean", nullable=false)
*/
private $privacy = '0';
/**
* #var string
*
* #ORM\Column(name="school", type="string", length=255, nullable=false)
*/
private $school;
/**
* #var string
*
* #ORM\Column(name="college", type="string", length=255, nullable=false)
*/
private $college;
/**
* #var string
*
* #ORM\Column(name="work", type="string", length=255, nullable=false)
*/
private $work;
/**
* #var string
*
* #ORM\Column(name="relationship_status", type="string", length=50, nullable=false)
*/
private $relationshipStatus;
public function getSalt() {
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getRoles() {
return array('ROLE_API');
}
public function eraseCredentials() {
}
/** #see \Serializable::serialize() */
public function serialize() {
return serialize(array(
$this->id,
$this->email,
// see section on salt below
// $this->salt,
));
}
/** #see \Serializable::unserialize() */
public function unserialize($serialized) {
list (
$this->id,
$this->email,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set parentId
*
* #param integer $parentId
* #return Users
*/
public function setParentId($parentId) {
$this->parentId = $parentId;
return $this;
}
/**
* Get parentId
*
* #return integer
*/
public function getParentId() {
return $this->parentId;
}
/**
* Set firstName
*
* #param string $firstName
* #return Users
*/
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 Users
*/
public function setLastName($lastName) {
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName() {
return $this->lastName;
}
/**
* 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->email;
}
/**
* Set regType
*
* #param string $regType
* #return Users
*/
public function setRegType($regType) {
$this->regType = $regType;
return $this;
}
/**
* Get regType
*
* #return string
*/
public function getRegType() {
return $this->regType;
}
/**
* Set oauthUid
*
* #param string $oauthUid
* #return Users
*/
public function setOauthUid($oauthUid) {
$this->oauthUid = $oauthUid;
return $this;
}
/**
* Get oauthUid
*
* #return string
*/
public function getOauthUid() {
return $this->oauthUid;
}
/**
* Set active
*
* #param boolean $active
* #return Users
*/
public function setActive($active) {
$this->active = $active;
return $this;
}
/**
* Get active
*
* #return boolean
*/
public function getActive() {
return $this->active;
}
/**
* Set email
*
* #param string $email
* #return Users
*/
public function setEmail($email) {
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail() {
return $this->email;
}
/**
* 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 xmppPassword
*
* #param string $xmppPassword
* #return Users
*/
public function setXmppPassword($xmppPassword) {
$this->xmppPassword = $xmppPassword;
return $this;
}
/**
* Get xmppPassword
*
* #return string
*/
public function getXmppPassword() {
return $this->xmppPassword;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Users
*/
public function setCreatedAt($createdAt) {
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt() {
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Users
*/
public function setUpdatedAt($updatedAt) {
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt() {
return $this->updatedAt;
}
/**
* Set deletedAt
*
* #param \DateTime $deletedAt
* #return Users
*/
public function setDeletedAt($deletedAt) {
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get deletedAt
*
* #return \DateTime
*/
public function getDeletedAt() {
return $this->deletedAt;
}
/**
* Set activationCode
*
* #param string $activationCode
* #return Users
*/
public function setActivationCode($activationCode) {
$this->activationCode = $activationCode;
return $this;
}
/**
* Get activationCode
*
* #return string
*/
public function getActivationCode() {
return $this->activationCode;
}
/**
* Set userProfilePic
*
* #param string $userProfilePic
* #return Users
*/
public function setUserProfilePic($userProfilePic) {
$this->userProfilePic = $userProfilePic;
return $this;
}
/**
* Get userProfilePic
*
* #return string
*/
public function getUserProfilePic() {
return $this->userProfilePic;
}
/**
* Set userTimelinePic
*
* #param string $userTimelinePic
* #return Users
*/
public function setUserTimelinePic($userTimelinePic) {
$this->userTimelinePic = $userTimelinePic;
return $this;
}
/**
* Get userTimelinePic
*
* #return string
*/
public function getUserTimelinePic() {
return $this->userTimelinePic;
}
/**
* Set country
*
* #param string $country
* #return Users
*/
public function setCountry($country) {
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry() {
return $this->country;
}
/**
* Set state
*
* #param string $state
* #return Users
*/
public function setState($state) {
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string
*/
public function getState() {
return $this->state;
}
/**
* Set city
*
* #param string $city
* #return Users
*/
public function setCity($city) {
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity() {
return $this->city;
}
/**
* Set hobbies
*
* #param string $hobbies
* #return Users
*/
public function setHobbies($hobbies) {
$this->hobbies = $hobbies;
return $this;
}
/**
* Get hobbies
*
* #return string
*/
public function getHobbies() {
return $this->hobbies;
}
/**
* Set interests
*
* #param string $interests
* #return Users
*/
public function setInterests($interests) {
$this->interests = $interests;
return $this;
}
/**
* Get interests
*
* #return string
*/
public function getInterests() {
return $this->interests;
}
/**
* Set about
*
* #param string $about
* #return Users
*/
public function setAbout($about) {
$this->about = $about;
return $this;
}
/**
* Get about
*
* #return string
*/
public function getAbout() {
return $this->about;
}
/**
* Set gender
*
* #param boolean $gender
* #return Users
*/
public function setGender($gender) {
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return boolean
*/
public function getGender() {
return $this->gender;
}
/**
* Set dob
*
* #param \DateTime $dob
* #return Users
*/
public function setDob($dob) {
$this->dob = $dob;
return $this;
}
/**
* Get dob
*
* #return \DateTime
*/
public function getDob() {
return $this->dob;
}
/**
* Set quickbloxId
*
* #param integer $quickbloxId
* #return Users
*/
public function setQuickbloxId($quickbloxId) {
$this->quickbloxId = $quickbloxId;
return $this;
}
/**
* Get quickbloxId
*
* #return integer
*/
public function getQuickbloxId() {
return $this->quickbloxId;
}
/**
* Set privacy
*
* #param boolean $privacy
* #return Users
*/
public function setPrivacy($privacy) {
$this->privacy = $privacy;
return $this;
}
/**
* Get privacy
*
* #return boolean
*/
public function getPrivacy() {
return $this->privacy;
}
/**
* Set school
*
* #param string $school
* #return Users
*/
public function setSchool($school) {
$this->school = $school;
return $this;
}
/**
* Get school
*
* #return string
*/
public function getSchool() {
return $this->school;
}
/**
* Set college
*
* #param string $college
* #return Users
*/
public function setCollege($college) {
$this->college = $college;
return $this;
}
/**
* Get college
*
* #return string
*/
public function getCollege() {
return $this->college;
}
/**
* Set work
*
* #param string $work
* #return Users
*/
public function setWork($work) {
$this->work = $work;
return $this;
}
/**
* Get work
*
* #return string
*/
public function getWork() {
return $this->work;
}
/**
* Set relationshipStatus
*
* #param string $relationshipStatus
* #return Users
*/
public function setRelationshipStatus($relationshipStatus) {
$this->relationshipStatus = $relationshipStatus;
return $this;
}
/**
* Get relationshipStatus
*
* #return string
*/
public function getRelationshipStatus() {
return $this->relationshipStatus;
}
You can find the answer here https://github.com/Sylius/Sylius/issues/2931.

Entity class has no field or association named like in my DB

I want to create an request that recovers a number according to e.rules_id, e.status_id
public function findStat()
{
$types = $this->em
->getRepository('EthanRestBundle:EthanRules')
->createQueryBuilder('e')
->select('e.rules_id, e.status_id, COUNT(*)')
->groupBy('e.rules_id, e.status_id')
->orderBy('rules_id');
$types = $types->getQuery()->getSingleScalarResult();
return $types;
}
The error message:
[Semantical Error] line 0, col 9 near 'rules_id, e.status_id,': Error: Class Ethan\RestBundle\Entity\EthanRules has no field or association named rules_id"
I develop on Symfony 2 with FosrestBundle to create a REST Application
namespace Ethan\RestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EthanRules
*
* #ORM\Table(name="ethan_rules")
* #ORM\Entity
*/
class EthanRules
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="customer_key", type="string", length=45, nullable=false)
*/
private $customerKey;
/**
* #var string
*
* #ORM\Column(name="subscription_key", type="string", length=45, nullable=true)
*/
private $subscriptionKey;
/**
* #var string
*
* #ORM\Column(name="pole", type="string", length=45, nullable=false)
*/
private $pole;
/**
* #var string
*
* #ORM\Column(name="link_number", type="string", length=255, nullable=false)
*/
private $linkNumber;
/**
* #var string
*
* #ORM\Column(name="user", type="string", length=100, nullable=true)
*/
private $user;
/**
* #var \DateTime
*
* #ORM\Column(name="date_rdv", type="datetime", nullable=true)
*/
private $dateRdv;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
private $updatedAt;
/**
* #var \Rules
*
* #ORM\ManyToOne(targetEntity="Rules")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="rules_id", referencedColumnName="id")
* })
*/
private $rules;
/**
* #var \Status
*
* #ORM\ManyToOne(targetEntity="Status")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="status_id", referencedColumnName="id")
* })
*/
private $status;
public function _construct()
{
$this->createdAt = new \DateTime();
$this->createdAt->setTimestamp(time());
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set customerKey
*
* #param string $customerKey
* #return EthanRules
*/
public function setCustomerKey($customerKey)
{
$this->customerKey = $customerKey;
return $this;
}
/**
* Get customerKey
*
* #return string
*/
public function getCustomerKey()
{
return $this->customerKey;
}
/**
* Set subscriptionKey
*
* #param string $subscriptionKey
* #return EthanRules
*/
public function setSubscriptionKey($subscriptionKey)
{
$this->subscriptionKey = $subscriptionKey;
return $this;
}
/**
* Get subscriptionKey
*
* #return string
*/
public function getSubscriptionKey()
{
return $this->subscriptionKey;
}
/**
* Set pole
*
* #param string $pole
* #return EthanRules
*/
public function setPole($pole)
{
$this->pole = $pole;
return $this;
}
/**
* Get pole
*
* #return string
*/
public function getPole()
{
return $this->pole;
}
/**
* Set linkNumber
*
* #param string $linkNumber
* #return EthanRules
*/
public function setLinkNumber($linkNumber)
{
$this->linkNumber = $linkNumber;
return $this;
}
/**
* Get linkNumber
*
* #return string
*/
public function getLinkNumber()
{
return $this->linkNumber;
}
/**
* Set user
*
* #param string $user
* #return EthanRules
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return string
*/
public function getUser()
{
return $this->user;
}
/**
* Set dateRdv
*
* #param \DateTime $dateRdv
* #return EthanRules
*/
public function setDateRdv($dateRdv)
{
$this->dateRdv = $dateRdv;
return $this;
}
/**
* Get dateRdv
*
* #return \DateTime
*/
public function getDateRdv()
{
return $this->dateRdv;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return EthanRules
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return EthanRules
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set rules
*
* #param \Ethan\RestBundle\Entity\Rules $rules
* #return EthanRules
*/
public function setRules(\Ethan\RestBundle\Entity\Rules $rules = null)
{
$this->rules = $rules;
return $this;
}
/**
* Get rules
*
* #return \Ethan\RestBundle\Entity\Rules
*/
public function getRules()
{
return $this->rules;
}
/**
* Set status
*
* #param \Ethan\RestBundle\Entity\Status $status
* #return EthanRules
*/
public function setStatus(\Ethan\RestBundle\Entity\Status $status = null)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return \Ethan\RestBundle\Entity\Status
*/
public function getStatus()
{
return $this->status;
}
}
Since you have not defined a property $rules_id Doctrine can not select one. Instead join both entities and select their ids:
$types = $this->em
->getRepository('EthanRestBundle:EthanRules')
->createQueryBuilder('e')
->select('r.id AS rule_id, s.id AS status_id, COUNT(DISTINCT e.id)')
->leftJoin('e.rules', 'r')
->leftJoin('e.status', 's')
->groupBy('r.id, s.id')
->orderBy('r.id');
BUT: you use getSingleScalarResult() which will not work with multiple result rows!
Propably getArrayResult() will be best choice for you, since it looks like you don't want to work with objects.
Doctrine by default names all fields with camelCase in the entity classes but with_underscores in the Database.
You need to change your rules_id and status_id to rules and status accordingly.

Updating a field in a table from a join OneToOne Symfony2 / Doctrine 2

I have a problem with Doctrine2 in Symfony2 and two relationed entities.
I have a class Users
class Users
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=50)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=50)
*/
private $password;
/**
* #var boolean
*
* #ORM\Column(name="type", type="boolean")
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="first_name", type="string", length=100)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="last_name", type="string", length=100)
*/
private $lastName;
/**
* #var string
*
* #ORM\Column(name="tel", type="string", length=255)
*/
private $tel;
/**
* #var string
*
* #ORM\Column(name="url_img", type="string", length=255)
*/
private $urlImg;
/**
* #var string
*
* #ORM\Column(name="company", type="string", length=255)
*/
private $company;
/**
* #ORM\OneToOne(targetEntity="Providers", mappedBy="userId", cascade={"persist"})
*/
protected $provider;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 type
*
* #param boolean $type
* #return Users
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return boolean
*/
public function getType()
{
return $this->type;
}
/**
* Set idPrest
*
* #param integer $idPrest
* #return Users
*/
public function setIdPrest($idPrest)
{
$this->idPrest = $idPrest;
return $this;
}
/**
* Get idPrest
*
* #return integer
*/
public function getIdPrest()
{
return $this->idPrest;
}
/**
* Set idEmbaucher
*
* #param integer $idEmbaucher
* #return Users
*/
public function setIdEmbaucher($idEmbaucher)
{
$this->idEmbaucher = $idEmbaucher;
return $this;
}
/**
* Get idEmbaucher
*
* #return integer
*/
public function getIdEmbaucher()
{
return $this->idEmbaucher;
}
/**
* Set email
*
* #param string $email
* #return Users
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set firstName
*
* #param string $firstName
* #return Users
*/
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 Users
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* #return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set tel
*
* #param string $tel
* #return Users
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel()
{
return $this->tel;
}
/**
* Set urlImg
*
* #param string $urlImg
* #return Users
*/
public function setUrlImg($urlImg)
{
$this->urlImg = $urlImg;
return $this;
}
/**
* Get urlImg
*
* #return string
*/
public function getUrlImg()
{
return $this->urlImg;
}
/**
* Set company
*
* #param string $company
* #return Users
*/
public function setCompany($company)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* #return string
*/
public function getCompany()
{
return $this->company;
}
/**
* Set provider
*
* #param Providers $provider
* #return Users
*/
public function setProvider($provider = null)
{
$this->provider = $provider;
return $this;
}
/**
* Get provider
*
* #return Providers
*/
public function getProvider()
{
return $this->provider;
}
}
in relation with class provider on OneToOne (inheriting)
class Providers
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Users", inversedBy="provider")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $userId;
/**
* #ORM\OneToOne(targetEntity="Advertisement", mappedBy="provider")
*/
protected $advertisement;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* #param \leBonCoinDesServices\MainBundle\Entity\Users $userId
* #return Providers
*/
public function setUserId($userId = null)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* #return \leBonCoinDesServices\MainBundle\Entity\Users
*/
public function getUserId()
{
return $this->userId;
}
}
I just want to get the fields user_id created from the relationship for be able to when I create a user get the id of this one and give it to the user_id field.
And when I test my controller to create a user and a provider :
public function newUserAction()
{
$user = new Users();
$user->setUsername("gokusan");
$user->setPassword("test");
$user->setType("p");
$user->setEmail("test#msn.com");
$user->setFirstName("KHALIL");
$user->setLastName("Ahmed");
$user->setTel("0142021148");
$user->setUrlImg("img/test/");
$user->setCompany("Push&Pull");
$this->getDoctrine()->getEntityManager()->persist($user);
$this->getDoctrine()->getEntityManager()->flush();
$provider = new Providers();
$id = $user->getId();
$provider->setUserId($id);
$this->getDoctrine()->getEntityManager()->persist($provider);
$this->getDoctrine()->getEntityManager()->flush();
die("User Added");
}
that puts me error :
Warning: spl_object_hash() expects parameter 1 to be object, integer given in F:\Programs Files\Program Files\wamp\www\leboncoindesservices\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 1367
Your Providers::setUserId($userId) expects $userId to be of type \leBonCoinDesServices\MainBundle\Entity\Users and not of type integer. So you should be able to pass your Users object to the $provider like
$user = new Users();
...
$provder = new Providers();
...
$provider->setUserId($user);

Categories