in my symfony project I have an entity that has this field:
/**
* Solde
* #ORM\Column(type="float", nullable=true)
*/
private $solde;
This field can be either null, equal to 0 or greater than 0 but can not be negative.
So I wonder how I can organize the asserts for this field?
Can I do something like this?
/**
* #Assert\!Negative
*/
Otherwise the asserts I should use would be:
/ **
* #Assert \ Null ()
* #Assert\GreaterThanOrEqual(0)
* /
But if I use both, it may be a problem because one must make sure that the value is zero, and the other must verify that it is positive or equal to 0
If you are using Symfony 4.3+ you can use PositiveOrZero:
/**
* #Assert\PositiveOrZero
*/
If you combine this with nullable=true, all positive numbers, 0 and null will be valid.
For older versions of Symfony you can use:
/**
* #Assert\GreaterThanOrEqual(0)
*/
Again with nullable=true and you will have the same effect.
or you can create your own validator with its set of logic
enter link description here
Related
I have entity with uniq field, inviteCode. And when I create new entity I want set automatic some random code, but this code must be different from exist in db, what do you thing, what practices about that you know ?
/**
* #ORM\Table(name="users")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #AssertBridge\UniqueEntity(
* groups={"registration"},
* fields="inviteCode",
* errorPath="not valid",
* message="This inviteCode is already in use."
* )
*/
class User extends AbstractUser implements UserInterface
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=500, unique=true)
* #Annotation\SerializedName("_invite_code")
*/
private $inviteCode;
I found something like that
private function calculateReference($number)
{
$multipliers = array(7,3,1);
$length = strlen($number);
$numberArr = str_split($number);
$sum = 0;
for ($i = $length - 1; $i >= 0; --$i)
{
$sum += $numberArr[$i] * $multipliers[($length - 1 - $i) % 3];
}
return $number.(10 - $sum % 10) % 10;
}
first get max id from table then call function calculateReference with id and then setInviteCode.
But I believe doctrine have something exist for this issue or maybe somebody have good example for this
Someone provided a great answer here https://stackoverflow.com/a/13917309/4173130.
But like he said at the end, you don't need doctrine for such a simple feature. Generating the code in the constructor is an efficient, simple and clear solution.
You can use a UUID library like ramsey/uuid. Then you would be able to generate any random code with Uuid::uuid4();.
Another solution is to use random_bytes() with base64_encode : base64_encode(random_bytes(32)).
Please don't try to create a new function to generate random values. Most of time it is not secure, see https://www.owasp.org/index.php/Insecure_Randomness.
Why not using a uuid? It is included in php as a core function and i believe it suits your needs.
Check in the official documentation here
Trying to validate if one field is not empty (length > 0) then the length of the field being validated must be a certain length (2 characters). It seems like an "Assert\Expression" might work in this situation but I am having trouble trying to find the length of the properties. It seems like you cannot call php functions within the Expression. The expression documentation mentions functions but maybe I do not understand it... Do I need to register my own function that simply return a strlen(). If so how do you register your own functions? Can someone explain if there is a way to do this, or maybe there is a better way than using Expression that I am overlooking...
/**
* #var string
*
* #ORM\Column(name="plate", type="string", length=10)
*/
private $plate;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=2)
* #Assert\Expression(
* "strlen(this.getPlate()) == 0 or (strlen(this.getPlate()) > 0 and strlen(value) == 2)",
* message="Must be 2 characters"
* )
*/
private $state;
In the above case I get an error The function "strlen" does not exist around position 1
Looks like you will need to register your own function. Have a look at the docs: https://symfony.com/doc/current/components/expression_language/extending.html#registering-functions
There is an example on lowercase, strlen should be very similar.
EDIT:
You can also use a callback validator.
/**
* #Assert\Callback()
*/
public function validateState(ExecutionContextInterface $context)
{
if (!empty($this->plate) && mb_strlen($this->state) !== 2) {
$context->buildViolation('State must be 2 characters long')
->atPath('state')
->addViolation();
}
}
But if you are planning to using this kind of validation in multiple places, you can write and register your own validator.
I would like to make uniqueConstraint user and lesson.
/**
* #ORM\Table(name="ReviewSchool",uniqueConstraints={
* #ORM\UniqueConstraint(name="lessonid", columns={"lesson", "user"})})
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class ReviewSchool
{
* #ORM\ManyToOne(targetEntity="Lesson",inversedBy="reviewschool")
* #ORM\JoinColumn(name="review_lesson", referencedColumnName="id",onDelete="cascade")
*/
private $lesson;
/**
*
* #ORM\ManyToOne(targetEntity="User",inversedBy="reviewschool")
* #ORM\JoinColumn(name="review_user",referencedColumnName="id",onDelete="cascade")
*/
private $user;
However it shows
[Doctrine\DBAL\Schema\SchemaException]
There is no column with name 'lesson' on table 'ReviewSchool'.
Surely I have 'lesson' column, how can I solve this?
I have misunderstood something??
It allows to hint the SchemaTool to generate a database unique constraint on the specified table columns. It only has meaning in the SchemaTool schema generation context.
So you have to use column names. In your case:
#ORM\UniqueConstraint(columns={"review_lesson", "review_user"})}
In Doctrine2, I can write the following if I want to create single-column indexes for a set of columns in an entity:
/**
* Project
*
* #ORM\Entity(repositoryClass="Company\SomeBundle\Entity\ProjectRepository")
* #ORM\Table(indexes={
* #ORM\Index(name="name_idx", columns={"name"}),
* #ORM\Index(name="started_idx", columns={"started"}),
* })
*/
However, what if I wanted to create an index for each column in this entity? Is there a shorthand for this, so that I don't have to write it explicitly for each column like in the above example?
Just list properties that should be indexed.
<?php
/**
* #Entity
* #Table(name="ecommerce_products",indexes={#index(name="search_idx",columns={"name","email"})})
*/
class ECommerceProduct
{
}
I have a problem with serializing entity with many relations using groups.
I have a problem with serializing related entities this way.
Let's say I have two entities: Product and related Element.
/**
*
* #Serializer\ExclusionPolicy("none")
*/
class Product {
/**
* Primary key
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*
* #Serializer\Groups({"list","details"})
* #Serializer\Type("integer")
*/
protected $id;
/**
* #Serializer\Groups({"list","details"})
* #Serializer\Type("string")
*/
protected $name;
/**
* #ORM\Column(name="description", type="string", length=4096, nullable=true)
*
* #Serializer\Groups({"details"})
* #Serializer\Type("string")
*/
protected $description;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="Madden\ProjectBundle\Entity\ProjectResource", mappedBy="project")
* #Serializer\Groups({"details"})
* #Serializer\Type("ArrayCollection<Element>")
*/
protected $details1;
/**
* Relation to project tasks
* #ORM\OneToMany(targetEntity="Madden\ProjectBundle\Entity\ProjectTask", mappedBy="project")
* #Serializer\Exclude()
* #Serializer\Type("ArrayCollection<Element>")
*/
protected $details2;
...
}
Element entity has a similar structure:
/**
*
* #Serializer\ExclusionPolicy("none")
*/
class Element {
/**
* Primary key
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*
* #Serializer\Groups({"list","details"})
* #Serializer\Type("integer")
*/
protected $id;
/**
* #Serializer\Groups({"list","details"})
* #Serializer\Type("string")
*/
protected $name;
/**
* #ORM\Column(name="description", type="string", length=4096, nullable=true)
*
* #Serializer\Groups({"details"})
* #Serializer\Type("string")
*/
protected $description;
...
}
My problem is that when I'm serializing Product with 'details' group entity I want to serialize only id's of Elements but as you see entity has defined same groups as Product (in case that I would need details of element object) because I want have unified groups on all my entities and prevent making hundreds of groups like 'product_details','element_details', and so on.
Is there a way to eventualy change serialization group when I visit relation or something like that? Handler maybe or something like that?
Regards and thanks for any help
Unfortunately, you can't really (but keep reading ;-)), well at least not without changes to the serializer library. The culprit is that the list of groups is fixed within a GroupExclusionStrategy (which is referenced by the Context) the minute you start the serialization process. There is actually an assertion within the code that prevents modification of the exclusion strategy once the (de-)serialization is running.
But as it happens, I had the exact same problem in a project of mine as well, and I hacked the necessary changes into the serializer code. I have cleaned the code up a bit and uploaded it to Github (https://github.com/andreasferber/serializer/tree/recursion-groups).
It adds new property metadata with which you can add, remove or override the groups when descending into subobjects. With annotations it looks like this:
/**
* #Serializer\RecursionGroups(set={"foo", "bar"}, add={"baz"}, remove={"Default"})
*/
private $myProperty;
You should be able to use XML or Yaml metadata as well, however this is untested since I don't use them and I haven't added test cases yet. Have a look at the reference documentation. Since I haven't done any optimizations yet either, if your entities are really large and deeply nested, it might have a noticable performance impact.
Please let me know if you find this useful, or if you have any suggestions, because if this isn't only needed by me, I will add some tests and try to submit it upstream.
A solution for this is actually described in the official documentation.
That being said the solution proposed by #aferber seems better on many points: easier to maintain, less verbose, more flexible...
You need to use setGroups.
The _group suffix used in the official documentation is not needed.
$context->setGroups([
'Default', //if you want
// use this linked entity but show only its id
'group_of_linked_field',
'group_of_linked_field' => [
'id' // you will need to define this group first
],
// use this linked entity and show fields as described
'group_of_other_linked_field',
'group_of_other_linked_field' => [
// just as an example
'Default',
'details',
],
]);
This does not work with addGroup or addGroups! Both of them won't accept associative arrays. setGroups is your (only?) solution.