douci1918 2018-12-04 11:57
浏览 115
已采纳

纯PHP中的Laravel DB表模型实现

I have worked with some Laravel projects in the past. Creating a table and using them in a controller is pretty easy. However, I mostly work with pure PHP. Most of them are not class object based. I want to write code to work similar to the Laravel model system. I think it will be helpful to generate a DB table by hand.

I need to achieve the following:

Create a table manually and make class and the same name as the table. The class should able to get all the table info (fields, id, etc.) using the class name when creating the object (not assigning table name).

The class should able to get table fields dynamically and must have the ability to save data using Object->save(), and get a row using id: $obj=Object::find(2);

class drivers extends Model
{
//'''''
}

$driver = new $driver();
$diver->name="foo";
$diver->age=19;
$diver->save();
//------------
$diver=Driver::find(3);
$diver->delete();

I want to do the above in in pure PHP but I am unable to manage. Can anyone help? I really appreciate it, thank you.

I tried it like this:

<?php

class  Model
{

   protected function __construct() {

    public static function find($id){
        $tablename=get_somehow_extented_classname;
        //....

        return $datarow;
    }

    public static function delete($id){
        $tablename=get_somehow_extented_classname;
        //....
        $result=query("delete from $tablename where id=$id");

        return $result;
    }
}

use Model; // core model (I want this)

class drivers extends Model
{
    // nothing lot of things here
}
  • 写回答

1条回答 默认 最新

  • duanjia7912 2018-12-05 05:13
    关注

    found solution for my problem -> https://medium.com/@kshitij206/use-eloquent-without-laravel-7e1c73d79977 thanks..

    here i did steps..(sometimes can be helpful for other) created folder in xammp(my php running environment)

    executed composer require illuminate/database

    created bootstrap.php at root

    <?php
    
    require "vendor/autoload.php";
    
    use Illuminate\Database\Capsule\Manager as Capsule;
    
    $capsule = new Capsule;
    
    $capsule->addConnection([
    
       "driver" => "mysql",
    
       "host" =>"127.0.0.1",
    
       "database" => "testdatabase",
    
       "username" => "root",
    
       "password" => ""
    
    ]);
    
    //Make this Capsule instance available globally.
    $capsule->setAsGlobal();
    
    // Setup the Eloquent ORM.
    $capsule->bootEloquent();
    
    $capsule->bootEloquent();
    

    created admins.php at root

    <?php
    require "vendor/autoload.php";
    use Illuminate\Database\Eloquent\Model;
    
    class admins extends Model
    {
    
              public $timestamps = false;
              protected $primaryKey = 'admin_id';//primery key defaut id
    
    }
    

    created test.php at root

    <?php
    
    require "bootstrap.php";
    require "admins.php";
    
    
    $admin = new admins;
    $admin->admin_email="sdfdfd@.sds";
    $admin->admin_password="ljkklj";
    $admin->username="sdfdfd";
    $admin->profile_img ="pic\340943.png";
    $admin->save();
    
    //....
    $admin2=admins::find(2);
    

    and it is working... im gonna use this on my future pure php projects :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)