PHP code example of tourze / coupon-h5-link-bundle

1. Go to this page and download the library: Download tourze/coupon-h5-link-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/ */

    

tourze / coupon-h5-link-bundle example snippets


return [
    // ...
    Tourze\CouponH5LinkBundle\CouponH5LinkBundle::class => ['all' => true],
];

use Tourze\CouponH5LinkBundle\Entity\H5Link;
use Tourze\CouponCoreBundle\Entity\Coupon;

// Create a new H5 link
$h5Link = new H5Link();
$h5Link->setUrl('https://example.com/coupon-page');
$h5Link->setCoupon($coupon); // Associate with a coupon

// Save to database
$entityManager->persist($h5Link);
$entityManager->flush();

use Tourze\CouponH5LinkBundle\Repository\H5LinkRepository;

// Inject the repository
public function __construct(
    private H5LinkRepository $h5LinkRepository
) {}

// Find H5 links
$h5Links = $this->h5LinkRepository->findAll();

$h5Link = new H5Link();
$h5Link->setUrl('https://example.com/coupon');

// Get API array representation
$apiData = $h5Link->retrieveApiArray();
// Returns: [
//     'id' => 12345,
//     'createTime' => '2024-01-01 12:00:00',
//     'updateTime' => '2024-01-01 12:00:00',
//     'url' => 'https://example.com/coupon'
// ]

use Tourze\CouponH5LinkBundle\Repository\H5LinkRepository;
use Doctrine\Persistence\ManagerRegistry;

class CustomH5LinkRepository extends H5LinkRepository
{
    public function findByCouponType(string $type): array
    {
        return $this->createQueryBuilder('h')
            ->join('h.coupon', 'c')
            ->where('c.type = :type')
            ->setParameter('type', $type)
            ->getQuery()
            ->getResult();
    }
}

// URL validation ensures proper format
// Length constraint prevents database overflow
// NotBlank ensures 

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Tourze\CouponH5LinkBundle\Entity\H5Link;

#[AsEntityListener(event: Events::prePersist, entity: H5Link::class)]
class H5LinkListener
{
    public function prePersist(H5Link $h5Link): void
    {
        // Custom logic before saving
        $this->validateUrl($h5Link->getUrl());
    }
}