Hello I have several problems with the easyadmin multiple image uploads. Please help. Strangely I was having problems mostly with cakes and cakephotos. In easyadmin I add several images in Cakes, they get converted to array and stored in the db. After they get outputed when showed. Atleast that what I was planning to do...
Here is the Cakes.php entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* #ORM\Entity(repositoryClass="App\Repository\CakesRepository")
* #Vich\Uploadable
*/
class Cakes
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*/
private $CakeName;
/**
* #ORM\Column(type="integer")
*/
private $CakePrice;
/**
* #ORM\Column(type="string", length=255)
*/
private $Description;
/**
* #ORM\Column(type="json")
*/
private $CakeCategory = [];
/**
* #ORM\ManyToMany(targetEntity="Location", mappedBy="Cakes", cascade={"persist"})
*/
private $AvailableLocation = [];
/**
* #ORM\OneToMany(targetEntity="CakePhotos", mappedBy="Cake", cascade={"persist"})
*/
private $CakePhotos;
public function __construct()
{
$this->AvailableLocation = new ArrayCollection();
$this->CakePhotos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCakeName(): ?string
{
return $this->CakeName;
}
public function setCakeName(string $CakeName): self
{
$this->CakeName = $CakeName;
return $this;
}
public function getCakePrice(): ?int
{
return $this->CakePrice;
}
public function setCakePrice(int $CakePrice): self
{
$this->CakePrice = $CakePrice;
return $this;
}
public function getDescription(): ?string
{
return $this->Description;
}
public function setDescription(string $Description): self
{
$this->Description = $Description;
return $this;
}
public function getCakeCategory(): ?array
{
return $this->CakeCategory;
}
public function setCakeCategory(array $CakeCategory): self
{
$this->CakeCategory = $CakeCategory;
return $this;
}
/**
* #return Collection|Location[]
*/
public function getAvailableLocation(): Collection
{
return $this->AvailableLocation;
}
public function addAvailableLocation(Location $location): self
{
if (!$this->AvailableLocation->contains($location)) {
$this->AvailableLocation[] = $location;
$location->addCake($this);
}
return $this;
}
public function removeAvailableLocation(Location $location): self
{
if ($this->AvailableLocation->contains($location)) {
$this->AvailableLocation->removeElement($location);
$location->removeCake($this);
}
return $this;
}
/**
* #return Collection|CakePhoto[]
*/
public function getCakePhotos(): Collection
{
return $this->CakePhotos;
}
public function addCakePhoto(CakePhotos $CakePhotos): self
{
if (!$this->CakePhotos->contains($CakePhotos)) {
$this->CakePhoto[] = $CakePhotos;
$CakePhotos->setCake($this);
}
return $this;
}
public function removeCakePhoto(CakePhotos $CakePhotos): self
{
if ($this->CakePhotos->contains($CakePhotos)) {
$this->CakePhotos->removeElement($CakePhotos);
}
return $this;
}
public function getImageURL(): ?string
{
return $this->ImageURL;
}
public function setImageURL(string $ImageURL): self
{
$this->ImageURL = $ImageURL;
return $this;
}
}
Here is the CakePhotos.php entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* #ORM\Entity(repositoryClass="App\Repository\CakePhotosRepository")
* #Vich\Uploadable
*/
class CakePhotos
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $Image;
/**
* #Vich\UploadableField(mapping="cakephotos", fileNameProperty="Image")
*/
private $ImageFile;
/**
* #ORM\ManyToOne(targetEntity="Cakes", inversedBy="CakePhotos")
*/
private $Cake;
public function getId(): ?int
{
return $this->id;
}
public function getImage(): ?string
{
return $this->Image;
}
public function setImage(?string $Image)
{
$this->Image = $Image;
return $this;
}
public function getCake(): ?Cakes
{
return $this->Cake;
}
public function setCake(?Cake $cake): self
{
$this->Cake = $cake;
return $this;
}
public function __toString()
{
return $this->Image;
}
/**
* #return mixed
*/
public function getImageFile()
{
return $this->ImageFile;
}
/**
* #param mixed $imageFile
* #throws \Exception
*/
public function setImageFile(?File $ImageFile): void
{
$this->ImageFile = $ImageFile;
}
}
Here is my Form for converting
<?php
namespace App\Form;
use App\Entity\CakePhotos;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ImageFile', VichFileType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CakePhotos::class,
]);
}
}
All around the code I have different problems such as:
Argument 1 passed to App\Entity\CakePhotos::setCake() must be an instance of App\Entity\Cake or null, instance of App\Entity\Cakes given, called in /home/kumaskano1/Desktop/Learning/Symfony/Shirin/src/Entity/Cakes.php
you can solve your problem by doing this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ImageFile', VichFileType::class,array('data_class' =>null))
;
}
Related
I there,
I'm trying to use api platform with symfony 4 but I'm unable to load related entities of an object even using normalizationContext and Groups.
For example, I have 2 classes FieldType and Field. Each Field have a single one FieldType. To load the Field with the fieldtype I created the "field" group which I use in Field to load all the propeties and also in FieldType to also load the field type properties. But at the end I still get the IRI in the returned JSON.
Can someone help me to figure out what is wrong?
Here is the code for the 2 classes:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\AUDFieldRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiSubresource;
/**
* #ApiResource(
* normalizationContext={"groups"={"field:read"}},
* )
* #ORM\Entity(repositoryClass=AUDFieldRepository::class)
* #ORM\Table(name="aud_field")
*/
class AUDField
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
* #Groups("field:read")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, unique=true)
* #Groups({"field:read"})
*/
private $name;
/**
* #ORM\Column(type="boolean")
*/
private $optional = false;
/**
* #ORM\Column(type="string", length=40, nullable=true)
* #Groups({"field:read"})
*/
private $inputType;
/**
* #ORM\Column(type="text", nullable=true)
* #Groups({"field:read"})
*/
private $inputValues;
/**
* #var array
* #ORM\Column(type="json_array", nullable=true)
* #Groups({"field:read"})
*/
private $specifications;
/**
* #ORM\ManyToOne(targetEntity=AUDFieldType::class)
* #ORM\JoinColumn(nullable=false)
* #Groups({"field:read"})
*/
private $type;
/**
* #ORM\ManyToMany(targetEntity=AUDAttributeType::class, mappedBy="fields")
* #Groups({"field:read"})
*/
private $attributesTypes;
public function __construct()
{
$this->attributesTypes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getOptional(): bool
{
return $this->optional;
}
public function isOptional(): bool
{
return $this->optional;
}
public function setOptional(bool $bool): self
{
$this->optional = $bool;
return $this;
}
public function getSpecifications(): ?array
{
return $this->specifications;
}
public function getSpecification($key): ?array
{
return json_decode($this->specifications[$key]);
}
public function setSpecifications(?array $specs): self
{
$this->specifications = $specs;
return $this;
}
public function setSpecification($key, $value): ?array
{
return $this->specifications[$key] = $value;
}
public function hasSpecification($key): bool
{
return array_key_exists($key, $this->specifications);
}
/*public function addSpecification($key, $value): self
{
$this->specifications[$key] = $value;
return $this;
}
public function removeSpecification($key): self
{
unset($this->specifications[$key]);
return $this;
}*/
public function getInputValues(): ?string
{
return $this->inputValues;
}
public function setInputValues(?string $inputValues): self
{
$this->inputValues = $inputValues;
return $this;
}
public function getInputType(): ?string
{
return $this->inputType;
}
public function setInputType(?string $inputType): self
{
$this->inputType = $inputType;
return $this;
}
public function getType(): ?AUDFieldType
{
return $this->type;
}
public function setType(?AUDFieldType $type): self
{
$this->type = $type;
return $this;
}
/**
* #return Collection|AUDAttributeType[]
*/
public function getAttributesTypes(): Collection
{
return $this->attributesTypes;
}
public function addAttributesType(AUDAttributeType $attributesType): self
{
if (!$this->attributesTypes->contains($attributesType)) {
$this->attributesTypes[] = $attributesType;
$attributesType->addField($this);
}
return $this;
}
public function removeAttributesType(AUDAttributeType $attributesType): self
{
if ($this->attributesTypes->removeElement($attributesType)) {
$attributesType->removeField($this);
}
return $this;
}
}
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Repository\AUDFieldTypeRepository;
/**
* #ApiResource(normalizationContext={"groups"={"field:read"}})
* #ORM\Entity(repositoryClass=AUDFieldTypeRepository::class)
* #ORM\Table(name="aud_field_type")
*/
class AUDFieldType
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
* #Groups({"field:read"})
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
* #Groups({"field:read"})
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
End the Postman output for http://127.0.0.1:8000/api/field/1:
{
"#context": "/api/contexts/AUDField",
"#id": "/api/field/1",
"#type": "AUDField",
"id": 1,
"name": "Identifiant",
"specifications": {
"minlength": 4
},
"type": "/api/fieldtype/1",
"attributesTypes": [
"/api/attributetype/1"
]
}
And for http://127.0.0.1:8000/api/fieldtype/1:
{
"#context": "/api/contexts/AUDFieldType",
"#id": "/api/fieldtype/1",
"#type": "AUDFieldType"
}
Try to remove normalizationContext in AUDFieldType class or rename it with field-type:read.
I have a user entity that has a many-to-many relationship with another entity Roles.
I have a form that allows me to create a new user and for that, I use an EntityType for the manyToMany relationship 'roles'.
Here is my form
$builder
->add('username',null,['label' => 'Email'])
->add('password',null,['label'=>'Password'])
->add('roles',EntityType::class, [
'multiple' => true,
'class' => Role::class,
'choice_label' => 'role_name',
]
)
;
Everything works fine but when I submit the form I got this error:
Expected argument of type "App\Entity\Role", "string" given at
property path "roles".
EDIT:
here is my User entity :
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* #ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $username;
/**
* #ORM\Column(type="string", length=255)
*/
private $password;
/**
* #ORM\ManyToMany(targetEntity=Role::class, inversedBy="users")
*/
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getRoles()
{
return ['ROLE_ADMIN'];
}
public function getSalt()
{
return null;
}
public function eraseCredentials()
{
}
public function addRole(Role $role): self
{
if (!$this->roles->contains($role)) {
$this->roles[] = $role;
$role->addUser($this);
}
return $this;
}
public function removeRole(Role $role): self
{
if ($this->roles->contains($role)) {
$this->roles->removeElement($role);
$role->removeUser($this);
}
return $this;
}
}
Here is my Role entity :
<?php
namespace App\Entity;
use App\Repository\RoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=RoleRepository::class)
*/
class Role
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $role_name;
/**
* #ORM\ManyToMany(targetEntity=User::class, mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getRoleName(): ?string
{
return $this->role_name;
}
public function setRoleName(string $role_name): self
{
$this->role_name = $role_name;
return $this;
}
/**
* #return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
}
return $this;
}
}
I guess the problem is here
public function getRoles()
{
return ['ROLE_ADMIN'];
}
you should return an array or Role entity not string, it should be something like this
public function getRoles()
{
return $this->roles;
}
what if i'm using ArrayCollection in User entity?
/**
* #var Collection|Role[]
* #ORM\ManyToMany(targetEntity="App\Entity\Role")
* #ORM\JoinTable(
* name="user_roles",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
private $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getRoles()
{
return $this->roles;
}
I try to make a form for an entity called Product with an embedded form from an entity Barcode. When I edit an existing product and remove a barcode, it is still in the database with a product_id set to NULL. How can I do to remove it completely ?
My Barcode Entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\BarcodeRepository")
*/
class Barcode
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $code;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="barcodes")
* #ORM\JoinColumn(nullable=false)
*/
private $product;
public function __construct(Product $product = null)
{
$this->product = $product;
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
}
My Product Entity :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity()
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #UniqueEntity("slug")
*/
class Product
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*/
protected $name;
/**
* #ORM\Column(type="string", length=255)
* #Assert\Regex(pattern="/\.(svg|png)$/")
*/
protected $picture;
/**
* #ORM\Column(type="boolean")
*/
protected $is_activated;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $comments;
/**
* #ORM\Column(type="datetime")
*/
protected $created_at;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Barcode", mappedBy="product", orphanRemoval=true, cascade={"all"})
*/
protected $barcodes;
public function __construct()
{
$this->allergens = new ArrayCollection();
$this->barcodes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPicture(): ?string
{
return $this->picture;
}
public function setPicture(string $picture): self
{
$this->picture = $picture;
return $this;
}
public function getIsActivated(): ?bool
{
return $this->is_activated;
}
public function setIsActivated(bool $is_activated): self
{
$this->is_activated = $is_activated;
return $this;
}
public function getComments(): ?string
{
return $this->comments;
}
public function setComments(?string $comments): self
{
$this->comments = $comments;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
/**
* #return Collection|Barcode[]|null
*/
public function getBarcodes(): ?Collection
{
return $this->barcodes;
}
public function addBarcode(Barcode $barcode): self
{
if (!$this->barcodes->contains($barcode)) {
$this->barcodes[] = $barcode;
$barcode->setProduct($this);
}
return $this;
}
public function removeBarcode(Barcode $barcode): self
{
if ($this->barcodes->contains($barcode)) {
$this->barcodes->removeElement($barcode);
// set the owning side to null (unless already changed)
if ($barcode->getProduct() === $this) {
$barcode->setProduct(null);
}
}
return $this;
}
}
and my ProductType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('slug')
->add('picture')
->add('barcodes', CollectionType::class, [
'entry_type' => BarcodeType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
])
->add('is_activated')
->add('comments')
;
}
I try to make a form for an entity called Product with an embedded form from an entity Barcode. When I try to go to the form to add one product, I have the message "Return value of App\Entity\Product::getBarcodes() must implement interface Doctrine\Common\Collections\Collection, null returned".
I say in __construct to initialize the barcodes to implement the Collection but still the same..
My Barcode Entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\BarcodeRepository")
*/
class Barcode
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $code;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="barcodes")
* #ORM\JoinColumn(nullable=false)
*/
private $product;
public function __construct(Product $product = null)
{
$this->product = $product;
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
return $this;
}
}
My Product Entity :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity()
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #UniqueEntity("slug")
*/
class Product
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*/
protected $name;
/**
* #ORM\Column(type="string", length=255)
* #Assert\Regex(pattern="/\.(svg|png)$/")
*/
protected $picture;
/**
* #ORM\Column(type="boolean")
*/
protected $is_activated;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
protected $comments;
/**
* #ORM\Column(type="datetime")
*/
protected $created_at;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Barcode", mappedBy="product", orphanRemoval=true, cascade={"all"})
*/
protected $barcodes;
public function __construct()
{
$this->allergens = new ArrayCollection();
$this->barcodes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPicture(): ?string
{
return $this->picture;
}
public function setPicture(string $picture): self
{
$this->picture = $picture;
return $this;
}
public function getIsActivated(): ?bool
{
return $this->is_activated;
}
public function setIsActivated(bool $is_activated): self
{
$this->is_activated = $is_activated;
return $this;
}
public function getComments(): ?string
{
return $this->comments;
}
public function setComments(?string $comments): self
{
$this->comments = $comments;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->created_at = $created_at;
return $this;
}
/**
* #return Collection|Barcode[]
*/
public function getBarcodes(): Collection
{
return $this->barcodes;
}
public function addBarcode(Barcode $barcode): self
{
if (!$this->barcodes->contains($barcode)) {
$this->barcodes[] = $barcode;
$barcode->setProduct($this);
}
return $this;
}
public function removeBarcode(Barcode $barcode): self
{
if ($this->barcodes->contains($barcode)) {
$this->barcodes->removeElement($barcode);
// set the owning side to null (unless already changed)
if ($barcode->getProduct() === $this) {
$barcode->setProduct(null);
}
}
return $this;
}
}
and my ProductType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('slug')
->add('picture')
->add('barcodes', CollectionType::class, [
'entry_type' => BarcodeType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
])
->add('is_activated')
->add('comments')
;
}
In your Product entity, allow the possibility of a null return type by changing Collection to ?Collection:
/**
* #return Collection|Barcode[]|null
*/
public function getBarcodes(): ?Collection
{
return $this->barcodes;
}
After looking over the internet and here I didn' find something useful for me.
I'm currently creating a CRUD. Every 'Entreprise' having one or multiple 'Site' and I'm currently doing the CRUD for Site. I've made it by doing the make:form command.
Whhen I'm going to create a site the following error appear :
Catchable Fatal Error: Object of class App\Entity\Entreprise could not
be converted to string
I've tried to add the function __toString() as i saw. But maybe i didn't add it crrectly it changes nothing so I removed it.
My controller to create a site looks like this :
/**
* #Route("admin/sites/new", name="admin.sites.new")
* #param Request $request
* #return RedirectResponse|Response
*/
public function new (Request $request)
{
$site = new Site();
$form = $this->createForm(SiteType::class, $site);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$this->em->persist($site);
$this->em->flush();
$this->addFlash('success', 'Site crée avec succès');
return $this->redirectToRoute('admin.sites.index');
}
return $this->render('admin/sites/create.html.twig', [
'site' => $site,
'form' => $form->createView()
]);
}
}
My SiteType generate by the make:form commande :
/**
* #Route("admin/sites/new", name="admin.sites.new")
* #param Request $request
* #return RedirectResponse|Response
*/
public function new (Request $request)
{
$site = new Site();
$form = $this->createForm(SiteType::class, $site);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$this->em->persist($site);
$this->em->flush();
$this->addFlash('success', 'Site crée avec succès');
return $this->redirectToRoute('admin.sites.index');
}
return $this->render('admin/sites/create.html.twig', [
'site' => $site,
'form' => $form->createView()
]);
}
}
So here are my ENTITY
Entreprise
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\EntrepriseRepository")
*/
class Entreprise
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $entreprise_nom;
/**
* #ORM\Column(type="string", length=255)
*/
private $entreprise_siret;
/**
* #ORM\Column(type="string", length=10)
*/
private $entreprise_telephone;
/**
* #ORM\Column(type="string", length=255)
*/
private $entreprise_salesforce_number;
/**
* #ORM\Column(type="string", length=255)
*/
private $entreprise_compte_client;
/**
* #ORM\Column(type="string", length=255)
*/
private $entreprise_raison_sociale;
/**
* #ORM\Column(type="string", length=255)
*/
private $entreprise_APE;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $entreprise_image_link;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Site", mappedBy="entreprise_id")
*/
private $entreprise_id;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Catalogue", mappedBy="entreprise_id")
*/
private $entreprise_catalogue_id;
public function __construct()
{
$this->entreprise_id = new ArrayCollection();
$this->entreprise_catalogue_id = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEntrepriseNom(): ?string
{
return $this->entreprise_nom;
}
public function setEntrepriseNom(string $entreprise_nom): self
{
$this->entreprise_nom = $entreprise_nom;
return $this;
}
public function getEntrepriseSiret(): ?string
{
return $this->entreprise_siret;
}
public function setEntrepriseSiret(string $entreprise_siret): self
{
$this->entreprise_siret = $entreprise_siret;
return $this;
}
public function getEntrepriseTelephone(): ?int
{
return $this->entreprise_telephone;
}
public function setEntrepriseTelephone(int $entreprise_telephone): self
{
$this->entreprise_telephone = $entreprise_telephone;
return $this;
}
public function getEntrepriseSalesforceNumber(): ?string
{
return $this->entreprise_salesforce_number;
}
public function setEntrepriseSalesforceNumber(string $entreprise_salesforce_number): self
{
$this->entreprise_salesforce_number = $entreprise_salesforce_number;
return $this;
}
public function getEntrepriseCompteClient(): ?string
{
return $this->entreprise_compte_client;
}
public function setEntrepriseCompteClient(string $entreprise_compte_client): self
{
$this->entreprise_compte_client = $entreprise_compte_client;
return $this;
}
public function getEntrepriseRaisonSociale(): ?string
{
return $this->entreprise_raison_sociale;
}
public function setEntrepriseRaisonSociale(string $entreprise_raison_sociale): self
{
$this->entreprise_raison_sociale = $entreprise_raison_sociale;
return $this;
}
public function getEntrepriseAPE(): ?string
{
return $this->entreprise_APE;
}
public function setEntrepriseAPE(string $entreprise_APE): self
{
$this->entreprise_APE = $entreprise_APE;
return $this;
}
public function getEntrepriseImageLink(): ?string
{
return $this->entreprise_image_link;
}
public function setEntrepriseImageLink(?string $entreprise_image_link): self
{
$this->entreprise_image_link = $entreprise_image_link;
return $this;
}
/**
* #return Collection|Site[]
*/
public function getEntrepriseId(): Collection
{
return $this->entreprise_id;
}
public function addEntrepriseId(Site $entrepriseId): self
{
if (!$this->entreprise_id->contains($entrepriseId)) {
$this->entreprise_id[] = $entrepriseId;
$entrepriseId->setEntrepriseId($this);
}
return $this;
}
public function removeEntrepriseId(Site $entrepriseId): self
{
if ($this->entreprise_id->contains($entrepriseId)) {
$this->entreprise_id->removeElement($entrepriseId);
// set the owning side to null (unless already changed)
if ($entrepriseId->getEntrepriseId() === $this) {
$entrepriseId->setEntrepriseId(null);
}
}
return $this;
}
}
And here is
Site
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\SiteRepository")
*/
class Site
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $site_nom;
/**
* #ORM\Column(type="string", length=255)
*/
private $site_raison_sociale;
/**
* #ORM\Column(type="string", length=255)
*/
private $site_APE;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Client", mappedBy="site_id")
*/
private $site_id;
/**
* #ORM\OneToOne(targetEntity="App\Entity\Adresse", cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=false)
*/
private $adresse_id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Entreprise", inversedBy="entreprise_id")
* #ORM\JoinColumn(nullable=false)
*/
private $entreprise_id;
public function __construct()
{
$this->site_id = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getSiteNom(): ?string
{
return $this->site_nom;
}
public function setSiteNom(string $site_nom): self
{
$this->site_nom = $site_nom;
return $this;
}
public function getSiteRaisonSociale(): ?string
{
return $this->site_raison_sociale;
}
public function setSiteRaisonSociale(string $site_raison_sociale): self
{
$this->site_raison_sociale = $site_raison_sociale;
return $this;
}
public function getSiteAPE(): ?string
{
return $this->site_APE;
}
public function setSiteAPE(string $site_APE): self
{
$this->site_APE = $site_APE;
return $this;
}
/**
* #return Collection|Client[]
*/
public function getSiteId(): Collection
{
return $this->site_id;
}
public function addSiteId(Client $siteId): self
{
if (!$this->site_id->contains($siteId)) {
$this->site_id[] = $siteId;
$siteId->addSiteId($this);
}
return $this;
}
public function removeSiteId(Client $siteId): self
{
if ($this->site_id->contains($siteId)) {
$this->site_id->removeElement($siteId);
$siteId->removeSiteId($this);
}
return $this;
}
public function getAdresseId(): ?Adresse
{
return $this->adresse_id;
}
public function setAdresseId(Adresse $adresse_id): self
{
$this->adresse_id = $adresse_id;
return $this;
}
public function getEntrepriseId(): ?Entreprise
{
return $this->entreprise_id;
}
public function setEntrepriseId(?Entreprise $entreprise_id): self
{
$this->entreprise_id = $entreprise_id;
return $this;
}
public function __toString()
{
return $this->getSiteNom();
}
}
I didn't know what's wrong. Maybe the __toString I didn't write correclty !
I've wrote :
public function __toString()
{
return $this->getSiteNom();
}
}
Catchable Fatal Error: Object of class App\Entity\Entreprise
You need to implement the __toString() method in the Entreprise entity
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\EntrepriseRepository")
*/
class Entreprise
{
//...
public function __toString()
{
return $this->entreprise_nom;
}
// ...
}