How to convert smallint to boolean in Symfony Form? - symfony

I have an entity that contains a smallint value:
#[ORM\Column(type: 'smallint')]
private $isActive;
And when I want to generate a form with $form = $this->createForm(UserTestType::class, $user) that uses CheckBox to represent this isActive value, I get the following error:
Unable to transform value for property path "isActive": Expected a Boolean.
Is there a way to solve this?

Related

Symfony choice type array : Unable to transform value for property path : Expected an array

First of all, I'm a new user of Symfony
And actually I'm customing my form EasyAdmin with some fields and I have an issue with this one :
ChoiceField::new('villa')->setChoices($villasChoices)->allowMultipleChoices()
I get this error because of the allowMultipleChoices() func :
Unable to transform value for property path "villa": Expected an array.
My field is actually a collection type, That's why I have this error, there is my entity
#[ORM\OneToMany(mappedBy: 'Name', targetEntity: Villa::class)]
private Collection $Villa;
public function __construct()
{
$this->Villa = new ArrayCollection();
}
/**
* #return Collection<int, Villa>
*/
public function getVilla(): Collection
{
return $this->Villa;
}
How can I remplace Collection type by Array ?
Try to use AssociationField instead of ChoiceField.
AssociationField list entities automatically, while ChoiceField is made to pass array manually.
->setFormTypeOption('multiple', true)
Will do the trick for multiple choice, if your property allow multiples values (OneToMany, ManyToMany)
You must have a form like this:
$form = $this->createFormBuilder($yourEntity)
->add('villa', EntityType::class, [
'class' => Villa::class
'multiple' => true
])
;
Set the 'villa' item with EntityType::class and set in the options 'multiple' => true.
In your Villa entity you must set a __tostring method like this:
public function __toString(): string
{
return $this->name; //name is a string value
}

Regular expression doesn't work in class entity symfony

There are two entities: Product and ProductDetails:
class ProductDetail {
#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'productDetail')]
#[ORM\JoinColumn(nullable: false)]
private $productItem;
#[ORM\Column(type: 'date', nullable: true)]
#[Assert\GreaterThan('today')]
#[Assert\Expression(
expression: 'this.getProductItem()->getStatus() in ['not done'] or value',
message: 'The planification date cannot be null for this status!',
)]
private $finishedDate;
class Product {
#[ORM\Column(type: 'string', length: 255)]
private $status;
Basically I want to create a constraint for the finishedDate to not allow to be empty when the status is 'not done'.
But it doesn't do absolutely nothing and I'm curious if I'm missing anything. Even if I put value there and the date is empty the message is not triggered.
According to this link : https://symfony.com/doc/current/components/expression_language/syntax.html.
Could you try :
this.getProductItem().getStatus()
You can use a custom constraint in your case, it's more flexible :
https://symfony.com/doc/current/validation/custom_constraint.html

Gedmo doesn't fill dates on record flush

I am using Gedmo for it's Timestampable options, to automatically add created_at and updated_at dates but getting error:
Integrity constraint violation: 1048 Column 'created_at' cannot be null
Why they are not filled? That's how I am implementing it:
#[ORM\Column]
#[Gedmo\Timestampable(on: 'create')]
private ?\DateTime $createdAt;
#[ORM\Column]
#[Gedmo\Timestampable]
private ?\DateTime $updatedAt;
Doing the things as per the documentation. Am I missing something ?

Api Platform property types are always STRING

Despite my efforts with type hinting and #var int annotations, Api Platform’s interpretation of this property is always string.
Consider this property:
/**
* #var int $geonameId The ID of a Geonames.org city/place/town
*
* #ORM\Column(type="bigint", options={"unsigned": true}, nullable=true)
* #Assert\Type(type="int", message="Geoname id must be an integer")
*/
protected $geonameId;
public function getGeonameId(): ?int
{
return $this->geonameId;
}
public function setGeonameId(int $geonameId = null): self
{
$this->geonameId = $geonameId;
return $this;
}
I get the following schema out from Api Platform:
geonameId string
nullable: true
The ID of a Geonames.org city/place/town
And when I try submit "geonameId": 123456 I get the following error:
The type of the "geonameId" attribute for class <Entity> must be one of "string" ("int" given).
I know about the #ApiProperty annotation to customise attributes etc. but I don't want to have to verbosely define the type just to accept an integer when I have already used type hinting and/or #var annotation.
Does anybody see anything wrong with what I am doing / expecting? Thanks in advance...
Looks like Doctrine maps a BIGINT as a string.
Ref:
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/basic-mapping.html
bigint: Type that maps a database BIGINT to a PHP string.

Decimal return string in Doctrine

I have a field Decimal like this
/**
*
* #ORM\Column(name="foo", type="decimal", precision=0, scale=2, nullable=false, unique=false)
*/
private $foo;
When I use getFoo() or QueryBuilder my value is "159.79" instead of 159.79.
How can I get the correct type?
Even though you have declared it as a decimal in your Doctrine annotation, when it is retrived PHP will treat it as a string.
Reference here.
Values retrieved from the database are always converted to PHP’s
string type or null if no data is present.
You could change the getter to cast as a float and then return.
E.g.
public function getFoo()
{
return (float) $this->foo;
}
or
public function getFoo()
{
return floatval($this->foo);
}
Look at Type Juggling for more info.
More info on floatVal() here

Resources