Notice: unserialize(): Error at offset 0 of 6 bytes - php

when i try to display all users for admin interface i get this error
Notice: unserialize(): Error at offset 0 of 6 bytes
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* Description of AdminController
*
* #author saif
*/
class AdminController extends Controller {
public function valide_compteAction()
{
$em=$this->getDoctrine()->getManager();
$utilisateur=$em->getRepository('WelcomeBundle:Utilisateur')->findall();
return $this->render('AdminBundle:admin:valide_compte.html.twig',array('i'=>$utilisateur));
}
}
and this is my userclass
namespace WelcomeBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use \Symfony\Component\Security\Core\User\AdvancedUserInterface;
use JMS\SerializerBundle\Annotation\Type;
/**
* Utilisateur
*
* #ORM\Table(name="utilisateur")
* #ORM\Entity
*/
class Utilisateur extends BaseUser implements AdvancedUserInterface, \Serializable
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=40, nullable=false)
*/
protected $nom;
/**
* #var string
*
* #ORM\Column(name="prenom", type="string", length=40, nullable=false)
*/
protected $prenom;
/**
* #var integer
*
* #ORM\Column(name="numtelphone", type="integer", nullable=true)
*/
protected $numtelphone;
/**
* #var string
*
* #ORM\Column(name="addresse", type="string", length=40, nullable=true)
*/
protected $addresse;
/**
* #var integer
*
* #ORM\Column(name="jeton", type="integer", nullable=true)
*/
protected $jeton;
/**
* #var string
*
* #ORM\Column(name="photo", type="blob", nullable=true)
*/
protected $photo;
/**
* #var string
*
* #ORM\Column(name="mailreclamation", type="string", length=50, nullable=true)
*/
protected $mailreclamation;
public function __construct()
{
parent::__construct();
// your own logic
}
public function getId() {
return $this->id;
}
public function getNom() {
return $this->nom;
}
public function getPrenom() {
return $this->prenom;
}
public function getNumtelphone() {
return $this->numtelphone;
}
public function getAddresse() {
return $this->addresse;
}
public function getJeton() {
return $this->jeton;
}
public function getPhoto() {
return $this->photo;
}
public function getMailreclamation() {
return $this->mailreclamation;
}
public function setId($id) {
$this->id = $id;
}
public function setNom($nom) {
$this->nom = $nom;
}
public function setPrenom($prenom) {
$this->prenom = $prenom;
}
public function setNumtelphone($numtelphone) {
$this->numtelphone = $numtelphone;
}
public function setAddresse($addresse) {
$this->addresse = $addresse;
}
public function setJeton($jeton) {
$this->jeton = $jeton;
}
public function setPhoto($photo) {
$this->photo = $photo;
}
public function setMailreclamation($mailreclamation) {
$this->mailreclamation = $mailreclamation;
}
public function json_encode()
{
return json_encode(array(
$this->password,
$this->salt,
$this->usernameCanonical,
$this->username,
$this->expired,
$this->locked,
$this->credentialsExpired,
$this->enabled,
$this->id,
));
}
}
how can i solve this, i have wasted a lot of time in this

Check the serialized data in the database. Are they consistent? It is possible that the database driver assumes another data type. In this case check the blob type for the photo column. I had had a similar problem with json/array type.

Related

Relation One to Many return empty object

I have a Evaluation entity which has one Product and Product which can have several Evaluations. I'm trying to fetch one Product and to get the list of Evaluations associated with my entity
Produit.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use phpDocumentor\Reflection\Types\This;
/**
* Produit
*
* #ORM\Table(name="produit", indexes={#ORM\Index(name="fk_idcatedel", columns={"idCategorie"})})
* #ORM\Entity
*/
class Produit
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(name="libelle", type="string", length=20, nullable=true)
*/
private $libelle;
/**
* #var float|null
*
* #ORM\Column(name="prix", type="float", precision=10, scale=0, nullable=true)
*/
private $prix;
/**
* #var string|null
*
* #ORM\Column(name="description", type="string", length=50, nullable=true)
*/
private $description;
/**
* #var int
*
* #ORM\Column(name="qt", type="integer", nullable=false)
*/
private $qt;
/**
* #var string|null
*
* #ORM\Column(name="img", type="string", length=255, nullable=true)
*/
private $img;
/**
* #var \Categorie
*
* #ORM\ManyToOne(targetEntity="Categorie")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idCategorie", referencedColumnName="id")
* })
*/
private $idcategorie;
/**
* #ORM\OneToMany(targetEntity="Evaluation", mappedBy="idProduit")
*/
private $rates;
public function __construct()
{
$this->rates = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(?string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
public function getPrix(): ?float
{
return $this->prix;
}
public function setPrix(?float $prix): self
{
$this->prix = $prix;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getQt(): ?int
{
return $this->qt;
}
public function setQt(int $qt): self
{
$this->qt = $qt;
return $this;
}
public function getImg(): ?string
{
return $this->img;
}
public function setImg(?string $img): self
{
$this->img = $img;
return $this;
}
public function getIdcategorie(): ?Categorie
{
return $this->idcategorie;
}
public function setIdcategorie(?Categorie $idcategorie): self
{
$this->idcategorie = $idcategorie;
return $this;
}
/**
* #return Collection|Evaluation[]
*/
public function getRates(): Collection
{
return $this->rates;
}
}
Evaluation.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Evaluation
*
* #ORM\Table(name="evaluation", indexes={#ORM\Index(name="fk_idprodevaldel", columns={"id_produit"}), #ORM\Index(name="fk_iduser", columns={"id_user"})})
* #ORM\Entity
*/
class Evaluation
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="note", type="integer", nullable=false)
*/
private $note;
/**
* #var \Produit
*
* #ORM\ManyToOne(targetEntity="Produit", inversedBy="rates")
* #ORM\JoinColumn(name="id_produit", referencedColumnName="id")
*/
private $idProduit;
/**
* #var \Compte
*
* #ORM\ManyToOne(targetEntity="Compte")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_user", referencedColumnName="email")
* })
*/
private $idUser;
public function getId(): ?int
{
return $this->id;
}
public function getNote(): ?int
{
return $this->note;
}
public function setNote(int $note): self
{
$this->note = $note;
return $this;
}
public function getIdProduit(): ?Produit
{
return $this->idProduit;
}
public function setIdProduit(?Produit $idProduit): self
{
$this->idProduit = $idProduit;
return $this;
}
public function getIdUser(): ?Compte
{
return $this->idUser;
}
public function setIdUser(?Compte $idUser): self
{
$this->idUser = $idUser;
return $this;
}
}
The database
In my controller I succeed to get informations from the products but rates are empty
$produits = $this->getDoctrine()
->getRepository(Produit::class)
->find(1);
dump($produits);
$rates = $produits->getRates();
dump($rates); // #collection: ArrayCollection is empty
The Output :
The collection is not yet initialized due to lazy loading, and rightfully so. If you don't access at least to an element in the collection, it's pointless to load the whole collection because doctrine can safely assume you'll "discard" it. As soon as you access an element (either by looping onto collection or getting a specific element), the collection will be initialized and you have all items.
Another way is to use an EAGER fetch that will load the whole collection in the hydration phase. I would not reccomend it however, unless you're sure that everytime you load a Produit, you need this collection "ready". Even in the latter case, I would handle the collection "manually" as I recommend not to lose control on it (let's pretend you have A LOT of element inside it).
Read more about proxies and association, here

How do I convert Doctrine Annotation Information To Array?

I have the following inventory entity object for Doctrine that I created for use in Symfony 3.0.
Simply put how do I get access to the information that I put in the entity via annotations?
For example, companyID, has a ManyToOne annotation that references inversedBy="location". This particular information is very useful to me so I can tell if its a child relationship by foreign key or parent relationship.
If I can get the information about the entity that I described via annotations somehow in an array with Doctrine that would be great. Is this possible to do? Essentially I'm looking for introspection functions on the entity.
<?php
namespace AppBundle\Entity;
use Gedmo\Translatable\Translatable;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Inventory
*
* #ORM\Table(name="distribution_inventory")
* #ORM\Entity(repositoryClass="AppBundle\Repository\InventoryRepository")
*/
class Inventory implements Translatable {
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Gedmo\Translatable
* #ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* #var string
* #Gedmo\Slug(fields={"name","id"},suffix=".html")
* #ORM\Column(name="inventoryslug", type="string", length=255, nullable=false, nullable=true)
*/
private $inventoryslug;
/**
* #var string
*
* #ORM\Column(name="barcode", type="string", length=255, nullable=true)
*/
private $barcode;
/**
* #var string
* #Gedmo\Translatable
* #ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* #var string
* #ORM\Column(name="imagename", type="string", nullable=true)
*/
private $imagename;
/**
* #Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
/**
* #var \AppBundle\Entity\InventoryCategory
*
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\InventoryCategory")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="categoryid", referencedColumnName="id")
* })
*/
private $category;
/**
* #var \AppBundle\Entity\Company
* #Assert\Type(type="\AppBundle\Entity\Company")
* #Assert\Valid()
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\Company", inversedBy="Location")
* #ORM\JoinColumn(name="companyid", referencedColumnName="id", onDelete="CASCADE")
*/
protected $companyId;
/**
* #var \DateTime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
/**
* #var string
* #ORM\Column(name="defaultsellprice",precision=14, scale=2, nullable=true)
*/
private $defaultsellprice;
/**
* #var boolean
*
* #ORM\Column(name="onwaycount", type="integer", nullable=false)
*/
private $onwaycount;
/**
* #var boolean
*
* #ORM\Column(name="instorecount", type="integer", nullable=false)
*/
private $instorecount;
/**
* #var boolean
*
* #ORM\Column(name="wayoutcount", type="integer", nullable=false)
*/
private $wayoutcount;
/**
* #var boolean
*
* #ORM\Column(name="instore", type="string", length=10, nullable=false)
*/
private $instore;
/**
* #var string
*
* #ORM\Column(name="isarchived", type="string", length=5, nullable=false,options={"default":false})
*/
private $isarchived;
/**
* #var string
*
* #ORM\Column(name="archivestatus", type="string", length=5, nullable=false,options={"default":true})
*/
private $archivestatus;
function __construct() {
$this->onwaycount=0;
$this->instore=FALSE;
$this->instorecount=0;
$this->wayoutcount=0;
}
/**
* Get id
*
* #return int
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Inventory
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set barcode
*
* #param string $barcode
*
* #return Inventory
*/
public function setBarcode($barcode) {
$this->barcode = $barcode;
return $this;
}
/**
* Get barcode
*
* #return string
*/
public function getBarcode() {
return $this->barcode;
}
/**
* Set description
*
* #param string $description
*
* #return Inventory
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription() {
return $this->description;
}
public function getImagename() {
return $this->imagename;
}
public function getCategory() {
return $this->category;
}
public function getCompanyId() {
return $this->companyId;
}
public function getCreated() {
return $this->created;
}
public function getUpdated() {
return $this->updated;
}
public function getOnwaycount() {
return $this->onwaycount;
}
public function getInstorecount() {
return $this->instorecount;
}
public function getWayoutcount() {
return $this->wayoutcount;
}
public function getInstore() {
return $this->instore;
}
public function setImagename($imagename) {
$this->imagename = $imagename;
return $this;
}
public function setCategory(\AppBundle\Entity\InventoryCategory $category) {
$this->category = $category;
return $this;
}
public function setCompanyId(\AppBundle\Entity\Company $companyId) {
$this->companyId = $companyId;
return $this;
}
public function setCreated(\DateTime $created) {
$this->created = $created;
return $this;
}
public function setUpdated(\DateTime $updated) {
$this->updated = $updated;
return $this;
}
public function setOnwaycount($onwaycount) {
$this->onwaycount = $onwaycount;
return $this;
}
public function setInstorecount($instorecount) {
$this->instorecount = $instorecount;
return $this;
}
public function setWayoutcount($wayoutcount) {
$this->wayoutcount = $wayoutcount;
return $this;
}
public function setInstore($instore) {
$this->instore = $instore;
return $this;
}
public function getDefaultsellprice() {
return $this->defaultsellprice;
}
public function setDefaultsellprice($defaultsellprice) {
$this->defaultsellprice = $defaultsellprice;
return $this;
}
public function getInventoryslug() {
return $this->inventoryslug;
}
public function setInventoryslug($inventoryslug) {
$this->inventoryslug = $inventoryslug;
return $this;
}
public function setTranslatableLocale($locale) {
$this->locale = $locale;
}
public function getIsarchived() {
return $this->isarchived;
}
public function getArchivestatus() {
return $this->archivestatus;
}
public function setIsarchived($isarchived) {
$this->isarchived = $isarchived;
return $this;
}
public function setArchivestatus($archivestatus) {
$this->archivestatus = $archivestatus;
return $this;
}
}
Found this not sure if it will help though (http://tocacar.com/2013/01/25/doctrine2-object-introspection/)
To get an array of metadata:
$cmf = $em->getMetadataFactory();
$metadata = $cmf->getMetadataFor(\AppBundle\Entity\Inventory::class);
//Doctrine\ORM\Mapping\ClassMetadata instance
//as array:
$metadata = (array) $metadata;
To get the inversed information:
$metadata->getAssociationMapping('companyId')['inversedBy'];
//as array
$metadata['associationMappings']['companyId']['inversedBy'];
You can find more info on the docs.

The Doctrine repository "Doctrine\ORM\EntityRepository" must implement UserProviderInterface in symfony2

I am getting above error when login. With property option in security.yml it is working. I have added the #ORM\Entity(repositoryClass="versionR\userBundle\Entity\UserRepository") line on my entity class, dobule checked path, cleared cache but no luck.
User entity class
<?php
namespace versionR\userBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
//use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="versionR\userBundle\Entity\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=40, nullable=false)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255, nullable=false)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255, nullable=false)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255, nullable=false)
*/
private $address;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="versionR\userBundle\Entity\Role", inversedBy="user")
* #ORM\JoinTable(name="user_role",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="role_id", referencedColumnName="id")
* }
* )
*/
private $role;
/**
* Constructor
*/
public function __construct()
{
$this->role = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #param string $address
*/
public function setAddress($address)
{
$this->address = $address;
}
/**
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* #param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* #param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* #param \Doctrine\Common\Collections\Collection $role
*/
public function setRole($role)
{
$this->role->add($role);
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getRole()
{
return $this->role;
}
/**
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/**
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/**
* Returns the roles granted to the user.
*/
public function getRoles()
{
return $this->role->toArray();
}
/**
*/
public function getSalt()
{
return null;
}
/**
*/
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
}
User repository class
<?php
namespace versionR\userBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository implements UserProviderInterface
{
/**
* #param string $username
* #return \versionR\userBundle\Entity\User
*/
public function loadUserByUsername($username)
{
return $this->findOneBy(array('username' => $username));
}
public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(
sprintf(
'Instances of "%s" are not supported.',
$class
)
);
}
return $this->find($user->getId());
}
public function supportsClass($class)
{
return $this->getEntityName() === $class
|| is_subclass_of($class, $this->getEntityName());
}
}
Both classes are in Entity directory. Any suggestions to solve this problem. I am using symfony 2.5.2

Symfony2 getId non-object error

Hy everybody, i've got a problem with my Bundle when I'm trying to obtain the ID of an user.
$user = $userManager->findUserById($tmpID['uid']);
Where $tmpID['uid'] is the ID of the user and when I'm trying to to $user->getId() I obtain an error.
This is the error
FatalErrorException: Error: Call to a member function getId() on a non-object in /Applications/MAMP/htdocs/Api/src/Api/ApiBundle/Controller/DefaultController.php line 263
MyBundle\Entity\User.php
<?php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
*/
protected $description;
/**
* #var integer
*
* #ORM\Column(name="genre", type="integer", nullable=false)
*/
protected $genre;
/**
* #var string
*
* #ORM\Column(name="device", type="string", nullable=true)
*/
protected $device;
/**
* #var string
*
* #ORM\Column(name="avatar", type="string", nullable=true)
*/
protected $avatar;
/**
* #var string
*
* #ORM\Column(name="phone", type="integer", nullable=true)
*/
protected $phone;
public function __construct()
{
parent::__construct();
// your own logic
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = $description;
}
public function getGenre() {
return $this->genre;
}
public function setGenre($genre) {
$this->genre = $genre;
}
public function getDevice() {
return $this->device;
}
public function setDevice($device) {
$this->device = $device;
}
public function getPhone() {
return $this->phone;
}
public function setPhone($phone) {
$this->phone = $phone;
}
public function getAvatar() {
return $this->avatar;
}
public function setAvatar($avatar) {
$this->avatar = $avatar;
}
}
While i was going to the office I get the solution! :)
On my function $tmpIDis an variable wich has the token of an user when that used login into Symfony. The problem it was I was using a token of an user wich didn't exist into the DDBB (Yes, i erased the user 5 minutes before to get that error)
Then, i was trying to obtain a non-existent user and that's the reason why i get NULL for $user
I hope my problem can help other people.
p.s. Thanks to #Javad and #George for the suggestions

symfony2 entity field type error

I haven't found any solid example on how to do this.
I have my entity Shield, which can have more than 1 ShieldTypes. What I want to do is, create a form that creates a new Shield with different Shieldtypes.
This is my code, I honestly don't know where is my error:
Error is:
Entities passed to the choice field must be managed
500 Internal Server Error - FormException
Armory\SearchBundle\Entity\Shield.php
namespace Armory\SearchBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Armory\SearchBundle\Entity\Shield
*
* #ORM\Table(name="shield")
* #ORM\Entity
*/
class Shield
{
/**
* #var integer $id
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $name
* #ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* #var integer $defense
* #ORM\Column(name="defense", type="integer", nullable=false)
*/
private $defense;
/**
* #var boolean $active
* #ORM\Column(name="active", type="boolean", nullable=false)
*/
private $active;
/**
* #ORM\ManyToMany(targetEntity="ShieldTypes")
* #ORM\JoinTable(name="i_orbs_type",
* joinColumns={#ORM\JoinColumn(name="id_orb", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="id_type", referencedColumnName="id")}
* )
* #var ArrayCollection $types
*/
protected $types;
public function __construct(){
$this->types = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getTypes(){
return $this->types;
}
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setDefense($defense)
{
$this->defense = $defense;
}
public function getDefense()
{
return $this->defense;
}
public function setActive($active)
{
$this->active = $active;
}
public function getActive()
{
return (bool)$this->active;
}
}
Armory\SearchBundle\Form\ShieldType.php:
namespace Armory\SearchBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ShieldType extends AbstractType{
public function buildForm(FormBuilder $builder, array $options){
$builder->add('name','text');
$builder->add('defense','integer');
$builder->add('active','checkbox');
$builder->add('types','entity',
array(
'class'=>'Armory\SearchBundle\Entity\ShieldTypes',
'query_builder'=> function($repository){
return $repository->createQueryBuilder('t')->orderBy('t.id', 'ASC');
},
'property'=>'name', )
);
}
public function getName(){
return 'shield';
}
public function getDefaultOptions(array $options){
return array('data_class'=>'Armory\\SearchBundle\\Entity\\Shield');
}
}
Armory\SearchBundle\Entity\ShieldTypes.php
namespace Armory\SearchBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SOA\CRMBundle\Entity\ShieldTypes
*
* #ORM\Table(name="orbs_type")
* #ORM\Entity
*/
class ShieldTypes
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* #var string $title
*
* #ORM\Column(name="title", type="string", length=45, nullable=false)
*/
private $title;
public function getId()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
}
It can be a little confusing but:
Shield entity (database)
ShieldTypes entity (database)
ShieldType form (abstracttype)
Thank you...
So the problem was that I didn't create a repository. I created it (see code at the end) and I still got 'Entities passed to the choice field must be managed' error.
Then I set mutiple to true in:
$builder->add('types','entity',
array(
'class'=>'Armory\SearchBundle\Entity\ShieldTypes',
'query_builder'=> function(\Armory\SearchBundle\Entity\Repository\ShieldTypesRepository $repository){
return $repository->createQueryBuilder('s')->orderBy('s.id', 'ASC');},
'property'=>'title',
'multiple'=>true
)
);
I hope someone finds this useful, cause if I had had this, it would been easier.
Code for my repository:
namespace Armory\SearchBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class ShieldTypesRepository extends EntityRepository
{
public function getAll()
{
return $this->_em->createQuery('SELECT s FROM Armory\SearchBundle\Entity\ShieldTypes s')
->getResult();
}
}

Categories