# ivyforms/0.2.1/backend/src/Repository/Field/FieldRepository.php

The Innovative Form Builder – IvyForms, version 0.2.1. 133 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.2.1/code/backend/src/Repository/Field/FieldRepository.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.2.1/raw/backend/src/Repository/Field/FieldRepository.php
- Modified: 2025-08-08T07:58:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.2.1/code/backend/src/Repository/Field/FieldRepository.php#L10-L20`.

```php
<?php

/**
 * @copyright © TMS-Plugins. All rights reserved.
 * @licence   See LICENCE.md for license details.
 */

namespace IvyForms\Repository\Field;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\QueryExecutionException;
use IvyForms\Entity\Field\Field;
use IvyForms\Factory\Field\FieldFactory;
use IvyForms\Repository\AbstractRepository;
use IvyForms\Services\Translations\BackendStrings;

/**
 * Class FieldRepository
 *
 * @package IvyForms\Repository\Field
 */
class FieldRepository extends AbstractRepository implements FieldRepositoryInterface
{
    public const FACTORY = FieldFactory::class;

    /**
     * Add field to the database
     *
     * @param Field $entity
     *
     * @return int
     *
     * @throws QueryExecutionException
     */
    public function add($entity): int
    {

        $data = $entity->toArray();

        $result = $this->wpdb->insert(
            $this->table,
            [
                'fieldIndex'   => $data['fieldIndex'],
                'formId'       => $data['formId'],
                'type'         => $data['type'],
                'label'        => $data['label'],
                'required'     => (int)$data['required'],
                'defaultValue' => $data['defaultValue'],
                'placeholder'  => $data['placeholder'],
                'position'     => $data['position'],
                'dateCreated'  => current_time('mysql')
            ],
            ['%s', '%d', '%s', '%s', '%s', '%s', '%s', '%d', '%s']
        );

        if ($result === false) {
            throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_add_data'] . __CLASS__);
        }

        return $this->wpdb->insert_id;
    }

    /**
     * Update field in the database
     *
     * @param int      $id
     * @param Field $entity
     *
     * @return bool
     *
     * @throws QueryExecutionException
     */
    public function update(int $id, $entity): bool
    {

        $data = $entity->toArray();

        $result = $this->wpdb->update(
            $this->table,
            [
                'fieldIndex'   => $data['fieldIndex'],
                'formId'       => $data['formId'],
                'type'         => $data['type'],
                'label'        => $data['label'],
                'required'     => (int)$data['required'],
                'defaultValue' => $data['defaultValue'],
                'placeholder'  => $data['placeholder'],
                'position'     => $data['position'],
            ],
            [
                'id' => $data['id']
            ],
            ['%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%d'],
            ['%d']
        );

        if ($result === false) {
            throw new QueryExecutionException(BackendStrings::getAllFormsStrings()['unable_to_save_data'] . __CLASS__);
        }

        return $result;
    }

    /**
     * Get fields by form ID
     *
     * @param int $id
     *
     * @return mixed[]
     */
    public function getFieldsById(int $id): array
    {
        $rows =  $this->wpdb->get_results(
            $this->wpdb->prepare(
                $this->selectQuery() . " WHERE $this->table.formId = %d ORDER BY $this->table.position ASC",
                $id
            ),
            ARRAY_A
        );
        $result = [];

        foreach ($rows as $row) {
            $result[] = call_user_func([static::FACTORY, 'create'], $row);
        }

        return $result;
    }
}

```
