Timestampable Trait of Gedmo Doctrine extension throws error - doctrine

I got the following entity
use Gedmo\Timestampable\Traits\TimestampableEntity;
/**
* #ORM\Entity
* #ORM\Table(name="users")
* #UniqueEntity(fields="email", message="Email already taken")
*/
class User
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
...
if i run
$ php app/console doctrine:mapping:info
i get this error
found 8 mapped entities:
[OK] Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation
[OK] Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation
[OK] Gedmo\Translatable\Entity\Translation
[OK] Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry
[OK] Gedmo\Loggable\Entity\LogEntry
[OK] Gedmo\Tree\Entity\MappedSuperclass\AbstractClosure
[OK] Psren\CmsBundle\Entity\Group
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Gedmo\Timestampable" in property Psren\CmsBundle\Entity\User::$createdAt was never imported. Did you maybe forget to add a "use" statement for this annotation?
the annotation #Gedmo\Timestampable was defined in the trait of the extension. so it must have been loaded.
what went wrong?
thanks for help.

:-)
I just fortgot to load the annotations.
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
/**
* #ORM\Entity
* #ORM\Table(name="users")
* #UniqueEntity(fields="email", message="Email already taken")
*/
class User
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;

Related

"AuthenticatorInterface" not found on Security\Guard, Symfony 6.2

I'm trying to create a custom Token authenticator for my Symfony 6.2 and API Platform project
class TokenAuthenticator extends JWTTokenAuthenticator
{
/**
* #param PreAuthenticationJWTUserToken $preAuthToken
* #param UserProviderInterface $userProvider
* #return UserInterface
*/
public function getUser($preAuthToken, UserProviderInterface $userProvider): UserInterface
{
$user = parent::getUser($preAuthToken, $userProvider);
var_dump($preAuthToken->getPayload());exit;
}
}
But I always get this error:
Attempted to load interface "AuthenticatorInterface" from namespace "Symfony\Component\Security\Guard".
Did you forget a "use" statement for "Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface"?
that means, there's no AuthenticatorInterface on Security\Guard and Http\Authenticator replaces it, so the LexikJWTAuthenticationBundle must be updated to include the new change.
This new class contains new functions so is there any documentation regarding them? Also, the purpose of establishing this class TokenAuthenticator is to make the old token invalid when changing the password, so is there a better way to do this?

Doctrine - Set $id as UUID instead of Integer when creating new entity

I am working on a new project in Symfony 5.3. I am using this command bin/console make:entity for creating entities.
This wizard automatically creates an entity with $id as primary key of type integer. I am prefering UUID instead of integer.
How I should change settings to get Entity like this?
Thank you
namespace App\Entity;
use App\Repository\EventRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
/**
* #ORM\Entity(repositoryClass=EventRepository::class)
*/
class Event
{
/**
* #ORM\Id
* #ORM\Column(type="uuid", unique=true)
* #ORM\GeneratedValue(strategy="CUSTOM")
* #ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private $id;
...
}
There is no option to set the generated identifier strategy on make entity.
You can see all available option using php bin/console make:entity -h
Also there are no configuration in doctrine.yaml file to define this.
It could be a good feature to add for current and next version
To request a new feature, you can create a new issue feature request type:
https://github.com/symfony/symfony/issues/new/choose
You will need a github account

How can I "validate" DELETE request in api-platform

I want to check the entity variable and check if it is allowed to delete the entity. For example if the owner entity of the association is linked to another entity, I want to make the deletion impossible.
I've looked in the documentation of api-platform bu I could not find any help regarding my problems. Either you give the right to delete or not. I could not find how to control it (equivalent to validation for POST, PUT and PATCH).
You can use the access control feature of Api-Platform and Symfony Expression Language to achieve what you want. This way you can write pretty complex expressions.
I hope this example makes it clear.
user is the currently logged in user.
object is the resource user is trying to delete.
/**
* #ApiResource(
* itemOperations={
* "delete"={
* "access_control"="is_granted('ROLE_USER') and object.getUsers().contains(user),
* }
* }
* )
*/
class Entity
{
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="User", inversedBy="entities")
* #ORM\JoinTable(name="entity_users")
*/
private $users;
/**
* #return ArrayCollection
*/
public function getUsers(): ArrayCollection
{
return $this->users;
}
}
In this case only users who are stored in users Array of Entity can delete this resource.

Use entities from composer package in api platform

As we use the entities in multiple projects we have required our entities via composer.
All the stuff that would normally be in the annotations I have written into a yaml file.
But I don't get any routes shown in the frontend or the swagger documentation.
I have found the following configuration: api_platform.resource_class_directories
But you have to set a directory there. I would not really like to have a hardcoded path like vendor/my-company/my-entity-package/src/Entity.
So how do I solve this issue?
Is there a magic variable in symfony like the %kernel.project_dir% which points to the vendor directory?
Did I forget to set some configuration?
I was able to get this working by extending the classes of my composer package.
So in api/src/Entity/Order.php I have the following:
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Company\ComposerPackage\Entity\Order as ComposerOrder;
use Doctrine\ORM\Mapping as ORM;
/**
* #ApiResource()
* #ORM\Entity
*/
class Order extends ComposerOrder
{
/**
* #var int The id of this order entity.
*
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
}
For some reason api-platform was not recognizing the id property and it's annotations so I had to copy it to the child class.
The entity in the composer package also needed a change for this to work:
namespace Company\ComposerPackage\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\MappedSuperclass
*/
class Order
{
// all properties are defined here including the #ORM, #Assert etc. annotations
}
I had to add the #ORM\MappedSuperclass annotation.
The #ORM\HasLifecycleCallbacks annotation also had to be done in the childclass for some reason. Also if you have additional configuration for the table e.g. table name or unique constraints these also have to be added to the child class.

Symfony 2 - Set UniqueEntity message

I have a Symfony 2/Doctrine 2 entity with a UniqueEntity constraint. As show in the documentation, it should be possible to set a custom error message. I tied the following syntax, but that dose not work:
/**
* #ORM\Entity
* #ORM\Table(name="User")
* #UniqueEntity("email", message="Your E-Mail adress has already been registered")
*/
class User
What is the correct notation for the UniqueEntity constraint message? Or is the documentation simply wrong?
If you use only fields option in this annotaion, it can be used as the default option (the only option without name). However when you specify additional settings, you have to specify fields property.
/**
* #ORM\Entity
* #ORM\Table(name="User")
* #UniqueEntity(
* fields={"email"},
* message="Your E-Mail adress has already been registered"
* )
*/
class User

Resources