Hiệu suất mongodb so với postgresql

Symfony cung cấp tất cả các công cụ bạn cần để sử dụng cơ sở dữ liệu trong ứng dụng của mình nhờ Doctrine, bộ thư viện PHP tốt nhất để làm việc với cơ sở dữ liệu. Các công cụ này hỗ trợ cơ sở dữ liệu quan hệ như MySQL và PostgreSQL cũng như cơ sở dữ liệu NoSQL như MongoDB

Show

    Cơ sở dữ liệu là một chủ đề rộng, vì vậy tài liệu được chia thành ba bài viết

    • Bài viết này giải thích cách khuyến nghị để làm việc với cơ sở dữ liệu quan hệ trong các ứng dụng Symfony;
    • Đọc bài viết khác này nếu bạn cần quyền truy cập cấp thấp để thực hiện các truy vấn SQL thô tới cơ sở dữ liệu quan hệ (tương tự như PDO của PHP);
    • Đọc tài liệu DoctrineMongoDBBundle nếu bạn đang làm việc với cơ sở dữ liệu MongoDB

    Đầu tiên, cài đặt hỗ trợ Doctrine qua

    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    6 , cũng như MakerBundle, sẽ giúp tạo một số mã

    1
    2
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle

    Thông tin kết nối cơ sở dữ liệu được lưu trữ dưới dạng biến môi trường có tên là

    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    7. Để phát triển, bạn có thể tìm và tùy chỉnh cái này bên trong
    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    8

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"

    thận trọng

    Nếu tên người dùng, mật khẩu, máy chủ lưu trữ hoặc tên cơ sở dữ liệu chứa bất kỳ ký tự nào được coi là đặc biệt trong URI (chẳng hạn như

    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    9,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    0,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    1,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    2,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    3,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    4,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    5,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    6,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    7), bạn phải mã hóa chúng. Xem RFC 3986 để biết danh sách đầy đủ các ký tự dành riêng hoặc sử dụng chức năng urlencode để mã hóa chúng. Trong trường hợp này bạn cần bỏ tiền tố
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    8 trong
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    9 để tránh sai sót.
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    0

    Bây giờ, các tham số kết nối của bạn đã được thiết lập, Doctrine có thể tạo cơ sở dữ liệu

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    1 cho bạn

    1
    $ php bin/console doctrine:database:create

    Có nhiều tùy chọn hơn trong

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    9 mà bạn có thể định cấu hình, bao gồm cả
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    3 (e. g. 5. 7 nếu bạn đang sử dụng MySQL 5. 7), điều này có thể ảnh hưởng đến cách Doctrine hoạt động

    Mẹo

    Còn nhiều lệnh Doctrine khác. Chạy

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    4 để xem danh sách đầy đủ

    Giả sử bạn đang xây dựng một ứng dụng cần hiển thị các sản phẩm. Thậm chí không cần suy nghĩ về Doctrine hoặc cơ sở dữ liệu, bạn đã biết rằng bạn cần một đối tượng

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 để đại diện cho những sản phẩm đó

    Bạn có thể sử dụng lệnh

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    6 để tạo lớp này và bất kỳ trường nào bạn cần. Lệnh sẽ hỏi bạn một số câu hỏi - hãy trả lời chúng như được thực hiện bên dưới

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)

    Ái chà. Bây giờ bạn có một tệp

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    7 mới

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }

    Ghi chú

    Bắt đầu từ v1. 44. 0 - MakerBundle chỉ hỗ trợ các thực thể sử dụng thuộc tính PHP

    Ghi chú

    Nhầm lẫn tại sao giá là một số nguyên? . Đây chỉ là một ví dụ. Tuy nhiên, lưu trữ giá dưới dạng số nguyên (e. g. 100 = $1 USD) có thể tránh được vấn đề làm tròn

    Ghi chú

    Nếu bạn đang sử dụng cơ sở dữ liệu SQLite, bạn sẽ thấy lỗi sau. PDOException. SQLSTATE[HY000]. Lỗi chung. 1 Không thể thêm cột NOT NULL với giá trị mặc định NULL. Thêm tùy chọn

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    8 vào thuộc tính
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    9 để khắc phục sự cố

    thận trọng

    Có giới hạn 767 byte cho tiền tố khóa chỉ mục khi sử dụng bảng InnoDB trong MySQL 5. 6 và các phiên bản trước đó. Các cột chuỗi có độ dài 255 ký tự và mã hóa

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    00 vượt quá giới hạn đó. Điều này có nghĩa là bất kỳ cột nào thuộc loại
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    01 và
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    02 phải đặt giá trị tối đa của nó là
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    03 thành
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    04. Nếu không, bạn sẽ thấy lỗi này. "[PDOException] SQLSTATE[42000]. Lỗi cú pháp hoặc vi phạm quyền truy cập. 1071 Khóa được chỉ định quá dài;

    Lớp này được gọi là một "thực thể". Và chẳng bao lâu nữa, bạn sẽ có thể lưu và truy vấn các đối tượng Sản phẩm vào bảng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    05 trong cơ sở dữ liệu của mình. Mỗi thuộc tính trong thực thể
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 có thể được ánh xạ tới một cột trong bảng đó. Điều này thường được thực hiện với các thuộc tính. các
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    07 bình luận mà bạn nhìn thấy trên mỗi tài sản

    Hiệu suất mongodb so với postgresql

    Lệnh

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    6 là một công cụ giúp cuộc sống dễ dàng hơn. Nhưng đây là mã của bạn. thêm/xóa trường, thêm/xóa phương thức hoặc cập nhật cấu hình

    Doctrine hỗ trợ nhiều loại trường khác nhau, mỗi loại có các tùy chọn riêng. Để xem danh sách đầy đủ, hãy xem tài liệu về Loại ánh xạ của Doctrine. Nếu bạn muốn sử dụng XML thay vì chú thích, hãy thêm

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    09 và
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    10 vào ánh xạ thực thể trong tệp
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    9 của bạn

    thận trọng

    Hãy cẩn thận không sử dụng các từ khóa SQL dành riêng làm tên bảng hoặc tên cột của bạn (e. g.

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    12 hoặc
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    13). Xem Học thuyết để biết chi tiết về cách thoát khỏi những. Hoặc, thay đổi tên bảng bằng
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    14 phía trên lớp hoặc định cấu hình tên cột bằng tùy chọn
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    15

    Lớp

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 đã được định cấu hình đầy đủ và sẵn sàng lưu vào bảng
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    05. Nếu bạn vừa định nghĩa lớp này, cơ sở dữ liệu của bạn thực sự chưa có bảng
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    05. Để thêm nó, bạn có thể tận dụng DoctrineMigrationsBundle đã được cài đặt

    1
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    1

    Nếu mọi thứ hoạt động, bạn sẽ thấy một cái gì đó như thế này

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    2
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    3

    Nếu bạn mở tệp này, nó sẽ chứa SQL cần thiết để cập nhật cơ sở dữ liệu của bạn. Để chạy SQL đó, hãy thực hiện di chuyển của bạn

    1
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    5

    Lệnh này thực thi tất cả các tệp di chuyển chưa được chạy trên cơ sở dữ liệu của bạn. Bạn nên chạy lệnh này trong quá trình sản xuất khi triển khai để luôn cập nhật cơ sở dữ liệu sản xuất của mình

    Nhưng nếu bạn cần thêm thuộc tính trường mới vào

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5, chẳng hạn như
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    9 thì sao? . Tuy nhiên, bạn cũng có thể sử dụng lại
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    6

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    6
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    7

    Điều này thêm thuộc tính

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    9 mới và các phương thức
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    23 và
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    24

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    8
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    9

    Thuộc tính mới được ánh xạ, nhưng nó chưa tồn tại trong bảng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    05. Không vấn đề gì. Tạo một di chuyển mới

    1
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    1

    Lần này, SQL trong tệp được tạo sẽ trông như thế này

    1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    3

    Hệ thống di chuyển thông minh. Nó so sánh tất cả các thực thể của bạn với trạng thái hiện tại của cơ sở dữ liệu và tạo SQL cần thiết để đồng bộ hóa chúng. Giống như trước đây, thực hiện di chuyển của bạn

    1
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    5

    Thao tác này sẽ chỉ thực thi một tệp di chuyển mới, vì DoctrineMigrationsBundle biết rằng lần di chuyển đầu tiên đã được thực hiện trước đó. Đằng sau hậu trường, nó quản lý một bảng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    26 để theo dõi điều này

    Mỗi khi bạn thực hiện thay đổi đối với lược đồ của mình, hãy chạy hai lệnh này để tạo quá trình di chuyển rồi thực hiện nó. Đảm bảo cam kết các tệp di chuyển và thực thi chúng khi bạn triển khai

    Mẹo

    Nếu bạn muốn thêm các thuộc tính mới theo cách thủ công, lệnh

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    6 có thể tạo các phương thức getter & setter cho bạn

    1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    7

    Nếu bạn thực hiện một số thay đổi và muốn tạo lại tất cả các phương thức getter/setter, hãy vượt qua cả

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    28

    Đã đến lúc lưu một đối tượng

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 vào cơ sở dữ liệu. Hãy tạo một bộ điều khiển mới để thử nghiệm

    1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    9

    Bên trong bộ điều khiển, bạn có thể tạo một đối tượng

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 mới, thiết lập dữ liệu trên đó và lưu nó

    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    0
    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    1

    dùng thử

    http. //máy chủ cục bộ. 8000/sản phẩm

    Xin chúc mừng. Bạn vừa tạo hàng đầu tiên của mình trong bảng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    05. Để chứng minh điều đó, bạn có thể truy vấn trực tiếp cơ sở dữ liệu

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    2
    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    3

    Hãy xem ví dụ trước chi tiết hơn

    • dòng 13 Đối số
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      32 báo cho Symfony vào phương thức của bộ điều khiển
    • dòng 15 Phương thức
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      33 lấy đối tượng quản lý thực thể của Doctrine, đây là đối tượng quan trọng nhất trong Doctrine. Nó chịu trách nhiệm lưu các đối tượng vào và tìm nạp các đối tượng từ cơ sở dữ liệu
    • dòng 17-20 Trong phần này, bạn khởi tạo và làm việc với đối tượng
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      34 giống như bất kỳ đối tượng PHP thông thường nào khác
    • dòng 23 Cuộc gọi
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      35 yêu cầu Doctrine "quản lý" đối tượng
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      34. Điều này không khiến truy vấn được thực hiện đối với cơ sở dữ liệu
    • dòng 26 Khi phương thức
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      37 được gọi, Doctrine sẽ xem qua tất cả các đối tượng mà nó đang quản lý để xem liệu chúng có cần được duy trì trong cơ sở dữ liệu hay không. Trong ví dụ này, dữ liệu của đối tượng
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      34 không tồn tại trong cơ sở dữ liệu, vì vậy trình quản lý thực thể thực hiện truy vấn
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      39, tạo một hàng mới trong bảng
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      05

    Ghi chú

    Nếu cuộc gọi

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    37 không thành công, một ngoại lệ
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    42 sẽ được đưa ra. Xem Giao dịch và Đồng thời

    Cho dù bạn đang tạo hay cập nhật đối tượng, quy trình làm việc luôn giống nhau. Doctrine đủ thông minh để biết liệu nó có nên CHÈN hay CẬP NHẬT thực thể của bạn hay không

    Trình xác thực Symfony tái sử dụng siêu dữ liệu Doctrine để thực hiện một số tác vụ xác thực cơ bản

    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    0_______6_______5

    Mặc dù thực thể

    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 không xác định bất kỳ cấu hình xác thực rõ ràng nào, Symfony xem xét kỹ cấu hình ánh xạ Doctrine để suy ra một số quy tắc xác thực. Ví dụ: nếu thuộc tính
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    44 không thể là
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    45 trong cơ sở dữ liệu, ràng buộc NotNull sẽ tự động được thêm vào thuộc tính (nếu nó chưa chứa ràng buộc đó)

    Bảng sau đây tóm tắt ánh xạ giữa siêu dữ liệu Doctrine và các ràng buộc xác thực tương ứng được Symfony tự động thêm vào

    Doctrine attributeValidation constraintNotes

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    46NotNullRequires installing the PropertyInfo component
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    47TypeRequires installing the PropertyInfo component
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    02UniqueEntity 
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    03Length 

    Vì thành phần Biểu mẫu cũng như Nền tảng API sử dụng nội bộ thành phần Trình xác thực nên tất cả các biểu mẫu và API web của bạn cũng sẽ tự động được hưởng lợi từ các ràng buộc xác thực tự động này

    Xác thực tự động này là một tính năng hay để cải thiện năng suất của bạn, nhưng nó không thay thế hoàn toàn cấu hình xác thực. Bạn vẫn cần thêm một số ràng buộc xác thực để đảm bảo rằng dữ liệu do người dùng cung cấp là chính xác

    Tìm nạp một đối tượng ra khỏi cơ sở dữ liệu thậm chí còn dễ dàng hơn. Giả sử bạn muốn có thể truy cập

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    50 để xem sản phẩm mới của mình

    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    6
    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    7

    Một khả năng khác là sử dụng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    51 bằng cách sử dụng hệ thống tự động của Symfony và được tiêm bởi bộ chứa tiêm phụ thuộc

    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    8
    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    9

    dùng thử

    http. //máy chủ cục bộ. 8000/sản phẩm/1

    Khi bạn truy vấn một loại đối tượng cụ thể, bạn luôn sử dụng cái được gọi là "kho lưu trữ" của nó. Bạn có thể coi kho lưu trữ là một lớp PHP có nhiệm vụ duy nhất là giúp bạn tìm nạp các thực thể của một lớp nhất định

    Khi bạn có một đối tượng kho lưu trữ, bạn có nhiều phương thức trợ giúp

    1
    0_______20_______1

    Bạn cũng có thể thêm các phương thức tùy chỉnh cho các truy vấn phức tạp hơn. Thêm về điều đó sau trong phần

    Mẹo

    Khi hiển thị trang HTML, thanh công cụ gỡ lỗi web ở cuối trang sẽ hiển thị số lượng truy vấn và thời gian thực hiện chúng

    Hiệu suất mongodb so với postgresql

    Nếu số lượng truy vấn cơ sở dữ liệu quá cao, biểu tượng sẽ chuyển sang màu vàng để cho biết có điều gì đó không đúng. Nhấp vào biểu tượng để mở Symfony Profiler và xem các truy vấn chính xác đã được thực thi. Nếu bạn không thấy thanh công cụ gỡ lỗi web, hãy cài đặt

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    52 bằng cách chạy lệnh này.
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    53

    6. 2

    Entity Value Resolver được giới thiệu trong Symfony 6. 2

    2. 7. 1

    Tự động nối dây của

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    54 đã được giới thiệu trong DoctrineBundle 2. 7. 1

    Trong nhiều trường hợp, bạn có thể sử dụng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    54 để tự động thực hiện truy vấn cho mình. Bạn có thể đơn giản hóa bộ điều khiển để

    1
    2_______20_______3

    Đó là nó. Gói sử dụng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    56 từ tuyến đường để truy vấn cho
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 theo cột
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    58. Nếu không tìm thấy, một trang 404 sẽ được tạo

    Hành vi này được bật theo mặc định trên tất cả các bộ điều khiển của bạn. Bạn có thể tắt nó bằng cách đặt tùy chọn cấu hình

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    59 thành
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    60

    Khi bị tắt, bạn có thể bật nó riêng lẻ trên các bộ điều khiển mong muốn bằng cách sử dụng thuộc tính

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    61

    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    8
    1
    5

    Mẹo

    Khi được bật trên toàn cầu, có thể vô hiệu hóa hành vi trên một bộ điều khiển cụ thể bằng cách sử dụng

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    61 được đặt thành
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    63

    chức năng công khai hiển thị (# [Người dùng hiện tại] # [MapEntity (đã tắt. true)] Người dùng $user). Phản hồi { // Người dùng không được giải quyết bởi EntityValueResolver //

    }

    Nếu các ký tự đại diện tuyến đường của bạn khớp với các thuộc tính trên thực thể của bạn, thì trình phân giải sẽ tự động tìm nạp chúng

    1
    6_______20_______7

    Tìm nạp tự động hoạt động trong những trường hợp này

    • Nếu
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      56 nằm trong lộ trình của bạn, thì điều này được sử dụng để tìm nạp bằng khóa chính thông qua phương thức
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      65
    • Trình phân giải sẽ cố gắng thực hiện tìm nạp
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      66 bằng cách sử dụng tất cả các ký tự đại diện trong tuyến đường thực sự là các thuộc tính trên thực thể của bạn (các ký tự không phải thuộc tính sẽ bị bỏ qua)

    Bạn có thể kiểm soát hành vi này bằng cách thêm thuộc tính

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    61 và sử dụng

    Nếu tính năng tìm nạp tự động không hoạt động, bạn có thể viết một biểu thức bằng cách sử dụng thành phần Ngôn ngữ biểu thức

    1
    8
    1
    9

    Trong biểu thức, biến

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    68 sẽ là lớp Kho lưu trữ của thực thể của bạn và bất kỳ ký tự đại diện tuyến đường nào - như
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    69 đều có sẵn dưới dạng biến

    Điều này cũng có thể được sử dụng để giúp giải quyết nhiều đối số

    $ php bin/console doctrine:database:create
    0_______21_______1

    Trong ví dụ trên, đối số

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    34 được xử lý tự động nhưng
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    71 được định cấu hình với thuộc tính vì cả hai đều không thể tuân theo quy ước mặc định

    Một số tùy chọn có sẵn trên chú thích

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    61 để kiểm soát hành vi

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    58

    Nếu tùy chọn

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    58 được định cấu hình và khớp với tham số tuyến đường, thì trình phân giải sẽ tìm thấy khóa chính

    1
    8
    $ php bin/console doctrine:database:create
    3

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    75

    Định cấu hình các thuộc tính và giá trị để sử dụng với phương thức

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    66. khóa là tên trình giữ chỗ tuyến đường và giá trị là tên thuộc tính Doctrine

    $ php bin/console doctrine:database:create
    4
    $ php bin/console doctrine:database:create
    5

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    77

    Định cấu hình các thuộc tính sẽ được sử dụng trong phương thức

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    66 bằng cách loại trừ một hoặc nhiều thuộc tính để không phải tất cả đều được sử dụng

    $ php bin/console doctrine:database:create
    0_______21_______7

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    79Nếu đúng, thì khi sử dụng
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    66, bất kỳ giá trị nào là
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    45 sẽ không được sử dụng cho truy vấn.
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    82

    Theo mặc định,

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    54 sử dụng trình quản lý thực thể mặc định nhưng bạn có thể định cấu hình

    1
    8
    $ php bin/console doctrine:database:create
    9

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    84Nếu đúng, buộc Doctrine luôn tìm nạp thực thể từ cơ sở dữ liệu thay vì bộ đệm.
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    63Nếu đúng, thì
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    54 sẽ không cố gắng thay thế đối số

    Khi bạn đã tìm nạp một đối tượng từ Doctrine, bạn tương tác với nó giống như với bất kỳ mô hình PHP nào

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    0____27_______1

    Sử dụng Doctrine để chỉnh sửa một sản phẩm hiện có bao gồm ba bước

    1. tìm nạp đối tượng từ Doctrine;
    2. sửa đổi đối tượng;
    3. gọi điện cho người quản lý tổ chức
      $ composer require symfony/orm-pack
      $ composer require --dev symfony/maker-bundle
      37

    Bạn có thể gọi

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    88, nhưng không cần thiết. Doctrine đã "theo dõi" đối tượng của bạn để thay đổi

    Việc xóa một đối tượng rất giống nhau, nhưng yêu cầu gọi phương thức

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    89 của trình quản lý thực thể

    1
    2
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    3

    Như bạn có thể mong đợi, phương thức

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    89 thông báo cho Doctrine rằng bạn muốn xóa đối tượng đã cho khỏi cơ sở dữ liệu. Truy vấn
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    91 không thực sự được thực thi cho đến khi phương thức
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    37 được gọi

    Bạn đã thấy cách đối tượng kho lưu trữ cho phép bạn chạy các truy vấn cơ bản mà không cần thực hiện bất kỳ thao tác nào

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    4
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    5

    Nhưng nếu bạn cần một truy vấn phức tạp hơn thì sao?

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    6_______27_______7

    Khi bạn tìm nạp kho lưu trữ của mình (tôi. e.

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    95), nó thực sự là một thể hiện của đối tượng này. Điều này là do cấu hình
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    96 đã được tạo ở đầu lớp thực thể
    // src/Entity/Product.php
    namespace App\Entity;
    
    use App\Repository\ProductRepository;
    use Doctrine\ORM\Mapping as ORM;
    
     #[ORM\Entity(repositoryClass: ProductRepository::class)]
    class Product
    {
        #[ORM\Id]
        #[ORM\GeneratedValue]
        #[ORM\Column]
        private int $id;
    
        #[ORM\Column(length: 255)]
        private string $name;
    
        #[ORM\Column]
        private int $price;
    
        public function getId(): ?int
        {
            return $this->id;
        }
    
        // .. getter and setter methods
    }
    5 của bạn

    Giả sử bạn muốn truy vấn tất cả các đối tượng Sản phẩm lớn hơn một mức giá nhất định. Thêm một phương pháp mới cho điều này vào kho lưu trữ của bạn

    # .env (or override DATABASE_URL in .env.local to avoid committing your changes)
    
    # customize this line!
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
    
    # to use mariadb:
    DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=mariadb-10.5.8"
    
    # to use sqlite:
    # DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"
    
    # to use postgresql:
    # DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
    
    # to use oracle:
    # DATABASE_URL="oci8://db_user:[email protected]:1521/db_name"
    6
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    9

    Chuỗi được chuyển đến

    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    98 có thể trông giống như SQL, nhưng đó là Ngôn ngữ truy vấn Doctrine. Điều này cho phép bạn nhập các truy vấn bằng cách sử dụng ngôn ngữ truy vấn phổ biến, nhưng thay vào đó tham chiếu các đối tượng PHP (i. e. trong câu lệnh
    $ composer require symfony/orm-pack
    $ composer require --dev symfony/maker-bundle
    99)

    Bây giờ, bạn có thể gọi phương thức này trên kho lưu trữ

    1
    8
    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    1

    Xem cách đưa kho lưu trữ vào bất kỳ dịch vụ nào

    Doctrine cũng cung cấp Trình tạo truy vấn, một cách hướng đối tượng để viết truy vấn. Bạn nên sử dụng điều này khi các truy vấn được xây dựng động (i. e. dựa trên điều kiện PHP)

    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    2
    $ php bin/console make:entity
    
    Class name of the entity to create or update:
    > Product
    
    New property name (press <return> to stop adding fields):
    > name
    
    Field type (enter ? to see all types) [string]:
    > string
    
    Field length [255]:
    > 255
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    > price
    
    Field type (enter ? to see all types) [string]:
    > integer
    
    Can this field be null in the database (nullable) (yes/no) [no]:
    > no
    
    New property name (press <return> to stop adding fields):
    >
    (press enter again to finish)
    3

    Ngoài ra, bạn có thể truy vấn trực tiếp bằng SQL nếu cần

    1
    0______0_______5

    Với SQL, bạn sẽ lấy lại dữ liệu thô chứ không phải đối tượng (trừ khi bạn sử dụng chức năng NativeQuery)

    Xem tài liệu tham khảo cấu hình Doctrine

    Doctrine cung cấp tất cả các chức năng bạn cần để quản lý các mối quan hệ cơ sở dữ liệu (còn được gọi là các liên kết), bao gồm các mối quan hệ ManyToOne, OneToMany, OneToOne và ManyToMany

    Để biết thông tin, hãy xem Cách làm việc với các Hiệp hội / Mối quan hệ Học thuyết

    Đọc bài viết về test code tương tác với cơ sở dữ liệu

    Cộng đồng Doctrine đã tạo một số tiện ích mở rộng để thực hiện các nhu cầu phổ biến, chẳng hạn như "tự động đặt giá trị của thuộc tính createdAt khi tạo thực thể". Đọc thêm về các tiện ích mở rộng Doctrine có sẵn và sử dụng StofDoctrineExtensionsBundle để tích hợp chúng vào ứng dụng của bạn