PHP code example of blackcube / yii-bridge-model

1. Go to this page and download the library: Download blackcube/yii-bridge-model 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/ */

    

blackcube / yii-bridge-model example snippets


#[Bridge]                           // Auto-detect target getter/setter
#[Bridge(name: 'title')]            // Map to different name
#[Bridge(format: 'Y-m-d')]          // Date format for conversion
#[Bridge(getter: 'getX', setter: 'setX')]  // Explicit target methods



declare(strict_types=1);

namespace App\Model;

use Yiisoft\ActiveRecord\ActiveRecord;

class Product extends ActiveRecord
{
    protected ?int $id = null;
    protected string $name = '';
    protected ?float $price = null;
    protected bool $active = true;
    protected ?DateTimeImmutable $publishedAt = null;

    public function tableName(): string
    {
        return 'products';
    }

    public function getId(): ?int { return $this->id; }
    public function getName(): string { return $this->name; }
    public function setName(string $name): void { $this->name = $name; }
    public function getPrice(): ?float { return $this->price; }
    public function setPrice(?float $price): void { $this->price = $price; }
    public function isActive(): bool { return $this->active; }
    public function setActive(bool $active): void { $this->active = $active; }
    public function getPublishedAt(): ?DateTimeImmutable { return $this->publishedAt; }
    public function setPublishedAt(?DateTimeImmutable $publishedAt): void { $this->publishedAt = $publishedAt; }
}



declare(strict_types=1);

namespace App\Form;

use Blackcube\BridgeModel\Attributes\Bridge;
use Blackcube\BridgeModel\BridgeFormModel;

class ProductForm extends BridgeFormModel
{
    #[Bridge]
    public ?int $id = null;

    #[Bridge]
    public string $name = '';

    #[Bridge]
    public ?float $price = null;

    #[Bridge]
    public bool $active = true;

    #[Bridge(format: 'Y-m-d')]
    public ?string $publishedAt = null;

    public function rules(): array
    {
        return [
            'name' => [new Required(), new Length(min: 3, max: 255)],
            'price' => [new Number(min: 0)],
        ];
    }
}

// Factory method — creates and populates
$form = ProductForm::createFromModel($product);

// Or manually
$form = new ProductForm();
$form->initFromModel($product);

// Load POST data
$form->load($request->getParsedBody());

// Validate
if ($form->validate()) {
    // Transfer to AR
    $form->populateModel($product);
    $product->save();
}

class ProductForm extends BridgeFormModel
{
    public const SCENARIO_CREATE = 'create';
    public const SCENARIO_EDIT = 'edit';

    #[Bridge]
    public ?int $id = null;

    #[Bridge]
    public string $name = '';

    #[Bridge]
    public ?float $price = null;

    public function scenarios(): array
    {
        return [
            self::SCENARIO_CREATE => ['name', 'price'],
            self::SCENARIO_EDIT => ['name', 'price', 'id'],
        ];
    }
}

// Usage
$form = ProductForm::createFromModel($product);
$form->setScenario(ProductForm::SCENARIO_EDIT);

// AR has DateTimeImmutable
protected ?DateTimeImmutable $publishedAt = null;

// FormModel has string with format
#[Bridge(format: 'Y-m-d')]
public ?string $publishedAt = null;

// Transfer AR → FormModel: DateTimeImmutable becomes "2025-02-01"
// Transfer FormModel → AR: "2025-02-01" becomes DateTimeImmutable

#[Bridge(type: 'DateTimeImmutable', format: 'Y-m-d')]
public ?string $createdAt = null;

class Product extends ActiveRecord implements ElasticInterface
{
    use MagicComposeActiveRecordTrait;
    use ElasticTrait;
    
    // Regular properties...
}

class ProductForm extends BridgeFormModel
{
    #[Bridge]
    public string $name = '';

    // Elastic properties (sku, color, size) are added automatically
    // from the AR's JSON Schema when you call initFromModel()

    public function scenarios(): array
    {
        return [
            self::SCENARIO_DEFAULT => ['name', self::ALL_ELASTIC_ATTRIBUTES],
            'basic' => ['name', self::NO_ELASTIC_ATTRIBUTES],
        ];
    }
}

#[Bridge(getter: 'fetchTitle', setter: 'storeTitle')]
public string $name = '';