Model

Brief introduction

A table entity is required, whether it is an advanced query or a underlying query. A table field and a class property are one by one mappings, and the operation of a class is equivalent to an operation on a watch, which is called an entity Swoft 2.x and 1.x entities use many simple methods, both of which have a Builder query constructor and are consistent with the method of querying the constructor, except that the return may become an entity.

Entity

A table structure in which an entity class corresponds to a database

An entity object represents a row of data records for a table

Note: Entities cannot be injected into any class as properties because each entity object is a different row of data records. The entity object is created wherever it is used.

Entity definitions

Let's look at an example of an entity definition:

<?php declare(strict_types=1);

 namespace SwoftTest\Db\Testing\Entity;

 use Swoft\Db\Annotation\Mapping\Column;
 use Swoft\Db\Annotation\Mapping\Entity;
 use Swoft\Db\Annotation\Mapping\Id;
 use Swoft\Db\Eloquent\Model;

 /**
  * Class User
  *
  * @since 2.0
  *
  * @Entity(table="user",pool="db.pool2")
  */
 class User extends Model
 {
     /**
      * @Id(incrementing=true)
      *
      * @Column(name="id", prop="id")
      * @var int|null
      */
     private $id;

     /**
      * @Column(name="password", hidden=true)
      * @var string|null
      */
     private $pwd;

     /**
      * @Column()
      *
      * @var int|null
      */
     private $age;

     /**
      * @return int|null
      */
     public function getId(): ?int
     {
         return $this->id;
     }

     /**
      * @param int|null $id
      */
     public function setId(?int $id): void
     {
         $this->id = $id;
     }

     /**
      * @return int|null
      */
     public function getAge(): ?int
     {
         return $this->age;
     }

     /**
      * @param int|null $age
      */
     public function setAge(?int $age): void
     {
         $this->age = $age;
     }

     /**
      * @return string|null
      */
     public function getPwd(): ?string
     {
         return $this->pwd;
     }

     /**
      * @param string|null $pwd
      */
     public function setPwd(?string $pwd): void
     {
         $this->pwd = $pwd;
     }
 }

Annotation tags

@Entity

Mark a class as an entity with two parameters

  • Name defines the database table name for the entity map (required)
  • Pool The connection selected by the entity Chime think db.pool you can replace it with your own defined connection pool, which is designed for 2 purposes, the first one you can switch yourself to define a DB connection pool, and the second is that you can use your own implementation of the database driver, If User the table is MySQL , the Count table can be PostSQL implemented using a different connection pool.

    @Column

    Mark a column, and if a column is not defined @Column then the query will not be displayed, so that even if you add a database field, the production environment does not affect the operation.

  • Name defines the table field of the Class property map, the properties that are not marked by the annotation, and are not mapped (the default is the fields named property name)
  • Prop set an alias for a field
  • Whether the hidden is hidden, if it is true then it toArray() will be hidden, but does not affect you by Getter getting it, you can also call the entity's setVisible method to unhide him.

    Description: All field properties must have getter and setter methods that you can use to phpstorm shortcut keys ctrl+n , which will be more properties quickly generated getter and setter .

2.x removes the first type of the Type property that is now defined using the annotation defined on the property @var , determines the return value type, and the underlying strong turn type

@Id

The annotation indicates that the current class property corresponds to the primary key in the database table and must have this annotation tag and cannot set multiple @Id annotations

  • Whether the incrementing is an incremented primary key, the default is the increment primary key.
/docs/2.x/en/db/model.html
progress-bar