theozebua / oze-framework-core

Just a simple mvc framework for beginners.
MIT License
4 stars 1 forks source link

feature/QueryBuilder #23

Closed sensasi-delight closed 1 year ago

sensasi-delight commented 1 year ago

jaga-jaga kalau abang males ngoding 😁

DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO `users` (`id`, `name`, `email`, `password`) VALUES
    (1, 'Oze1', 'oze1@oze.com', NULL),
    (2, 'Oze2', 'oze2@oze.com', NULL),
    (3, 'Oze3', 'oze3@oze.com', NULL);
       echo '<pre>';
        // var_dump(User::select()); // error, and shouldbe
        var_dump(User::all());
        var_dump(User::select('name', 'email', 'password')->all());
        var_dump(User::select(['name', 'email', 'password'])->all());
        var_dump(User::select(['name'], 'email', 'password')->all());
        var_dump(User::select('name', ['email', 'password'])->all());
        var_dump(User::select(['name' => ['email', 'password']])->all()); // only read email and password
        echo '</pre>';
sensasi-delight commented 1 year ago

Ada error di Env/Environment.php.

di tempatku kok gak error ya bang??

Itu untuk apa dibuat property-nya ya bang?

deprecation notice bang di PHP 8.2 (dynamic property)

theozebua commented 1 year ago

deprecation notice bang di PHP 8.2 (dynamic property)

Kita pakai PHP ^8.1 bang, lupa memang ku buat di composer.json Dan aku juga udah buat __set() magic method disitu 🤔 Sesuai dengan dokumentasi PHP, "The creation of dynamic properties is deprecated" tetapi yang menggunakan magic method __get()/__set() masih tetap bisa seharusnya kalau memang abang pakainya PHP ^8.2

The creation of dynamic properties is deprecated, unless the class opts in by using the #[\AllowDynamicProperties] attribute. stdClass allows dynamic properties. Usage of the __get()/__set() magic methods is not affected by this change.

See documentation

sensasi-delight commented 1 year ago

@theozebua

Kita pakai PHP ^8.1 bang, lupa memang ku buat di composer.json

udah require php:8.1 aku masukin di https://github.com/theozebua/oze-framework-core/pull/23/commits/cab431365865173b90e190d5be575c1c9ea9080d, sudah aku serve di dua versi PHP gak muncul error bang.

Dan aku juga udah buat __set() magic method disitu 🤔 Sesuai dengan dokumentasi PHP, "The creation of dynamic properties is deprecated" tetapi yang menggunakan magic method __get()/__set() masih tetap bisa seharusnya kalau memang abang pakainya PHP ^8.2

The creation of dynamic properties is deprecated, unless the class opts in by using the #[\AllowDynamicProperties] attribute. stdClass allows dynamic properties. Usage of the __get()/__set() magic methods is not affected by this change.

See documentation

kayaknya gini bang, __set() bisa tapi dynamic property di dalam __set() tetap bakal deprecate bang. contoh:

<?php

class X
{
    private int $mililiter;

    final public function __set(string $name, mixed $value)
    {
        if ($name === 'liter') {
            $this->mililiter = $value * 1000;
        } else {
            $this->{$name} = $value;
        }
    }
}

$x = new X;

$x->liter = 2; // not dynamic 'liter' name is handled on __set()
$x->gallon = 2; // still dynamic because on it will set the `$this->gallon` dynamic prop

echo 'end program';

output on 8.1 and 8.2:

C:\Users\zain\Desktop\oze-framework\public>php81 test.php
end program
C:\Users\zain\Desktop\oze-framework\public>php82 test.php 

Deprecated: Creation of dynamic property X::$gallon is deprecated in C:\Users\zain\Desktop\oze-framework\public\test.php on line 12     
end program
C:\Users\zain\Desktop\oze-framework\public>

posible solution:

<?php

class X
{
    private array $properties;

    final public function __set(string $name, mixed $value)
    {
        $this->properties[$name] = $value;
    }

    public function __get($name)
    {
        return $this->properties[$name];
    }
}

$x = new X;

$x->liter = 2;
$x->gallon = 2;

echo 'end program';
theozebua commented 1 year ago

Aku udah buat query builder-nya ya bang. Tinggal dilanjutkan aja, atau bisa sama-sama kita kerjakan. 😁 Silahkan ke branch feat/query-builder.