Giter Club home page Giter Club logo

component_database's People

Contributors

jgauthi avatar

Watchers

 avatar

component_database's Issues

Pdo entity

Création d'une class Pdo Entity Factory, s'inspirer de AbstractCrud.php

Voici le code émis sur ccpro:

<?php
// @todo: Créer une class PdoEntityFactory

class PdoEntityFactory
{
    /**
     * @return bool
     */
    public function insert()
    {
        $error = [];

        if (empty($this->id_site)) {
            $error[] = 'id_site require';
        } elseif (empty($this->id_liste)) {
            $error[] = 'id_liste require';
        } elseif ($this->active === null) {
            $this->active = 0;
        }
        if (!empty($error)) {
            throw new InvalidArgumentException(implode(', ', $error));
        }

        $arguments = [
            'id_site' => intval($this->id_site),
            'id_liste' => intval($this->id_liste),
            'active' => intval($this->active)
        ];

        $pdo = newPDO();
        $request = $pdo->prepare("INSERT INTO `{$this->__table}`
            SET id_site = :id_site,
                id_liste = :id_liste,
                active = :active
        ")->execute($arguments);

        return boolval($request);
    }

    /**
     * @return bool
     */
    public function update()
    {
        if (empty($this->id_site)) {
            throw new InvalidArgumentException('id_site require for delete this entry');
        } elseif ($this->id_liste === null && $this->active === null) {
            throw new InvalidArgumentException('id_liste or active required for update this entry');
        }

        $arguments = ['id_site' => intval($this->id_site)];
        $fields = [];

        if ($this->id_liste !== null) {
            $arguments['id_liste'] = intval($this->id_liste);
            $fields[] = 'id_liste = :id_liste';
        }
        if ($this->active !== null) {
            $arguments['active'] = intval($this->active);
            $fields[] = 'active = :active';
        }

        if (empty($fields)) {
            throw new InvalidArgumentException(__CLASS__.': No data to update');
        }

        $fields = implode(', ', $fields);

        $pdo = newPDO();
        $request = $pdo
            ->prepare("UPDATE `{$this->__table}` SET {$fields} WHERE id_site = :id_site LIMIT 1")
            ->execute($arguments)
        ;

        return boolval($request);
    }

    /**
     * @return bool
     */
    public function delete()
    {
        if (empty($this->id_site)) {
            throw new InvalidArgumentException('id_site require for delete this entry');
        }

        $pdo = newPDO();
        $request = $pdo
            ->prepare("DELETE FROM `{$this->__table}` WHERE id_site = :id_site LIMIT 1")
            ->execute(['id_site' => intval($this->id_site)])
        ;

        return boolval($request);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.