Home > PHP Framework > Laravel > body text

what is laravel orm

青灯夜游
Release: 2022-02-14 15:07:24
Original
3535 people have browsed it

In laravel, the full name of orm is "Object-Relational Mapping", which means "object-relational mapping". Its function is to make a mapping between the relational database and business entity objects; in this way, when operating business objects, There is no need to deal with complex SQL statements, just operate the properties and methods of the object.

what is laravel orm

The operating environment of this tutorial: Windows 7 system, Laravel 6 version, Dell G3 computer.

What is ORM

ORM, the full name is Object-Relational Mapping, its function is to make a mapping between relational database and business entity objects , In this way, when we operate specific business objects, we no longer need to deal with complex SQL statements, we only need to simply operate the properties and methods of the objects.

ORM implementation method

The two most common implementation methods are ActiveRecord and DataMapper (the former is used in laravel)

  • # Models in ##ActiveRecord (very popular) correspond to data tables one-to-one, while models and data tables in

  • DataMapper are completely separated.

Laravel's Eloquent ORM uses ActiveRecord implementation. Each Eloquent model class corresponds to a table in the database. We can add, delete, modify, and query the database by calling the corresponding method of the model class. .

Understand the two magic functions __call () and __callStatic ()

class Test{
    //动态调用的时候 没有找到此函数 则执行__call() 方法
    public function __call($method, $parameters){
        echo 22222222222;
        return (new Rest)->$method(...$parameters);
    }
    //静态调用的时候 没有找到此函数 则执行__callStatic()方法
    public static function __callStatic($method, $parameters){
        echo 1111111111;
        return (new static)->$method(...$parameters);
    }
}

class Rest{
  public function foo($name , $age){
  echo 333;
  dump($name,$age);
  }
}

//先调用了__callStatic(), 在调用__call(), 然后调用 foo();
Test::foo('张三',17);  
//只调用了 __call(), 然后调用 foo();
(new Test())->foo('李四',16);die;
Copy after login

Understanding the first two magic functions will also understand the difficulties in laravel Eloqument ORM Now, let’s take a look at the source code in Model

/**
 * Handle dynamic method calls into the model. * * @param string $method
  * @param array $parameters
  * @return mixed
 */public function __call($method, $parameters)
{
 if (in_array($method, ['increment', 'decrement'])) {
      return $this->$method(...$parameters);
  }
  return $this->newQuery()->$method(...$parameters);
}

/**
 * Handle dynamic static method calls into the method. * * @param string $method
  * @param array $parameters
  * @return mixed
 */public static function __callStatic($method, $parameters)
{
 return (new static)->$method(...$parameters);
}
Copy after login

new static returns the caller’s instance, new self () returns its own instance

When using eloqument to query

$list = Politician::where('party_id', 1)->count();
Copy after login

where method is not in Model, it will first execute the callStatic() function to obtain the App\Models\Politician instance, then execute call (), and look for where () count(), etc. in the instance returned by $this->newQuery () method.

Take a closer look at the instance returned by the newQuery () method. After understanding these two magic functions, the difficulty of implementing ORM in laravel can be overcome.

The query constructor in laravel

$list = DB::table('categoty')->get();
Copy after login

Eloquent ORM actually encapsulates the query construction, making it easier to operate. If you are interested in the source code of the query constructor, you can take a look

[Related recommendations:

laravel video tutorial]

The above is the detailed content of what is laravel orm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!