douci1918 2018-12-04 03: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-04 21: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 :)

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

报告相同问题?