Symfony2 - PUGXMultiUserBundle - Cannot instantiate abstract class - symfony

I tried to install PUGXMultiUserBundle, now as I did all steps I tried to load the application (only the main location: http://localhost/jpp/web/app_dev.php ), but I always get the following error:
Fatal error: Cannot instantiate abstract class JPP\UserBundle\Entity\User in C:\xampp\htdocs\JPP\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Token\AbstractToken.php on line 155
I tried several things without success, please could you help me? Below you can find my Entity classes. I was able to create all the tables in the database with GIT. The rentity classes are located under: JPP/UserBundle/Entity. If you need more files please tell me... Thank you very much!!!
Roger
Abstract User Class:
<?php
namespace JPP\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="user")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"userprofile" = "UserProfile", "usercompany" = "UserCompany"})
*
*/
abstract class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
Class UserCompany
<?php
// src/jpp/UserBundle/Entity/User.php
namespace JPP\UserBundle\Entity;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="userCompany")
* #UniqueEntity(fields = "username", targetClass = "JPP\UserBundle\Entity\User", message="fos_user.username.already_used")
* #UniqueEntity(fields = "email", targetClass = "JPP\UserBundle\Entity\User", message="fos_user.email.already_used")
*/
class UserCompany extends User
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer
*
* #ORM\Column(name="usertype", type="integer")
*/
protected $userType;
/**
* #var string
*
* #ORM\Column(name="companyName", type="string", length=255)
*/
protected $companyName;
/**
* #var string
*
* #ORM\Column(name="sector", type="string", length=255)
*/
protected $sector;
/**
* #var string
*
* #ORM\Column(name="amountOfEmployees", type="string", length=255)
*/
protected $amountOfEmployees;
/**
* #var string
*
* #ORM\Column(name="turnover", type="string", length=255)
*/
protected $turnover;
/**
* #var string
*
* #ORM\Column(name="companyLink", type="string", length=255)
*/
protected $companyLink;
/**
* #var string
*
* #ORM\Column(name="facebookLink", type="string", length=255)
*/
protected $facebookLink;
/**
* #var string
*
* #ORM\Column(name="twitterLink", type="string", length=255)
*/
protected $twitterLink;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userType
*
* #param integer $userType
* #return UserType
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return integer
*/
public function getUserType()
{
return $this->userType;
}
/**
* Set companyName
*
* #param string $companyName
* #return Company
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
return $this;
}
/**
* Get companyName
*
* #return string
*/
public function getCompanyName()
{
return $this->companyName;
}
/**
* Set sector
*
* #param string $sector
* #return Company
*/
public function setSector($sector)
{
$this->sector = $sector;
return $this;
}
/**
* Get sector
*
* #return string
*/
public function getSector()
{
return $this->sector;
}
/**
* Set amountOfEmployees
*
* #param string $amountOfEmployees
* #return Company
*/
public function setAmountOfEmployees($amountOfEmployees)
{
$this->amountOfEmployees = $amountOfEmployees;
return $this;
}
/**
* Get amountOfEmployees
*
* #return string
*/
public function getAmountOfEmployees()
{
return $this->amountOfEmployees;
}
/**
* Set turnover
*
* #param string $turnover
* #return Company
*/
public function setTurnover($turnover)
{
$this->turnover = $turnover;
return $this;
}
/**
* Get turnover
*
* #return string
*/
public function getTurnover()
{
return $this->turnover;
}
/**
* Set companyLink
*
* #param string $companyLink
* #return Company
*/
public function setCompanyLink($companyLink)
{
$this->companyLink = $companyLink;
return $this;
}
/**
* Get companyLink
*
* #return string
*/
public function getCompanyLink()
{
return $this->companyLink;
}
/**
* Set facebookLink
*
* #param string $facebookLink
* #return Company
*/
public function setFacebookLink($facebookLink)
{
$this->facebookLink = $facebookLink;
return $this;
}
/**
* Get facebookLink
*
* #return string
*/
public function getFacebookLink()
{
return $this->facebookLink;
}
/**
* Set twitterLink
*
* #param string $twitterLink
* #return Company
*/
public function setTwitterLink($twitterLink)
{
$this->twitterLink = $twitterLink;
return $this;
}
/**
* Get twitterLink
*
* #return string
*/
public function getTwitterLink()
{
return $this->twitterLink;
}
}
Class UserProfile
// src/jpp/UserBundle/Entity/User.php
namespace JPP\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="userProfile")
* #UniqueEntity(fields = "username", targetClass = "JPP\UserBundle\Entity\User", message="fos_user.username.already_used")
* #UniqueEntity(fields = "email", targetClass = "JPP\UserBundle\Entity\User", message="fos_user.email.already_used")
*/
class UserProfile extends User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer
*
* #ORM\Column(name="usertype", type="integer")
*/
protected $userType;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $foreName;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $surName;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $street;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $plz;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $place;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $phone;
/**
* #ORM\Column(type="string", length=255)
*
*/
protected $mobile;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set userType
*
* #param integer $userType
* #return UserType
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return integer
*/
public function getUserType()
{
return $this->userType;
}
/**
* Set foreName
*
* #param string $foreName
* #return ForeName
*/
public function setForeName($foreName)
{
$this->foreName = $foreName;
return $this;
}
/**
* Get foreName
*
* #return string
*/
public function getForeName()
{
return $this->foreName;
}
/**
* Set surName
*
* #param string $surName
* #return SurName
*/
public function setSurName($surName)
{
$this->surName = $surName;
return $this;
}
/**
* Get surName
*
* #return string
*/
public function getSurName()
{
return $this->surName;
}
/**
* Set street
*
* #param string $street
* #return Street
*/
public function setStreet($street)
{
$this->street = $street;
return $this;
}
/**
* Get street
*
* #return string
*/
public function getStreet()
{
return $this->street;
}
/**
* Set plz
*
* #param string $plz
* #return PLZ
*/
public function setPlz($plz)
{
$this->plz = $plz;
return $this;
}
/**
* Get plz
*
* #return string
*/
public function getPlz()
{
return $this->plz;
}
/**
* Set place
*
* #param string $place
* #return Place
*/
public function setPlace($place)
{
$this->place = $place;
return $this;
}
/**
* Get place
*
* #return string
*/
public function getPlace()
{
return $this->place;
}
/**
* Set phone
*
* #param string $phone
* #return Phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set mobile
*
* #param string $mobile
* #return Mobile
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile
*
* #return string
*/
public function getMobile()
{
return $this->mobile;
}
//public function __construct()
//{
// parent::__construct();
// your own logic
//}
}

The error says:
Fatal error: Cannot instantiate abstract class JPP\UserBundle\Entity\User in C:\xampp\htdocs\JPP\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Token\AbstractToken.php on line 155
It means that Symfony tries to instantiante your User class (e.g. new User()) but fails because this class is abstract. So, just remove abstract definition.
Replace
abstract class User extends BaseUser
with
class User extends BaseUser

Related

"Missing value for primary key"

I'd just migrated to Symfony 3.4 and had this kind of message :
request.CRITICAL: Uncaught PHP Exception Doctrine\Common\Proxy\Exception\OutOfBoundsException: "Missing value for primary key idBen on Lea\PrestaBundle\Entity\EgwContact" at /htdocs/vendor/doctrine/common/lib/Doctrine/Common/Proxy/Exception/OutOfBoundsException.php line 40
Thanks in advance for your help
Here is my entity code for egw_contact :
<?php
namespace Lea\PrestaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Lea\PrestaBundle\Entity\EgwContact
*
* #ORM\Table(name="egw_contact")
* #ORM\Entity(repositoryClass="Lea\PrestaBundle\Entity\EgwContactRepository")
*/
class EgwContact
{
/**
* #var integer $idBen
*
* #ORM\Column(name="id_ben", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $idBen;
/**
* #var string $idOrganisation
*
* #ORM\Column(name="id_organisation", type="string", length=64, nullable=true)
*/
private $idOrganisation;
/**
* #var integer $idOwner
*
* #ORM\Column(name="id_owner", type="bigint", nullable=false)
*/
private $idOwner;
/**
* #var integer $dateCreation
*
* #ORM\Column(name="date_creation", type="bigint", nullable=false)
*/
private $dateCreation;
/**
* #var integer $idModifier
*
* #ORM\Column(name="id_modifier", type="bigint", nullable=false)
*/
private $idModifier;
/**
* #var integer $dateLastModified
*
* #ORM\Column(name="date_last_modified", type="bigint", nullable=false)
*/
private $dateLastModified;
/**
* #var string $catId
*
* #ORM\Column(name="cat_id", type="string", length=32, nullable=false)
*/
private $catId;
/**
* #var string $civilite
*
* #ORM\Column(name="civilite", type="string", length=64, nullable=false)
*/
private $civilite;
/**
* #var string $nomComplet
*
* #ORM\Column(name="nom_complet", type="string", length=64, nullable=false)
*/
private $nomComplet;
/**
* #var string $nom
*
* #ORM\Column(name="nom", type="string", length=64, nullable=false)
*/
private $nom;
/**
* #var string $prenom
*
* #ORM\Column(name="prenom", type="string", length=64, nullable=false)
*/
private $prenom;
/**
* #var string $deuxiemePrenom
*
* #ORM\Column(name="deuxieme_prenom", type="string", length=64, nullable=false)
*/
private $deuxiemePrenom;
/**
* #var string $nomJeuneFille
*
* #ORM\Column(name="nom_jeune_fille", type="string", length=64, nullable=false)
*/
private $nomJeuneFille;
/**
* #var string $organisation
*
* #ORM\Column(name="organisation", type="string", length=64, nullable=false)
*/
private $organisation;
/**
* #var string $fonction
*
* #ORM\Column(name="fonction", type="string", length=64, nullable=false)
*/
private $fonction;
/**
* #var string $service
*
* #ORM\Column(name="Service", type="string", length=64, nullable=false)
*/
private $service;
/**
* #var string $adresseLigne1
*
* #ORM\Column(name="adresse_ligne_1", type="string", length=64, nullable=false)
*/
private $adresseLigne1;
/**
* #var string $adresseLigne2
*
* #ORM\Column(name="adresse_ligne_2", type="string", length=64, nullable=false)
*/
private $adresseLigne2;
/**
* #var string $adresseLigne3
*
* #ORM\Column(name="adresse_ligne_3", type="string", length=64, nullable=false)
*/
private $adresseLigne3;
/**
* #var string $ville
*
* #ORM\Column(name="ville", type="string", length=64, nullable=false)
*/
private $ville;
/**
* #var string $region
*
* #ORM\Column(name="region", type="string", length=64, nullable=false)
*/
private $region;
/**
* #var string $cp
*
* #ORM\Column(name="cp", type="string", length=64, nullable=false)
*/
private $cp;
/**
* #var string $pays
*
* #ORM\Column(name="pays", type="string", length=64, nullable=false)
*/
private $pays;
/**
* #var string $telPro1
*
* #ORM\Column(name="tel_pro_1", type="string", length=40, nullable=false)
*/
private $telPro1;
/**
* #var string $telPro2
*
* #ORM\Column(name="tel_pro_2", type="string", length=64, nullable=false)
*/
private $telPro2;
/**
* #var string $telDomicile1
*
* #ORM\Column(name="tel_domicile_1", type="string", length=14, nullable=false)
*/
private $telDomicile1;
/**
* #var string $telDomicile2
*
* #ORM\Column(name="tel_domicile_2", type="string", length=64, nullable=false)
*/
private $telDomicile2;
/**
* #var string $faxPro
*
* #ORM\Column(name="fax_pro", type="string", length=40, nullable=false)
*/
private $faxPro;
/**
* #var string $faxPerso
*
* #ORM\Column(name="fax_perso", type="string", length=64, nullable=false)
*/
private $faxPerso;
/**
* #var string $portablePro
*
* #ORM\Column(name="portable_pro", type="string", length=64, nullable=false)
*/
private $portablePro;
/**
* #var string $portablePerso
*
* #ORM\Column(name="portable_perso", type="string", length=40, nullable=false)
*/
private $portablePerso;
/**
* #var string $emailPro
*
* #ORM\Column(name="email_pro", type="string", length=64, nullable=false)
*/
private $emailPro;
/**
* #var string $emailPerso
*
* #ORM\Column(name="email_perso", type="string", length=64, nullable=false)
*/
private $emailPerso;
/**
* #var string $sitePerso
*
* #ORM\Column(name="site_perso", type="string", length=64, nullable=false)
*/
private $sitePerso;
/**
* #var string $dateNaissance
*
* #ORM\Column(name="date_naissance", type="string", length=64, nullable=false)
*/
private $dateNaissance;
/**
* #var string $lieuNaissance
*
* #ORM\Column(name="lieu_naissance", type="string", length=64, nullable=false)
*/
private $lieuNaissance;
/**
* #var string $paysNaissance
*
* #ORM\Column(name="pays_naissance", type="string", length=64, nullable=false)
*/
private $paysNaissance;
/**
* #var string $nationalite
*
* #ORM\Column(name="nationalite", type="string", length=64, nullable=false)
*/
private $nationalite;
/**
* #var string $situationMaritale
*
* #ORM\Column(name="situation_maritale", type="string", length=64, nullable=false)
*/
private $situationMaritale;
/**
* #var integer $enfantsACharge
*
* #ORM\Column(name="enfants_a_charge", type="integer", nullable=false)
*/
private $enfantsACharge;
/**
* #var integer $idSecu
*
* #ORM\Column(name="numero_SS", type="string", nullable=false)
*/
private $idSecu;
/**
* #ORM\OneToMany(targetEntity="EgwCategories", mappedBy="contactCategorie")
*/
private $typeContact;
/**
* #ORM\OneToMany(targetEntity="Relance", mappedBy="contact")
*
*/
protected $relances;
/**
* #ORM\OneToMany(targetEntity="EgwProjet", mappedBy="contact")
*/
protected $projets;
/**
* #ORM\OneToMany(targetEntity="EgwPrestation", mappedBy="contact")
*/
protected $prestations;
/**
* #ORM\OneToMany(targetEntity="EgwPrestation", mappedBy="contactP")
*/
protected $prestationsP;
/**
* #ORM\OneToMany(targetEntity="EgwContactParcoursPro", mappedBy="parcoursProContact")
*/
protected $contactParcoursPro;
/**
* #ORM\OneToMany(targetEntity="EgwContactFormation", mappedBy="formationContact")
*/
protected $contactFormation;
public function __construct()
{
$this->prestations = new ArrayCollection();
$this->prestationsP = new ArrayCollection();
$this->contactParcoursPro = new ArrayCollection();
$this->contactFormation = new ArrayCollection();
$this->typeContact = new ArrayCollection();
$this->projets = new ArrayCollection();
}
/**
* Get idBen
*
* #return integer
*/
public function getIdBen()
{
return $this->idBen;
}
/**
* Set idOrganisation
*
* #param string $idOrganisation
* #return EgwContact
*/
public function setIdOrganisation($idOrganisation = null)
{
$this->idOrganisation = $idOrganisation;
return $this;
}
/**
* Get idOrganisation
*
* #return string
*/
public function getIdOrganisation()
{
return $this->idOrganisation;
}
/**
* Set idOwner
*
* #param integer $idOwner
* #return EgwContact
*/
public function setIdOwner($idOwner)
{
$this->idOwner = $idOwner;
return $this;
}
/**
* Get idOwner
*
* #return integer
*/
public function getIdOwner()
{
return $this->idOwner;
}
/**
* Set dateCreation
*
* #param integer $dateCreation
* #return EgwContact
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return integer
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set idModifier
*
* #param integer $idModifier
* #return EgwContact
*/
public function setIdModifier($idModifier)
{
$this->idModifier = $idModifier;
return $this;
}
/**
* Get idModifier
*
* #return integer
*/
public function getIdModifier()
{
return $this->idModifier;
}
/**
* Set dateLastModified
*
* #param integer $dateLastModified
* #return EgwContact
*/
public function setDateLastModified($dateLastModified)
{
$this->dateLastModified = $dateLastModified;
return $this;
}
/**
* Get dateLastModified
*
* #return integer
*/
public function getDateLastModified()
{
return $this->dateLastModified;
}
/**
* Set catId
*
* #param string $catId
* #return EgwContact
*/
public function setCatId($catId)
{
$this->catId = $catId;
return $this;
}
/**
* Get catId
*
* #return string
*/
public function getCatId()
{
return $this->catId;
}
/**
* Set civilite
*
* #param string $civilite
* #return EgwContact
*/
public function setCivilite($civilite)
{
$this->civilite = $civilite;
return $this;
}
/**
* Get civilite
*
* #return string
*/
public function getCivilite()
{
return $this->civilite;
}
/**
* Set nomComplet
*
* #param string $nomComplet
* #return EgwContact
*/
public function setNomComplet($nomComplet)
{
$this->nomComplet = $nomComplet;
return $this;
}
/**
* Get nomComplet
*
* #return string
*/
public function getNomComplet()
{
return $this->nomComplet;
}
/**
* Set nom
*
* #param string $nom
* #return EgwContact
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* #return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set prenom
*
* #param string $prenom
* #return EgwContact
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
return $this;
}
/**
* Get prenom
*
* #return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set deuxiemePrenom
*
* #param string $deuxiemePrenom
* #return EgwContact
*/
public function setDeuxiemePrenom($deuxiemePrenom)
{
$this->deuxiemePrenom = $deuxiemePrenom;
return $this;
}
/**
* Get deuxiemePrenom
*
* #return string
*/
public function getDeuxiemePrenom()
{
return $this->deuxiemePrenom;
}
/**
* Set nomJeuneFille
*
* #param string $nomJeuneFille
* #return EgwContact
*/
public function setNomJeuneFille($nomJeuneFille)
{
$this->nomJeuneFille = $nomJeuneFille;
return $this;
}
/**
* Get nomJeuneFille
*
* #return string
*/
public function getNomJeuneFille()
{
return $this->nomJeuneFille;
}
/**
* Set organisation
*
* #param string $organisation
* #return EgwContact
*/
public function setOrganisation($organisation)
{
$this->organisation = $organisation;
return $this;
}
/**
* Get organisation
*
* #return string
*/
public function getOrganisation()
{
return $this->organisation;
}
/**
* Set fonction
*
* #param string $fonction
* #return EgwContact
*/
public function setFonction($fonction)
{
$this->fonction = $fonction;
return $this;
}
/**
* Get fonction
*
* #return string
*/
public function getFonction()
{
return $this->fonction;
}
/**
* Set service
*
* #param string $service
* #return EgwContact
*/
public function setService($service)
{
$this->service = $service;
return $this;
}
/**
* Get service
*
* #return string
*/
public function getService()
{
return $this->service;
}
/**
* Set adresseLigne1
*
* #param string $adresseLigne1
* #return EgwContact
*/
public function setAdresseLigne1($adresseLigne1)
{
$this->adresseLigne1 = $adresseLigne1;
return $this;
}
/**
* Get adresseLigne1
*
* #return string
*/
public function getAdresseLigne1()
{
return $this->adresseLigne1;
}
/**
* Set adresseLigne2
*
* #param string $adresseLigne2
* #return EgwContact
*/
public function setAdresseLigne2($adresseLigne2)
{
$this->adresseLigne2 = $adresseLigne2;
return $this;
}
/**
* Get adresseLigne2
*
* #return string
*/
public function getAdresseLigne2()
{
return $this->adresseLigne2;
}
/**
* Set adresseLigne3
*
* #param string $adresseLigne3
* #return EgwContact
*/
public function setAdresseLigne3($adresseLigne3)
{
$this->adresseLigne3 = $adresseLigne3;
return $this;
}
/**
* Get adresseLigne3
*
* #return string
*/
public function getAdresseLigne3()
{
return $this->adresseLigne3;
}
/**
* Set ville
*
* #param string $ville
* #return EgwContact
*/
public function setVille($ville)
{
$this->ville = $ville;
return $this;
}
/**
* Get ville
*
* #return string
*/
public function getVille()
{
return $this->ville;
}
/**
* Set region
*
* #param string $region
* #return EgwContact
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* Get region
*
* #return string
*/
public function getRegion()
{
return $this->region;
}
/**
* Set cp
*
* #param string $cp
* #return EgwContact
*/
public function setCp($cp)
{
$this->cp = $cp;
return $this;
}
/**
* Get cp
*
* #return string
*/
public function getCp()
{
return $this->cp;
}
/**
* Set pays
*
* #param string $pays
* #return EgwContact
*/
public function setPays($pays)
{
$this->pays = $pays;
return $this;
}
/**
* Get pays
*
* #return string
*/
public function getPays()
{
return $this->pays;
}
/**
* Set telPro1
*
* #param string $telPro1
* #return EgwContact
*/
public function setTelPro1($telPro1)
{
$this->telPro1 = $telPro1;
return $this;
}
/**
* Get telPro1
*
* #return string
*/
public function getTelPro1()
{
return $this->telPro1;
}
/**
* Set telPro2
*
* #param string $telPro2
* #return EgwContact
*/
public function setTelPro2($telPro2)
{
$this->telPro2 = $telPro2;
return $this;
}
/**
* Get telPro2
*
* #return string
*/
public function getTelPro2()
{
return $this->telPro2;
}
/**
* Set telDomicile1
*
* #param string $telDomicile1
* #return EgwContact
*/
public function setTelDomicile1($telDomicile1)
{
$this->telDomicile1 = $telDomicile1;
return $this;
}
/**
* Get telDomicile1
*
* #return string
*/
public function getTelDomicile1()
{
return $this->telDomicile1;
}
/**
* Set telDomicile2
*
* #param string $telDomicile2
* #return EgwContact
*/
public function setTelDomicile2($telDomicile2)
{
$this->telDomicile2 = $telDomicile2;
return $this;
}
/**
* Get telDomicile2
*
* #return string
*/
public function getTelDomicile2()
{
return $this->telDomicile2;
}
/**
* Set faxPro
*
* #param string $faxPro
* #return EgwContact
*/
public function setFaxPro($faxPro)
{
$this->faxPro = $faxPro;
return $this;
}
/**
* Get faxPro
*
* #return string
*/
public function getFaxPro()
{
return $this->faxPro;
}
/**
* Set faxPerso
*
* #param string $faxPerso
* #return EgwContact
*/
public function setFaxPerso($faxPerso)
{
$this->faxPerso = $faxPerso;
return $this;
}
/**
* Get faxPerso
*
* #return string
*/
public function getFaxPerso()
{
return $this->faxPerso;
}
/**
* Set portablePro
*
* #param string $portablePro
* #return EgwContact
*/
public function setPortablePro($portablePro)
{
$this->portablePro = $portablePro;
return $this;
}
/**
* Get portablePro
*
* #return string
*/
public function getPortablePro()
{
return $this->portablePro;
}
/**
* Set portablePerso
*
* #param string $portablePerso
* #return EgwContact
*/
public function setPortablePerso($portablePerso)
{
$this->portablePerso = $portablePerso;
return $this;
}
/**
* Get portablePerso
*
* #return string
*/
public function getPortablePerso()
{
return $this->portablePerso;
}
/**
* Set emailPro
*
* #param string $emailPro
* #return EgwContact
*/
public function setEmailPro($emailPro)
{
$this->emailPro = $emailPro;
return $this;
}
/**
* Get emailPro
*
* #return string
*/
public function getEmailPro()
{
return $this->emailPro;
}
/**
* Set emailPerso
*
* #param string $emailPerso
* #return EgwContact
*/
public function setEmailPerso($emailPerso)
{
$this->emailPerso = $emailPerso;
return $this;
}
/**
* Get emailPerso
*
* #return string
*/
public function getEmailPerso()
{
return $this->emailPerso;
}
/**
* Set sitePerso
*
* #param string $sitePerso
* #return EgwContact
*/
public function setSitePerso($sitePerso)
{
$this->sitePerso = $sitePerso;
return $this;
}
/**
* Get sitePerso
*
* #return string
*/
public function getSitePerso()
{
return $this->sitePerso;
}
/**
* Set dateNaissance
*
* #param string $dateNaissance
* #return EgwContact
*/
public function setDateNaissance($dateNaissance)
{
$this->dateNaissance = $dateNaissance;
return $this;
}
/**
* Get dateNaissance
*
* #return string
*/
public function getDateNaissance()
{
return $this->dateNaissance;
}
/**
* Set lieuNaissance
*
* #param string $lieuNaissance
* #return EgwContact
*/
public function setLieuNaissance($lieuNaissance)
{
$this->lieuNaissance = $lieuNaissance;
return $this;
}
/**
* Get lieuNaissance
*
* #return string
*/
public function getLieuNaissance()
{
return $this->lieuNaissance;
}
/**
* Set paysNaissance
*
* #param string $paysNaissance
* #return EgwContact
*/
public function setPaysNaissance($paysNaissance)
{
$this->paysNaissance = $paysNaissance;
return $this;
}
/**
* Get paysNaissance
*
* #return string
*/
public function getPaysNaissance()
{
return $this->paysNaissance;
}
/**
* Set nationalite
*
* #param string $nationalite
* #return EgwContact
*/
public function setNationalite($nationalite)
{
$this->nationalite = $nationalite;
return $this;
}
/**
* Get nationalite
*
* #return string
*/
public function getNationalite()
{
return $this->nationalite;
}
/**
* Set situationMaritale
*
* #param string $situationMaritale
* #return EgwContact
*/
public function setSituationMaritale($situationMaritale)
{
$this->situationMaritale = $situationMaritale;
return $this;
}
/**
* Get situationMaritale
*
* #return string
*/
public function getSituationMaritale()
{
return $this->situationMaritale;
}
/**
* Set enfantsACharge
*
* #param integer $enfantsACharge
* #return EgwContact
*/
public function setEnfantsACharge($enfantsACharge)
{
$this->enfantsACharge = $enfantsACharge;
return $this;
}
/**
* Get enfantsACharge
*
* #return integer
*/
public function getEnfantsACharge()
{
return $this->enfantsACharge;
}
/**
* Set idSecu
*
* #param integer $idSecu
* #return EgwContact
*/
public function setIdSecu($idSecu)
{
$this->idSecu = $idSecu;
return $this;
}
/**
* Get idSecu
*
* #return string
*/
public function getidSecu()
{
return $this->idSecu;
}
/**
* Add typeContact
*
* #param Lea\PrestaBundle\Entity\EgwContact $typeContact
* #return EgwContact
etc...
...
}
I FOUND ! in a Form calling the entity Ewg_contact linked to the entity Ewg_Categories, there was a hard parameter called: I replaced it with a variable. And it works.
This is by looking at the getProxy () function of the Doctrine class AbstractProxyFactory.php: there is an exception that calls the OutOfBoundsException :: missingPrimaryKeyValue function in which there is the message "
Missing value for primary key
Here is the correction made to the Form in question:
$param287=287;
$builder->add('categorie', EntityType::class, array(
'class' => 'LeaPrestaBundle:EgwCategories',
'label' => 'catName',
'required' => true,
'empty_data' => null,
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('c')
->where('c.catParent = :parent')
->setParameter('parent',$param287 )
->orderBy('c.catName','ASC');
},
));
EXPLANATION : Before it was :
->setParameter('parent', 287 )
witch provocated an exception in the getproxy function of AbstractProxyFactory.php class
Correct now
Thanks for your messages
i have the same problem. I found a possible "solution" in Doctrine Documentation. This one maybe can help.
i see in Documentation it's not possible to use not-primary key for joins columns.
Your column id_ben is a primary key ?
It is not possible to use join columns pointing to non-primary keys. Doctrine will think these are the primary keys and create lazy-loading proxies with the data, which can lead to unexpected results. Doctrine can for performance reasons not validate the correctness of this settings at runtime but only through the Validate Schema command.
It's probably a problem between 2 entities 👍
The link between entities egw_accounts and egw_addressbook is : ACCOUNT_ID
In the database, about the field ACCOUNT_ID there are exactly the same values in entities egw_accounts and egw_addressbook 👍
egw_accounts->ACCOUNT_ID = egw_addressbook->ACCOUNT_ID
SO, perhaps it's in the definition of entities that there's a problem
Remember that, i didnt have the problem in SYMFONY 2 : but in SYMFONY 3.4, the problem appears
THANKS for your help!
ENTITY EGW_ACCOUNTS 👍
class EgwAccounts { /** * #var integer $accountId * * #ORM\Id * #ORM\GeneratedValue(strategy="IDENTITY") * #ORM\OneToOne(targetEntity="EgwAddressbook", cascade={"persist", "merge", "remove"}) * #ORM\JoinColumn(name="account_id", referencedColumnName="account_id") */ private $accountId;
With the SETTER :
`
/**
* Set accountId
*
* #param Lea\PrestaBundle\Entity\EgwAddressbook $accountId
* #return EgwAccounts
*/
public function setAccountId(\Lea\PrestaBundle\Entity\EgwAddressbook $accountId)
{
$this->accountId = $accountId;
return $this;
}`
THE ENTITY EGW_ADDRESSBOOK 👍
/** * #var integer $accountId * #ORM\Column(name="account_id", type="integer", nullable=true) */ private $accountId;

The associationrefers to the owning side field e which is not defined as association, but as field

I'm making a blog project. So I have an article Class and a Commentaire Blog class.
But I'm having an error when I want to link boths :
[Mapping] FAIL - The entity-class 'AppBundle\Entity\Article' mapping
is invalid:
* The association AppBundle\Entity\Article#commentaires refers to the owning side field AppBundle\Entity\Commentaire_blog#message which is
not defined as association, but as field.
* The association AppBundle\Entity\Article#commentaires refers to the owning side field AppBundle\Entity\Commentaire_blog#message which does
not exist.
[Mapping] FAIL - The entity-class 'AppBundle\Entity\Commentaire_blog'
mapping is invalid:
* The mappings AppBundle\Entity\Commentaire_blog#article and AppBundle\Entity\Article#commentaires are inconsistent with each
other.
I'm a bit lost...
Here are my both Class :
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
*/
class Article
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="text")
*/
private $titre;
/**
* #var string
*
* #ORM\Column(name="article", type="text")
*/
private $article;
/**
* #var \DateTime
*
* #ORM\Column(name="date_article", type="datetime")
*/
private $dateArticle;
/**
* #ORM\OneToMany(targetEntity="Commentaire_blog", mappedBy="messa")
*/
private $commentaires;
public function __construct()
{
$this->commentaires = new ArrayCollection();
}
/**
* #return Collection|Commentaire_blog[]
*/
public function getCommentaires()
{
return $this->commentaires;
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Article
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set article
*
* #param string $article
*
* #return Article
*/
public function setArticle($article)
{
$this->article = $article;
return $this;
}
/**
* Get article
*
* #return string
*/
public function getArticle()
{
return $this->article;
}
/**
* Set dateArticle
*
* #param \DateTime $dateArticle
*
* #return Article
*/
public function setDateArticle($dateArticle)
{
$this->dateArticle = $dateArticle;
return $this;
}
/**
* Get dateArticle
*
* #return \DateTime
*/
public function getDateArticle()
{
return $this->dateArticle;
}
And The second :
* Commentaire_blog
*
* #ORM\Table(name="commentaire_blog")
* #ORM\Entity(repositoryClass="AppBundle\Repository \Commentaire_blogRepository")
*/
class Commentaire_blog
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="usernamne", type="string", length=255)
*/
private $usernamne;
/**
* #var string
*
* #ORM\Column(name="message", type="text")
*/
private $message;
/**
* #var bool
*
* #ORM\Column(name="is_visible", type="boolean")
*/
private $isVisible;
/**
* #return mixed
*/
public function getArticle()
{
return $this->article;
}
/**
* #param mixed $article
*/
public function setArticle($article)
{
$this->article = $article;
}
/**
* #ORM\ManyToOne(targetEntity="Article", inversedBy="commentaires")
* #ORM\JoinColumn(nullable=true)
*/
private $article;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set usernamne
*
* #param string $usernamne
*
* #return Commentaire_blog
*/
public function setUsernamne($usernamne)
{
$this->usernamne = $usernamne;
return $this;
}
/**
* Get usernamne
*
* #return string
*/
public function getUsernamne()
{
return $this->usernamne;
}
/**
* Set message
*
* #param string $message
*
* #return Commentaire_blog
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Get message
*
* #return string
*/
public function getMessage()
{
return $this->message;
}
/**
* Set isVisible
*
* #param boolean $isVisible
*
* #return Commentaire_blog
*/
public function setIsVisible($isVisible)
{
$this->isVisible = $isVisible;
return $this;
}
/**
* Get isVisible
*
* #return bool
*/
public function getIsVisible()
{
return $this->isVisible;
}
}
Any Idea how to make the link correct...
Try this:
Class Article:
/**
* #ORM\OneToMany(targetEntity="Commentaire_blog", mappedBy="article")
*/
private $commentaires;
Class commentaire_blog:
/**
* #ORM\ManyToOne(targetEntity="Article", inversedBy="commentaires")
* #ORM\JoinColumn(nullable=true)
*/
private $article;
Also, consider converting the getCommentaires result to Array:
public function getCommentaires()
{
return $this->commentaires->toArray();
}
To understand more about the owning and inverse side of each relation try reading this: Associations: Owning and Inverse Side.

count an attribute in an entity

I have an entity "user" that has OneToMany relation with the entity "vehicule". I'm trying to count the number of vehicules for each user. This is my function
$emm = $this->getDoctrine();
$directions = $emm->getRepository('OCUserBundle:User')->findAll();
foreach($directions as $direction) {
$direction->getVehicule()->count();
}
return $this->render('DemandevehBundle:Demande:affiche.html.twig', array('demandes' => $demandes
, ));
but how could I put it in the return so that i could use it in my affiche.html.twig. because I want to show foreach user the number of vehicules he have . Thanks a lot
This is my entity Vehicule
<?php
namespace Car\PfeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use OC\UserBundle\Entity\User;
/**
* Vehicule
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Car\PfeBundle\Entity\VehiculeRepository")
*/
class Vehicule
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="carte_grise", type="integer", unique=true)
*/
private $carteGrise;
/**
* #var string
*
* #ORM\Column(name="modele", type="string", length=255)
*/
private $modele;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="categorie", type="string", length=255)
*/
private $categorie;
/**
* #var integer
*
* #ORM\Column(name="puissance", type="integer")
*
*/
private $puissance;
/**
* #var integer
*
* #ORM\Column(name="nb_place", type="integer")
*/
private $nbPlace;
/**
* #var integer
*
* #ORM\Column(name="kilometrage", type="integer")
*/
private $kilometrage;
/**
* #var string
*
* #ORM\Column(name="marque", type="string", length=255)
*/
private $marque;
/**
* #var string
*
* #ORM\Column(name="carburant", type="string", length=255)
*/
private $carburant;
/**
* #var string
*
* #ORM\Column(name="transmission", type="string", length=255)
*/
private $transmission;
/**
* #ORM\ManyToOne(targetEntity="OC\UserBundle\Entity\User", inversedBy="vehicule")
* #ORM\JoinColumn(name="User_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $direction;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set Id
*
* #param integer $carteGrise
* #return Vehicule
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Set carteGrise
*
* #param integer $carteGrise
* #return Vehicule
*/
public function setCarteGrise($carteGrise)
{
$this->carteGrise = $carteGrise;
return $this;
}
/**
* Get carteGrise
*
* #return integer
*/
public function getCarteGrise()
{
return $this->carteGrise;
}
/**
* Set modele
*
* #param string $modele
* #return Vehicule
*/
public function setModele($modele)
{
$this->modele = $modele;
return $this;
}
/**
* Get modele
*
* #return string
*/
public function getModele()
{
return $this->modele;
}
/**
* Set categorie
*
* #param string $categorie
* #return Vehicule
*/
public function setCategorie($categorie)
{
$this->categorie = $categorie;
return $this;
}
/**
* Get categorie
*
* #return string
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Set puissance
*
* #param integer $puissance
* #return Vehicule
*/
public function setPuissance($puissance)
{
$this->puissance = $puissance;
return $this;
}
/**
* Get puissance
*
* #return integer
*/
public function getPuissance()
{
return $this->puissance;
}
/**
* Set nbPlace
*
* #param integer $nbPlace
* #return Vehicule
*/
public function setNbPlace($nbPlace)
{
$this->nbPlace = $nbPlace;
return $this;
}
/**
* Get nbPlace
*
* #return integer
*/
public function getNbPlace()
{
return $this->nbPlace;
}
/**
* Set kilometrage
*
* #param integer $kilometrage
* #return Vehicule
*/
public function setKilometrage($kilometrage)
{
$this->kilometrage = $kilometrage;
return $this;
}
/**
* Get kilometrage
*
* #return integer
*/
public function getKilometrage()
{
return $this->kilometrage;
}
/**
* Set marque
*
* #param string $marque
* #return Vehicule
*/
public function setMarque($marque)
{
$this->marque = $marque;
return $this;
}
/**
* Get marque
*
* #return string
*/
public function getMarque()
{
return $this->marque;
}
/**
* Set carburant
*
* #param string $carburant
* #return Vehicule
*/
public function setCarburant($carburant)
{
$this->carburant = $carburant;
return $this;
}
/**
* Get carburant
*
* #return string
*/
public function getCarburant()
{
return $this->carburant;
}
/**
* Set transmission
*
* #param string $transmission
* #return Vehicule
*/
public function setTransmission($transmission)
{
$this->transmission = $transmission;
return $this;
}
/**
* Get transmission
*
* #return string
*/
public function getTransmission()
{
return $this->transmission;
}
public function __toString()
{
return (string)$this->id;
}
/**
* Set type
*
* #param string $type
* #return Vehicule
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return string
*/
public function getType()
{
return $this->type;
}
/**
* Set direction
*
* #param \OC\UserBundle\Entity\User $direction
* #return Vehicule
*/
public function setDirection(\OC\UserBundle\Entity\User $direction = null)
{
$this->direction = $direction;
return $this;
}
/**
* Get direction
*
* #return \OC\UserBundle\Entity\User
*/
public function getDirection()
{
return $this->direction;
}
}
and this is my entity User
<?php
namespace OC\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Car\PfeBundle\Entity\Vehicule;
/**
* User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="OC\UserBundle\Entity\UserRepository")
*/
class User implements UserInterface
{
/**
* #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=255, unique=true)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="nomDirec", type="string", length=255, unique=true)
*/
private $nomDirec;
/**
* #var string
*
* #ORM\Column(name="directeur", type="string", length=255)
*/
private $directeur;
/**
* #var string
*
* #ORM\Column(name="adresse", type="string", length=255)
*/
private $adresse;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="fax", type="integer")
*/
private $fax;
/**
* #var string
*
* #ORM\Column(name="tel", type="integer")
*/
private $tel;
/**
* #ORM\Column(name="salt", type="string", length=255)
*/
private $salt;
/**
* #ORM\Column(name="roles", type="array")
*/
private $roles = array();
/**
* #ORM\OneToMany(targetEntity="Car\PfeBundle\Entity\Vehicule", mappedBy="direction", cascade={"remove", "persist"})
*/
protected $vehicule;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* #param string $username
* #return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* #param string $password
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set roles
*
* #param array $roles
* #return User
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* #return array
*/
public function getRoles()
{
return $this->roles;
}
public function eraseCredentials()
{
}
/**
* Set nomDirec
*
* #param string $nomDirec
* #return User
*/
public function setNomDirec($nomDirec)
{
$this->nomDirec = $nomDirec;
return $this;
}
/**
* Get nomDirec
*
* #return string
*/
public function getNomDirec()
{
return $this->nomDirec;
}
/**
* Set directeur
*
* #param string $directeur
* #return User
*/
public function setDirecteur($directeur)
{
$this->directeur = $directeur;
return $this;
}
/**
* Get directeur
*
* #return string
*/
public function getDirecteur()
{
return $this->directeur;
}
/**
* Set adresse
*
* #param string $adresse
* #return User
*/
public function setAdresse($adresse)
{
$this->adresse = $adresse;
return $this;
}
/**
* Get adresse
*
* #return string
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* 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 fax
*
* #param \integer $fax
* #return User
*/
public function setFax($fax)
{
$this->fax = $fax;
return $this;
}
/**
* Get fax
*
* #return \integer
*/
public function getFax()
{
return $this->fax;
}
/**
* Set tel
*
* #param integer $tel
* #return User
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return integer
*/
public function getTel()
{
return $this->tel;
}
/**
* Set salt
*
* #param string $salt
* #return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* #return string
*/
public function getSalt()
{
return $this->salt;
}
public function __toString()
{
return strval( $this->getId() );
}
/**
* Constructor
*/
public function __construct()
{
$this->vehicule = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add vehicule
*
* #param \Car\PfeBundle\Entity\Vehicule $vehicule
* #return User
*/
public function addVehicule(\Car\PfeBundle\Entity\Vehicule $vehicule)
{
$this->vehicule[] = $vehicule;
return $this;
}
/**
* Remove vehicule
*
* #param \Car\PfeBundle\Entity\Vehicule $vehicule
*/
public function removeVehicule(\Car\PfeBundle\Entity\Vehicule $vehicule)
{
$this->vehicule->removeElement($vehicule);
}
/**
* Get vehicule
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getVehicule()
{
return $this->vehicule;
}
}
The correct way to do this in a OOP way , is to define a method that returns the number of vehicules foreach user, so you could to this:
define a getNumberOfVehicules function in your User Class 'Entity'
public function getNumberOfVehicules()
{
return $this->vehicule->count();
}
then in your twig template you just simply call that function , i.e:
{% for user in users %}
<p>{{user.username}} {{user.getNumberOfVehicules()}}</p>
{% endfor %}
to get the desired result, I would do something like this ( haven't tested the query, but it should pretty much work )
$em = $this->getDoctrine();
$userRepository = $em->getRepository('OCUserBundle:User');
$qb = $userRepository->createQueryBuilder('user')
->leftJoin('user.vehicule','vehicule')
->addSelect('COUNT(vehicule.id) AS vehicule_count')
->groupBy('user.id')
->getQuery();
$result = $qb->getResult();
Pass the result to the view
return $this->render('DemandevehBundle:Demande:affiche.html.twig',
array('demandes' => $result ));
In the view you can iterate over the results.
$qb = $this->getEntityManager()->createQueryBuilder();
return $qb->select('u.username as name, count(v.id) as count')
->from('OCUserBundle:User', 'u')
->leftJoin('u.vehicule', 'v')
->groupBy('u.id')
->getQuery()
->getResult();
Here we get an array as result with each user plus no of corresponding vehicles.Just pass result to twig and then foreach name and count.All the best.

Symfony Insert Foreign Key Value

I have 3 tables: EdiTradingPartner, EdiDocType and EdiTradingPartnerTransactions
src\Matrix\MatrixEdiBundle\Entity\EdiTradingPartner
namespace Matrix\MatrixEdiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdiTradingPartner
*
* #ORM\Table(name="edi_trading_partner")
* #ORM\Entity(repositoryClass="Matrix\MatrixEdiBundle\Repository\EdiTradingPartnerRepository")
*/
class EdiTradingPartner
{
/**
* #var string
*
* #ORM\Column(name="edi_interchange_id", type="string", length=30, nullable=false)
*/
private $ediInterchangeId;
/**
* #var string
*
* #ORM\Column(name="tp_name", type="string", length=30, nullable=false)
*/
private $tpName;
/**
* #var string
*
* #ORM\Column(name="tp_location", type="string", length=50, nullable=false)
*/
private $tpLocation;
/**
* #var integer
*
* #ORM\Column(name="edi_trading_partner_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $ediTradingPartnerId;
/**
* Set ediInterchangeId
*
* #param string $ediInterchangeId
* #return EdiTradingPartner
*/
public function setEdiInterchangeId($ediInterchangeId)
{
$this->ediInterchangeId = $ediInterchangeId;
return $this;
}
/**
* Get ediInterchangeId
*
* #return string
*/
public function getEdiInterchangeId()
{
return $this->ediInterchangeId;
}
/**
* Set tpName
*
* #param string $tpName
* #return EdiTradingPartner
*/
public function setTpName($tpName)
{
$this->tpName = $tpName;
return $this;
}
/**
* Get tpName
*
* #return string
*/
public function getTpName()
{
return $this->tpName;
}
/**
* Set tpLocation
*
* #param string $tpLocation
* #return EdiTradingPartner
*/
public function setTpLocation($tpLocation)
{
$this->tpLocation = $tpLocation;
return $this;
}
/**
* Get tpLocation
*
* #return string
*/
public function getTpLocation()
{
return $this->tpLocation;
}
/**
* Get ediTradingPartnerId
*
* #return integer
*/
public function getEdiTradingPartnerId()
{
return $this->ediTradingPartnerId;
}
}
src\Matrix\MatrixEdiBundle\Entity\EdiDocType
<?php
namespace Matrix\MatrixEdiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdiDocType
*
* #ORM\Table(name="edi_doc_type")
* #ORM\Entity(repositoryClass="Matrix\MatrixEdiBundle\Repository\EdiDocTypeRepository")
*/
class EdiDocType
{
/**
* #var integer
*
* #ORM\Column(name="doc_type", type="integer", nullable=false)
*/
private $docType;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255, nullable=false)
*/
private $description;
/**
* #var string
*
* #ORM\Column(name="direction", type="string", length=10, nullable=false)
*/
private $direction;
/**
* #var integer
*
* #ORM\Column(name="edi_doc_type_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $ediDocTypeId;
/**
* Set docType
*
* #param integer $docType
* #return EdiDocType
*/
public function setDocType($docType)
{
$this->docType = $docType;
return $this;
}
/**
* Get docType
*
* #return integer
*/
public function getDocType()
{
return $this->docType;
}
/**
* Set name
*
* #param string $name
* #return EdiDocType
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return EdiDocType
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set direction
*
* #param string $direction
* #return EdiDocType
*/
public function setDirection($direction)
{
$this->direction = $direction;
return $this;
}
/**
* Get direction
*
* #return string
*/
public function getDirection()
{
return $this->direction;
}
/**
* Get ediDocTypeId
*
* #return integer
*/
public function getEdiDocTypeId()
{
return $this->ediDocTypeId;
}
}
src\Matrix\MatrixEdiBundle\Entity\EdiTradingPartnerTransactions
namespace Matrix\MatrixEdiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdiTradingPartnerTransactions
*
* #ORM\Table(name="edi_trading_partner_transactions", indexes={#ORM\Index(name="edi_tp_id", columns={"edi_tp_id", "edi_doc_type_id"}), #ORM\Index(name="edi_transactions_id", columns={"edi_doc_type_id"}), #ORM\Index(name="IDX_F2BE50F7B9C737A1", columns={"edi_tp_id"})})
* #ORM\Entity(repositoryClass="Matrix\MatrixEdiBundle\Repository\EdiTradingPartnerTransactionsRepository")
*/
class EdiTradingPartnerTransactions
{
/**
* #var integer
*
* #ORM\Column(name="edi_tp_transactions_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $ediTpTransactionsId;
/**
* #var \Matrix\MatrixEdiBundle\Entity\EdiDocType
*
* #ORM\ManyToOne(targetEntity="Matrix\MatrixEdiBundle\Entity\EdiDocType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="edi_doc_type_id", referencedColumnName="edi_doc_type_id")
* })
*/
private $ediDocType;
/**
* #var \Matrix\MatrixEdiBundle\Entity\EdiTradingPartner
*
* #ORM\ManyToOne(targetEntity="Matrix\MatrixEdiBundle\Entity\EdiTradingPartner")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="edi_tp_id", referencedColumnName="edi_trading_partner_id")
* })
*/
private $ediTp;
/**
* Get ediTpTransactionsId
*
* #return integer
*/
public function getEdiTpTransactionsId()
{
return $this->ediTpTransactionsId;
}
/**
* Set ediDocType
*
* #param \Matrix\MatrixEdiBundle\Entity\EdiDocType $ediDocType
* #return EdiTradingPartnerTransactions
*/
public function setEdiDocType(\Matrix\MatrixEdiBundle\Entity\EdiDocType $ediDocType = null)
{
$this->ediDocType = $ediDocType;
return $this;
}
/**
* Get ediDocType
*
* #return \Matrix\MatrixEdiBundle\Entity\EdiDocType
*/
public function getEdiDocType()
{
return $this->ediDocType;
}
/**
* Set ediTp
*
* #param \Matrix\MatrixEdiBundle\Entity\EdiTradingPartner $ediTp
* #return EdiTradingPartnerTransactions
*/
public function setEdiTp(\Matrix\MatrixEdiBundle\Entity\EdiTradingPartner $ediTp = null)
{
$this->ediTp = $ediTp;
return $this;
}
/**
* Get ediTp
*
* #return \Matrix\MatrixEdiBundle\Entity\EdiTradingPartner
*/
public function getEdiTp()
{
return $this->ediTp;
}
}
I want to insert into EdiTradingPartnerTransactions table yet I really don't know what to do since I've been receiving this error:
Catchable fatal error: Argument 1 passed to
Matrix\MatrixEdiBundle\Entity\EdiTradingPartnerTransactions::setEdiTp()
must be an instance of
Matrix\MatrixEdiBundle\Entity\EdiTradingPartner, string given, called
in
C:\xampp\htdocs\Editracker\src\Matrix\MatrixEdiBundle\Controller\MatrixController.php
on line 474 and defined in
C:\xampp\htdocs\Editracker\src\Matrix\MatrixEdiBundle\Entity\EdiTradingPartnerTransactions.php
on line 85
Here's my function in the controller side:
public function deleteTpTransAction($tpId, $docType, $direction, $tpName) {
$em = $this->getDoctrine()->getManager();
$docTypeId = $em->getRepository('MatrixEdiBundle:EdiDocType')->getId($docType, $direction);
if ($docTypeId != null) {
$result = $em->getRepository('MatrixEdiBundle:EdiTradingPartnerTransactions')->getTpTrans($tpId, $docType, $direction);
if ($result == null) {
$transaction = new EdiTradingPartnerTransactions();
$transaction->setEdiTp($tpId);
$transaction->setEdiDocType($docTypeId);
$em->persist($transaction);
} else {
foreach ($result as $key) {
$id = $key->getEdiTpTransactionsId();
$transaction = $em->getRepository('MatrixEdiBundle:EdiTradingPartnerTransactions')->find($id);
$em->remove($transaction);
}
}
$em->flush();
}
return $this->redirect($this->generateUrl('matrix_edi_tpTrans'));
}
Any help would really be appreciated. Thanks in advanced!
The error here is pretty clear: Doctrine handle, at least for relationships, only objects of the "related" (let's call that way) entity.
When you do
$transaction->setEdiTp($tpId);
$transaction->setEdiDocType($docTypeId);
you're trying to insert ID(s) of related objects. This is an error: although your db expects an integer (foreign key), Doctrine is an abstaction "pillow" between your code and db; it choose to work with objects and not with ids(you can see it pretty clearly into entity classes files)
Solution here is pretty simple:
Fetch from db the entity for setEdiTp and setEdiDocType
Pass them to the functions
In PHP (Symfony):
$ediTp = $em
->getRepository('MatrixEdiBundle:EdiTp')
->findOneById($tpId);
$ediDocType = $em
->getRepository('MatrixEdiBundle:EdiDocType')
->findOneById($docTypeId);
then
$transaction->setEdiTp($ediTp);
$transaction->setEdiDocType($ediDocType);
Your error is because setEdiTp need an instance of EdiTradingPartner.
Your error say string given.
Because $tpId is string.
Verify
Change
$transaction->setEdiTp($tpId);
To
// $transaction->setEdiTp($tpId);
var_dump($tpId);
Normally, $tpId return a string, not a object.
So when you call deleteTpTransAction, $tpId must be an instance of setEdiTp
Changement :
$editp = $em->getRepository('MatrixEdiBundle:EdiTradingPartner')->find($tpId);
and change
$transaction->setEdiTp($tpId);
by
$transaction->setEdiTp($editTp);

#Assert\Valid of OneToMany property doesn't work

I'm trying to solve my problem but after many hours of search, It doesn't work :(
So I've got a class :
<?php
namespace Application\HappyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Bar
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Application\HappyBundle\Entity\BarRepository")
*/
class Bar
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
* #ORM\Column(name="name", type="string", length=45)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="addressComplete", type="string", length=255)
*/
private $addressComplete;
/**
* #var string
*
* #ORM\Column(name="postal_code", type="string", length=5, nullable=true)
*/
private $postalCode;
/**
* #var string
*
* #ORM\Column(name="town", type="string", length=45)
*/
private $town;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=45)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="latitude", type="string", length=20)
*/
private $latitude;
/**
* #var string
*
* #ORM\Column(name="longitude", type="string", length=20)
*/
private $longitude;
/**
* #var array
*
* #ORM\Column(name="validate", type="boolean", nullable=false)
*/
private $validate;
/**
* #var \DateTime
*
* #ORM\Column(name="last_update", type="datetime", nullable=true)
*/
private $lastUpdate;
/**
* #var ArrayCollection of AssocBarDay
* #Assert\Valid
* #ORM\OneToMany(targetEntity="Application\HappyBundle\Entity\AssocBarDay", mappedBy="bar" , cascade={"persist" , "remove"})
*/
private $day;
/**
* #ORM\OneToMany(targetEntity="Application\HappyBundle\Entity\AssocBarDrink", mappedBy="drink", cascade={"persist"})
*/
private $drink;
/**
* #ORM\OneToMany(targetEntity="Application\HappyBundle\Entity\Bar", mappedBy="barVersionned", cascade={"persist"})
*/
private $barHaveVersions;
/**
* #ORM\ManyToOne(targetEntity="Application\HappyBundle\Entity\Bar", inversedBy="barHaveVersions" , cascade={"persist"})
*/
private $barVersionned;
/**
* Constructor
*/
public function __construct()
{
$this->day = new \Doctrine\Common\Collections\ArrayCollection();
$this->drink = new \Doctrine\Common\Collections\ArrayCollection();
$this->validate = false;
$this->lastUpdate = new \DateTime('now');
}
public function __toString(){
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Bar
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
* #return Bar
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set postalCode
*
* #param string $postalCode
* #return Bar
*/
public function setPostalCode($postalCode)
{
$this->postalCode = $postalCode;
return $this;
}
/**
* Get postalCode
*
* #return string
*/
public function getPostalCode()
{
return $this->postalCode;
}
/**
* Set town
*
* #param string $town
* #return Bar
*/
public function setTown($town)
{
$this->town = $town;
return $this;
}
/**
* Get town
*
* #return string
*/
public function getTown()
{
return $this->town;
}
/**
* Set country
*
* #param string $country
* #return Bar
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set latitude
*
* #param string $latitude
* #return Bar
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* #return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* #param string $longitude
* #return Bar
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* #return string
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Add drink
*
* #param \Application\HappyBundle\Entity\AssocBarDrink $drink
* #return Bar
*/
public function addDrink(\Application\HappyBundle\Entity\AssocBarDrink $drink)
{
$this->drink[] = $drink;
return $this;
}
/**
* Remove drink
*
* #param \Application\HappyBundle\Entity\AssocBarDrink $drink
*/
public function removeDrink(\Application\HappyBundle\Entity\AssocBarDrink $drink)
{
$this->drink->removeElement($drink);
}
/**
* Get drink
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getDrink()
{
return $this->drink;
}
/**
* Add day
*
* #param \Application\HappyBundle\Entity\AssocBarDay $day
* #return Bar
*/
public function addDay(\Application\HappyBundle\Entity\AssocBarDay $day)
{
$this->day[] = $day;
return $this;
}
/**
* Remove day
*
* #param \Application\HappyBundle\Entity\AssocBarDay $day
*/
public function removeDay(\Application\HappyBundle\Entity\AssocBarDay $day)
{
$this->day->removeElement($day);
}
/**
* Get day
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getDay()
{
return $this->day;
}
/**
* Set lastUpdate
*
* #param \DateTime $lastUpdate
* #return Bar
*/
public function setLastUpdate($lastUpdate)
{
$this->lastUpdate = $lastUpdate;
return $this;
}
/**
* Get lastUpdate
*
* #return \DateTime
*/
public function getLastUpdate()
{
return $this->lastUpdate;
}
/**
* Set validate
*
* #param boolean $validate
* #return Bar
*/
public function setValidate($validate)
{
$this->validate = $validate;
return $this;
}
/**
* Get validate
*
* #return boolean
*/
public function getValidate()
{
return $this->validate;
}
/**
* Add barHaveVersions
*
* #param \Application\HappyBundle\Entity\Bar $barHaveVersions
* #return Bar
*/
public function addBarHaveVersion(\Application\HappyBundle\Entity\Bar $barHaveVersions)
{
$this->barHaveVersions[] = $barHaveVersions;
return $this;
}
/**
* Remove barHaveVersions
*
* #param \Application\HappyBundle\Entity\Bar $barHaveVersions
*/
public function removeBarHaveVersion(\Application\HappyBundle\Entity\Bar $barHaveVersions)
{
$this->barHaveVersions->removeElement($barHaveVersions);
}
/**
* Get barHaveVersions
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBarHaveVersions()
{
return $this->barHaveVersions;
}
/**
* Set barVersionned
*
* #param \Application\HappyBundle\Entity\Bar $barVersionned
* #return Bar
*/
public function setBarVersionned(\Application\HappyBundle\Entity\Bar $barVersionned = null)
{
$this->barVersionned = $barVersionned;
return $this;
}
/**
* Get barVersionned
*
* #return \Application\HappyBundle\Entity\Bar
*/
public function getBarVersionned()
{
return $this->barVersionned;
}
/**
* Set addressComplete
*
* #param string $addressComplete
* #return Bar
*/
public function setAddressComplete($addressComplete)
{
$this->addressComplete = $addressComplete;
return $this;
}
/**
* Get addressComplete
*
* #return string
*/
public function getAddressComplete()
{
return $this->addressComplete;
}
}
and OneToMany with this class:
<?php
namespace Application\HappyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContextInterface;
/**
* AssocBarDayHappyHour
* #ORM\Table(name="assoc_bar_day")
* #ORM\Entity(repositoryClass="Application\HappyBundle\Entity\AssocBarDayRepository")
*/
class AssocBarDay
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Application\HappyBundle\Entity\Bar", inversedBy="day" , cascade={"persist"})
*/
private $bar;
/**
* #var array
* #ORM\Column(name="day", type="array", nullable=true)
*/
private $day;
/**
* #var \DateTime
* #Assert\Time()
* #ORM\Column(name="time_start_happy_hour", type="time", nullable=true)
*/
private $timeStartHappyHour;
/**
* #var \DateTime
* #Assert\Time()
* #ORM\Column(name="time_end_happy_hour", type="time", nullable=true)
*/
private $timeEndHappyHour;
/**
* #var \DateTime
* #Assert\Time()
* #ORM\Column(name="time_bar_open", type="time", nullable=true)
*/
private $timeBarOpen;
/**
* #var \DateTime
* #Assert\Time()
* #ORM\Column(name="time_bar_close", type="time" , nullable=true)
*/
private $timeBarClose;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set timeStartHappyHour
*
* #param \DateTime $timeStartHappyHour
* #return AssocBarDay
*/
public function setTimeStartHappyHour($timeStartHappyHour)
{
$this->timeStartHappyHour = $timeStartHappyHour;
return $this;
}
/**
* Get timeStartHappyHour
*
* #return \DateTime
*/
public function getTimeStartHappyHour()
{
return $this->timeStartHappyHour;
}
/**
* Set timeEndHappyHour
*
* #param \DateTime $timeEndHappyHour
* #return AssocBarDay
*/
public function setTimeEndHappyHour($timeEndHappyHour)
{
$this->timeEndHappyHour = $timeEndHappyHour;
return $this;
}
/**
* Get timeEndHappyHour
*
* #return \DateTime
*/
public function getTimeEndHappyHour()
{
return $this->timeEndHappyHour;
}
/**
* Set timeBarOpen
*
* #param \DateTime $timeBarOpen
* #return AssocBarDay
*/
public function setTimeBarOpen($timeBarOpen)
{
$this->timeBarOpen = $timeBarOpen;
return $this;
}
/**
* Get timeBarOpen
*
* #return \DateTime
*/
public function getTimeBarOpen()
{
return $this->timeBarOpen;
}
/**
* Set timeBarClose
*
* #param \DateTime $timeBarClose
* #return AssocBarDay
*/
public function setTimeBarClose($timeBarClose)
{
$this->timeBarClose = $timeBarClose;
return $this;
}
/**
* Get timeBarClose
*
* #return \DateTime
*/
public function getTimeBarClose()
{
return $this->timeBarClose;
}
/**
* Set bar
*
* #param \Application\HappyBundle\Entity\Bar $bar
* #return AssocBarDay
*/
public function setBar(\Application\HappyBundle\Entity\Bar $bar = null)
{
$this->bar = $bar;
return $this;
}
/**
* Get bar
*
* #return \Application\HappyBundle\Entity\Bar
*/
public function getBar()
{
return $this->bar;
}
/**
* Set day
*
* #param array $day
* #return AssocBarDay
*/
public function setDay($day)
{
$this->day = $day;
return $this;
}
/**
* Get day
*
* #return array
*/
public function getDay()
{
return $this->day;
}
}
So when I post my form, the Assert of the Collection of days doesn't block when time is empty?
I put correctly the:
#Assert\Valid
Over day Collection and
#Assert\Time()
of each property of AssocBarDay
:( anyone I've got an idea?
Thanks for your help.
The reason is almost all of the validation constraints will not return invalid for empty/null values.
Just have a look at the TimeValidator's validate method ...
if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}
Therefore you will need to add the #Assert\NotBlank constraint additionally to have empty values generate a validation error for that property.
NotBlank
Validates that a value is not blank, defined as not equal to a blank
string and also not equal to null.

Resources