PHP code example of tourze / user-attribute-bundle
1. Go to this page and download the library: Download tourze/user-attribute-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Tourze\UserAttributeBundle\Entity\UserAttribute;
use Tourze\UserAttributeBundle\Repository\UserAttributeRepository;
// Create a new user attribute
$attribute = new UserAttribute();
$attribute->setUser($user);
$attribute->setName('preferred_language');
$attribute->setValue('en');
$attribute->setRemark('User preference for language');
$entityManager->persist($attribute);
$entityManager->flush();
// Retrieve user attributes
$repository = $entityManager->getRepository(UserAttribute::class);
$attributes = $repository->findBy(['user' => $user]);
use Doctrine\ORM\Events;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
#[AsDoctrineListener(event: Events::prePersist)]
class UserAttributeListener
{
public function prePersist(UserAttribute $attribute): void
{
// Custom logic before saving
if ($attribute->getName() === 'sensitive_data') {
$attribute->setValue(hash('sha256', $attribute->getValue()));
}
}
}
bash
php bin/console doctrine:migrations:migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.