PHP Classes

File: modules/new/Model.php

Recommend this page to a friend!
  Classes of Adrian M   upMVC   modules/new/Model.php   Download  
File: modules/new/Model.php
Role: Class source
Content type: text/plain
Description: Class source
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change: Important code update.

Beta2.0 - upMVC - Important code update.
Date: 2 months ago
Size: 6,480 bytes
 

Contents

Class file image Download
<?php

/**
    Generated by PHPistols
    The PHP code generator https://www.phpistols.com

    Created on Tue Oct 31 2023
 
    Copyright (c) 2023 BitsHost
    All rights reserved.

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * Here you may host your app for free:
     https://bitshost.biz/
 */


/**
 * Manage elements of the the table users.
 */

namespace New;

use
upMVC\Database;
use
PDO;

class
Model
{

    var
$dbConnexion;
    var
$users;
    var
$list;
    var
$id;
    var
$name;
    var
$email;
    var
$errorInfo;


   
/**
     * Constructor
     * @param $dbConnexion PDO db connexion
     */
   
public function __construct()
    {
       
$this->users = '`users`';
       
$database = new Database();
       
$this->dbConnexion = $database->getConnection();
    }


   
/**
     * Add a row in users.
     * @return int 0 or 1
     */
   
function add()
    {

       
$query = " INSERT INTO $this->users ( name, email)
                    VALUES (
                    :name,
                    :email
                    )"
;



       
$q = $this->dbConnexion->prepare($query);
        if (
$q->execute(array(':name' => $this->name, ':email' => $this->email,))) {
           
$this->id = $this->dbConnexion->lastInsertId();
            return (
1);
        } else {
           
//var_dump($q->errorInfo())
            //$q->debugDumpParams()
           
return 0;
        }
    }


   
/**
     * Update a row in users.
     * @return int 0 or 1
     */
   
function save()
    {

       
// the element exists
       
if ($this->id > 0) {
           
$query = " UPDATE $this->users SET
                    `name` = :name,
                    `email` = :email
            WHERE id = :id "
;


           
$q = $this->dbConnexion->prepare($query);
            if (
$q->execute(array(':id' => $this->id, ':name' => $this->name, ':email' => $this->email))) {
                return
1;
            } else {
               
//var_dump($q->errorInfo());
                //$q->debugDumpParams();
               
$errorInfo = $q->errorInfo();
                return
0;
            }
        }
       
// the element don't exists
       
else {
            return (
$this->add());
        }
    }


   
/**
     * Delete a row in users.
     * @param Int id_user
     * @return int 0 or 1
     */
   
function del()
    {

       
$query = "DELETE FROM $this->users WHERE `id` = :id";


       
$q = $this->dbConnexion->prepare($query);

        if (
$q->execute(array(':id' => $this->id))) {
            return (
1);
        } else {
           
//var_dump($q->errorInfo());
            //$q->debugDumpParams();
           
return 0;
        }
    }


   
/**
     * Get a row in users.
     * @param Int id_user
     * @return int 0 or 1
     */
   
function get()
    {

       
$query = " SELECT u.`id`, u.`name`, u.`email`
                    FROM
$this->users AS u
                    WHERE u.id = :id"
;


       
$q = $this->dbConnexion->prepare($query);
        if (
$q->execute(array(':id' => $this->id))) {
            if (
$row = $q->fetch(PDO::FETCH_ASSOC)) {
               
$this->id = $row['id'];
               
$this->name = $row['name'];
               
$this->email = $row['email'];

                return
1;
            } else {
                return
0;
            }
        } else {
           
//var_dump($q->errorInfo())
            //$q->debugDumpParams()
           
return 0;
        }
    }


   
/**
     * Get a list of row in users.
     * @param Int limitFrom
     * @param Int limitNumber
     * @param char orderBy
     * @param Int order
     * @return int 0 or Number of elements
     */
   
function getList($limitFrom, $limitNumber, $orderBy = '', $order = 'ASC')
    {

       
$query = " SELECT u.`id`, u.`name`, u.`email`
                    FROM
$this->users AS u ";

        if (
$order != 'ASC') $order = 'DESC';
        if (
$orderBy) {
           
$query .= "ORDER BY :orderBy $order ";
        }

        if (
$limitNumber) {
           
$query .= "LIMIT :limitFrom, :limitNumber ";
        }


       
$q = $this->dbConnexion->prepare($query);

        if (
$limitNumber) {
           
$q->bindValue(':limitFrom', intval($limitFrom), PDO::PARAM_INT);
           
$q->bindValue(':limitNumber', intval($limitNumber), PDO::PARAM_INT);
        }

        if (
$orderBy) {
           
$q->bindValue(':orderBy', intval($orderBy), PDO::PARAM_INT);
        }

        if (
$q->execute()) {
           
$this->list = array();
           
$i = 0;
            while (
$row = $q->fetch(PDO::FETCH_ASSOC)) {
               
$this->list[$i]['id'] = $row['id'];
               
$this->list[$i]['name'] = $row['name'];
               
$this->list[$i]['email'] = $row['email'];
               
$i++;
            }
            return (
$this->count());
        } else {
           
//var_dump($q->errorInfo())
            //$q->debugDumpParams()
           
return (0);
        }
    }


   
/**
     * Count row in users * @return int 0 or Number of elements
     */
   
function count()
    {

       
$query = "SELECT COUNT(*) AS nbRows
        FROM
$this->users";

       
$q = $this->dbConnexion->prepare($query);

        if (
$q->execute()) {
            if (
$row = $q->fetch(PDO::FETCH_ASSOC)) {
                return
$row['nbRows'];
            }
        } else {
           
//var_dump($q->errorInfo())
            //$q->debugDumpParams()
           
return 0;
        }
    }
}