I've these two entities:
Event:
class Event
{
/**
* #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;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* #var boolean
*
* #ORM\Column(name="invitationPermission", type="boolean")
*/
private $invitationPermission;
/**
* #var \DateTime
*
* #ORM\Column(name="creationDate", type="date")
*/
private $creationDate;
/**
* #var \DateTime
*
* #ORM\Column(name="modifiedDate", type="date")
*/
private $modifiedDate;
/**
* #ORM\OneToMany(targetEntity="Users", mappedBy="id")
*/
private $user;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Event
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
* #return Event
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set invitationPermission
*
* #param boolean $invitationPermission
* #return Event
*/
public function setInvitationPermission($invitationPermission)
{
$this->invitationPermission = $invitationPermission;
return $this;
}
/**
* Get invitationPermission
*
* #return boolean
*/
public function getInvitationPermission()
{
return $this->invitationPermission;
}
/**
* Set creationDate
*
* #param \DateTime $creationDate
* #return Event
*/
public function setCreationDate($creationDate)
{
$this->creationDate = $creationDate;
return $this;
}
/**
* Get creationDate
*
* #return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Set modifiedDate
*
* #param \DateTime $modifiedDate
* #return Event
*/
public function setModifiedDate($modifiedDate)
{
$this->modifiedDate = $modifiedDate;
return $this;
}
/**
* Get modifiedDate
*
* #return \DateTime
*/
public function getModifiedDate()
{
return $this->modifiedDate;
}
/**
* Set user
*
* #param user $user
* #return Event
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return user
*/
public function getUser()
{
return $this->user;
}
And Users:
class Users
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="nick", type="string", length=255)
*/
private $nick;
/**
* #var string
*
* #ORM\Column(name="pwd", type="string", length=255)
*/
private $pwd;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 nick
*
* #param string $nick
* #return Users
*/
public function setNick($nick)
{
$this->nick = $nick;
return $this;
}
/**
* Get nick
*
* #return string
*/
public function getNick()
{
return $this->nick;
}
/**
* Set pwd
*
* #param string $pwd
* #return Users
*/
public function setPwd($pwd)
{
$this->pwd = $pwd;
return $this;
}
/**
* Get pwd
*
* #return string
*/
public function getPwd()
{
return $this->pwd;
}
}
And I try to get an Event, and then by Event, get the User and then, it's Nickname:
$event = $em->getRepository('MyBundle:Event')->findOneById(1);
print_r($event->getUser()->getNickname());
And I'm getting:
Fatal error: Call to undefined method Doctrine\ORM\PersistentCollection::getNickname() in C:\xampp\htdocs\web\src\KNV\MyBundle\Controller\DefaultController.php on line 43
Any idea?
However, if I print the result of getUser() it's returning a big load of data (Most related with symfony framework).
First, there is no getNickname() method within your User entity, you should then use getNick().
But I think the problem is elsewhere, You defined a oneToMany relation betweet Event and User. So, an event could be related to many users. Then, if you call getUser() on a given event you'll then get a collection of users. You should then iterate to get single users objects upon which you can call getNick().
Related
Query Warning message from Symfony Profiler:
Klient Entity
The mappings AdminBundle\Entity\Klient#contacts and
AdminBundle\Entity\KlientContact#fkKlient
are inconsistent with each other.
KlientContact Entity
The association AdminBundle\Entity\KlientContact#fkKlient refers to
the inverse side field AdminBundle\Entity\Klient#nip which is not
defined as association. The association
AdminBundle\Entity\KlientContact#fkKlient refers to the inverse side
field AdminBundle\Entity\Klient#nip which does not exist.
What did I do wrong ? I assume that there is something that I am missing on Klient#nip (which is mapped as Id field)
[Klient Entity]
<?php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AdminBundle\Entity\KlientContact;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Klient
*
* #ORM\Table(name="klient")
* #ORM\Entity(repositoryClass="AdminBundle\Repository\KlientRepository")
* #ORM\HasLifecycleCallbacks
*/
class Klient
{
public function __construct()
{
$this->contacts = new ArrayCollection();
}
public function __toString() {
return "NIP: ".$this->nip .", NAZWA: ". $this->name .", TEL: ". $this->tel;
}
/**
* #var string
* #ORM\Id
* #ORM\Column(name="nip", type="string", length=255)
*
*/
private $nip;
/**
* #var string
*
* #ORM\Column(name="regon", type="string", length=255)
*/
private $regon;
/**
* #var \DateTime
* #ORM\Column(name="inserted_at", type="datetime", nullable=true)
*/
private $insertedAt;
/**
* #var \DateTime
* #ORM\Column(name="last_edited_at", type="datetime", nullable=true)
*/
private $lastEditedAt;
/**
* #var string
*
* #ORM\Column(name="krs", type="string", length=255)
*/
private $krs;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="tel", type="string", length=255, nullable=true)
*/
private $tel;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255, nullable=true)
*/
private $email;
/**
* #var type
*
* #ORM\OneToMany(targetEntity="KlientContact", mappedBy="fkKlient")
*/
private $contacts; // optional properyty holds an array of contact objects - this is not a fild in database
/**
* Set nip
*
* #param string $nip
*
* #return Klient
*/
public function setNip($nip)
{
$this->nip = $nip;
return $this;
}
/**
* Get nip
*
* #return string
*/
public function getNip()
{
return $this->nip;
}
/**
* Set regon
*
* #param string $regon
*
* #return Klient
*/
public function setRegon($regon)
{
$this->regon = $regon;
return $this;
}
/**
* Get regon
*
* #return string
*/
public function getRegon()
{
return $this->regon;
}
/**
* Set krs
*
* #param string $krs
*
* #return Klient
*/
public function setKrs($krs)
{
$this->krs = $krs;
return $this;
}
/**
* Get krs
*
* #return string
*/
public function getKrs()
{
return $this->krs;
}
/**
* Set address
*
* #param string $address
*
* #return Klient
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set name
*
* #param string $name
*
* #return Klient
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set tel
*
* #param string $tel
*
* #return Klient
*/
public function setTel($tel)
{
$this->tel = $tel;
return $this;
}
/**
* Get tel
*
* #return string
*/
public function getTel()
{
return $this->tel;
}
/**
* Set email
*
* #param string $email
*
* #return Klient
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Add contact
*
* #param \AdminBundle\Entity\KlientContact $contact
*
* #return Klient
*/
public function addContact(\AdminBundle\Entity\KlientContact $contact)
{
$this->contacts[] = $contact;
return $this;
}
/**
* Remove contact
*
* #param \AdminBundle\Entity\KlientContact $contact
*/
public function removeContact(\AdminBundle\Entity\KlientContact $contact)
{
$this->contacts->removeElement($contact);
}
/**
* Get contacts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getContacts()
{
return $this->contacts;
}
/**
* Set insertedAt
*
* #param \DateTime $insertedAt
*
* #return Klient
*/
public function setInsertedAt(\DateTime $insertedAt)
{
$this->insertedAt = $insertedAt;
return $this;
}
/**
* Get insertedAt
*
* #return \DateTime
*/
public function getInsertedAt()
{
return $this->insertedAt;
}
/**
* Set lastEditedAt
*
* #param \DateTime $lastEditedAt
*
* #return Klient
*/
public function setLastEditedAt(\DateTime $lastEditedAt)
{
$this->lastEditedAt = $lastEditedAt;
return $this;
}
/**
* Get lastEditedAt
*
* #return \DateTime
*/
public function getLastEditedAt()
{
return $this->lastEditedAt;
}
/**
*
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setLastEditedAt(new \DateTime('now'));
if ($this->getInsertedAt() == null) {
$this->setInsertedAt(new \DateTime('now'));
}
}
}
[KlientContact Entity]
<?php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AdminBundle\Entity\Klient;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* KlientContact
*
* #ORM\Table(name="klient_contact")
* #ORM\Entity(repositoryClass="AdminBundle\Repository\KlientContactRepository")
* #ORM\HasLifecycleCallbacks
*/
class KlientContact implements UserInterface
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="inserted_at", type="datetime", nullable=true)
*/
private $insertedAt;
/**
* #var \DateTime
*
* #ORM\Column(name="last_edited_at", type="datetime", nullable=true)
*/
private $lastEditedAt;
/**
* #ORM\ManyToOne(targetEntity="Klient", inversedBy="nip")
* #ORM\JoinColumn(name="fk_klient", referencedColumnName="nip")
*/
private $fkKlient;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="surname", type="string", length=255)
*/
private $surname;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=255)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="login", type="string", length=255, unique=true)
*/
private $login;
/**
* #var string
*
* #ORM\Column(name="password", type="text")
*/
private $password;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return KlientContact
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set surname
*
* #param string $surname
*
* #return KlientContact
*/
public function setSurname($surname)
{
$this->surname = $surname;
return $this;
}
/**
* Get surname
*
* #return string
*/
public function getSurname()
{
return $this->surname;
}
/**
* Set email
*
* #param string $email
*
* #return KlientContact
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phone
*
* #param string $phone
*
* #return KlientContact
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set login
*
* #param string $login
*
* #return KlientContact
*/
public function setLogin($login)
{
$this->login = $login;
return $this;
}
/**
* Get login
*
* #return string
*/
public function getLogin()
{
return $this->login;
}
/**
* Set password
*
* #param string $password
*
* #return KlientContact
*/
public function setPassword($password)
{
$hash = password_hash($password, PASSWORD_BCRYPT , ['cost' => 5]);
$this->password = $hash;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set fkKlient
*
* #param \AdminBundle\Entity\Klient $fkKlient
*
* #return KlientContact
*/
public function setFkKlient(\AdminBundle\Entity\Klient $fkKlient = null)
{
$this->fkKlient = $fkKlient;
return $this;
}
/**
* Get fkKlient
*
* #return \AdminBundle\Entity\Klient
*/
public function getFkKlient()
{
return $this->fkKlient;
}
public function eraseCredentials() {
}
public function getRoles() {
return ['ROLE_USER'];
}
public function getSalt() {
return null;
}
public function getUsername() {
}
/**
* Set insertedAt
*
* #param \DateTime $insertedAt
*
* #return KlientContact
*/
public function setInsertedAt(\DateTime $insertedAt)
{
$this->insertedAt = $insertedAt;
return $this;
}
/**
* Get insertedAt
*
* #return \DateTime
*/
public function getInsertedAt()
{
return $this->insertedAt;
}
/**
* Set lastEditedAt
*
* #param \DateTime $lastEditedAt
*
* #return KlientContact
*/
public function setLastEditedAt(\DateTime $lastEditedAt)
{
$this->lastEditedAt = $lastEditedAt;
return $this;
}
/**
* Get lastEditedAt
*
* #return \DateTime
*/
public function getLastEditedAt()
{
return $this->lastEditedAt;
}
/**
*
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setLastEditedAt(new \DateTime('now'));
if ($this->getInsertedAt() == null) {
$this->setInsertedAt(new \DateTime('now'));
}
}
}
The inverse side is wrong. Should be:
[KlientContact Entity]
/**
* #ORM\ManyToOne(targetEntity="Klient", inversedBy="contacts")
* #ORM\JoinColumn(name="fk_klient", referencedColumnName="nip")
*/
private $fkKlient;
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.
I am trying to write a web service to fetch the category list and all the business under that category in an nested array fashion.
I am getting an Semantical Error saying :
{
code: 500
message: "[Semantical Error] line 0, col 14 near 'StreetBumbApiBundle:Buss_owner': Error: Class 'StreetBumb\ApiBundle\Entity\Buss_owner' is not defined."
}
I already defined the entity in the controller, i dont know why it is showing this error.
This is how my controller's function looks like:
public function getCategoryAction(){
$em = $this->getDoctrine()->getEntityManager();
$pro = $em->getRepository('StreetBumbApiBundle:Category')
->getBusinessByCategory();
//return $pro;
$i = 0;
foreach($pro as $p)
{
$data['category'][$i]['id'] = $p->getId();
$data['category'][$i]['Name'] = $p->getCatName();
//$result[$i] = $p->getId();
$catId = $p->getId();
$business = $em->createQuery('SELECT b FROM StreetBumbApiBundle:Buss_owner b WHERE b.catId = :catId')->setParameter('catId', $catId);
$result = $business->getResult();
foreach($result as $r)
{
$data['business'][$i]['id'][] = $r->getId();
}
$i++;
}
return $data;
}
Please guide if anyone have idea.. Thanx
UPDATE:
Buss_owner Entity:
<?php
namespace StreetBumb\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Buss_owner
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="StreetBumb\ApiBundle\Entity\Buss_ownerRepository")
*/
class Buss_owner
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var integer
*
* #ORM\Column(name="phno", type="integer")
*/
private $phno;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="fbId", type="integer")
*/
private $fbId;
/**
* #var string
*
* #ORM\Column(name="uniqueId", type="string", length=255)
*/
private $uniqueId;
/**
* #var integer
*
* #ORM\Column(name="catId", type="integer")
* #ORM\ManyToMany(targetEntity="Category", mappedBy="Buss_owner")
*/
private $catId;
public function __construct() {
$this->catId = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Buss_owner
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set email
*
* #param string $email
* #return Buss_owner
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phno
*
* #param integer $phno
* #return Buss_owner
*/
public function setPhno($phno)
{
$this->phno = $phno;
return $this;
}
/**
* Get phno
*
* #return integer
*/
public function getPhno()
{
return $this->phno;
}
/**
* Set address
*
* #param string $address
* #return Buss_owner
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set catId
*
* #param integer $catId
* #return Buss_owner
*/
public function setCatId($catId)
{
$this->catId = $catId;
return $this;
}
/**
* Get catId
*
* #return integer
*/
public function getCatId()
{
return $this->catId;
}
/**
* Set password
*
* #param string $password
* #return Buss_owner
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set uniqueId
*
* #param string $uniqueId
* #return Buss_owner
*/
public function setUniqueId($uniqueId)
{
$this->uniqueId = $uniqueId;
return $this;
}
/**
* Get uniqueId
*
* #return string
*/
public function getUniqueId()
{
return $this->uniqueId;
}
/**
* Set fbId
*
* #param integer $fbId
* #return Buss_owner
*/
public function setFbId($fbId)
{
$this->fbId = $fbId;
return $this;
}
/**
* Get fbId
*
* #return integer
*/
public function getFbId()
{
return $this->fbId;
}
}
I believe the problem is the underscore in the entity name.
Doctrine's naming convention is to use CamelCase which it then converts to underscores in the database. Due to this behind the scenes magic, underscores can cause problems in entity names.
Try changing the entity class name to BussOwner and calling this in the controller. If you can't change the table name you can handle this by modifying the entity annotation to:
#ORM\Table(name="buss_owner")
I've been battling with this for several hours. I've built an app in Symfony 2.5 and when I go to log in as a user I receive this error:
FatalErrorException: Error: Call to a member function toArray() on a
non-object in /var/www/cwwa/src/CWWA/CoreBundle/Entity/Users.php line
402
On line 402, I have this code:
/**
* #inheritDoc
*/
public function getRoles()
{
//return array('ROLE_CUSTOMER_USER');
return $this->roles->toArray();
}
If I comment out return $this->roles->toArray(); and replace it with the line above it, my user gets logged in to the system without an issue.
Now the main issue I see other people having has been with Symfony2 running on a server with PHP 5.4. My laptop was running this until I downgraded it to 5.3. This hasn't solved the issue.
Another method I used was to replace return $this->roles->toArray(); to return $this->roles;, I get this error:
Catchable Fatal Error: Argument 4 passed to
Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct()
must be an array, integer given, called in
/var/www/cwwa/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php
on line 96 and defined in
/var/www/cwwa/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php
line 36
I assume from this that the system is retrieving a value from the database.
My Database uses two tables for security. One User table and the other is the Role table. I've included the Entity files for both the User and Role tables below:
Users.php
<?php
namespace CWWA\CoreBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="CWWA\CoreBundle\Entity\Users")
*/
class Users implements UserInterface, \Serializable
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=500, nullable=false)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=500, nullable=false)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=500, nullable=false)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="salt", type="string", length=500, nullable=false)
*/
private $salt;
/**
* #var string
*
* #ORM\Column(name="first_name", type="string", length=125, nullable=false)
*/
private $firstName;
/**
* #var string
*
* #ORM\Column(name="surname", type="string", length=45, nullable=false)
*/
private $surname;
/**
* #var integer
*
* #ORM\Column(name="customer", type="integer", nullable=false)
*/
private $customer;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime", nullable=false)
*/
private $created;
/**
* #var boolean
*
* #ORM\Column(name="is_active", type="boolean", nullable=false)
*/
private $isActive;
/**
* #var boolean
*
* #ORM\Column(name="blocked", type="boolean", nullable=false)
*/
private $blocked;
/**
* #var integer
*
* #ORM\Column(name="access_list", type="integer", nullable=false)
*/
private $accessList;
/**
* #ORM\ManyToMany(targetEntity="Roles", inversedBy="roles")
*
*/
private $roles;
public function __construct()
{
return $this->isActive = true;
return $this->salt = md5(uniqid(null, true));
return $this->roles = new ArrayCollection();
}
/**
* 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 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 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 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 surname
*
* #param string $surname
* #return Users
*/
public function setSurname($surname)
{
$this->surname = $surname;
return $this;
}
/**
* Get surname
*
* #return string
*/
public function getSurname()
{
return $this->surname;
}
/**
* Set customer
*
* #param integer $customer
* #return Users
*/
public function setCustomer($customer)
{
$this->customer = $customer;
return $this;
}
/**
* Get customer
*
* #return integer
*/
public function getCustomer()
{
return $this->customer;
}
/**
* Set created
*
* #param \DateTime $created
* #return Users
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set isActive
*
* #param boolean $isActive
* #return Users
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* #return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Set blocked
*
* #param boolean $blocked
* #return Users
*/
public function setBlocked($blocked)
{
$this->blocked = $blocked;
return $this;
}
/**
* Get blocked
*
* #return boolean
*/
public function getBlocked()
{
return $this->blocked;
}
/**
* Set accessList
*
* #param integer $accessList
* #return Users
*/
public function setAccessList($accessList)
{
$this->accessList = $accessList;
return $this;
}
/**
* Get accessList
*
* #return integer
*/
public function getAccessList()
{
return $this->accessList;
}
/**
* Set roles
*
* #param integer $roles
* #return Users
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* #inheritDoc
*/
public function getRoles()
{
//return array('ROLE_CUSTOMER_USER');
return $this->roles;
}
/**
* #inheritDoc
*/
public function eraseCredentials()
{
}
/**
* #see \Serializable::serialize()
*/
public function serialize()
{
return serialize(array(
$this->id,
));
}
/**
* #see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
) = unserialize($serialized);
}
public function isEqualTo(UserInterface $user)
{
return $this->id === $user->getId();
}
}
Roles.php
<?php
namespace CWWA\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\Role\RoleInterface;
/**
* Roles
*
* #ORM\Table(name="roles")
* #ORM\Entity
*/
class Roles implements RoleInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="names", type="string", length=30, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="role", type="string", length=20, nullable=false)
*/
private $role;
/**
* #ORM\ManyToMany(targetEntity="Users", mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set names
*
* #param string $names
* #return Roles
*/
public function setNames($names)
{
$this->names = $names;
return $this;
}
/**
* Get names
*
* #return string
*/
public function getNames()
{
return $this->names;
}
/**
* Set role
*
* #param string $role
* #return Roles
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* #return string
*/
public function getRole()
{
return $this->role;
}
}
Any help would be fantastic, especially after 4 hours of hitting brick walls!
In your User entity, the roles field should be inversed by users (the field on the other side of the entity).
/**
* #ORM\ManyToMany(targetEntity="Roles", inversedBy="users")
*/
private $roles;
You might need to update your database.
I had a similar problem. The toArray() method isn't working because $this->roles is not an instance of ArrayCollection, instead it's an integer. I don't quite understand why you have three return statements in your Users constructor function; try getting rid of those first and just set the properties you need to set:
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->roles = new ArrayCollection();
}
Now the toArray() method should work as $this->roles should be an ArrayCollection(). Just remember that the Symfony security layer expects either an array of role objects or an array of strings.
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.