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 :)

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line