- Mastering MongoDB 3.x
- Alex Giamas
- 266字
- 2021-08-20 10:10:53
Doctrine ODM
Laravel is one of the most widely used MVC frameworks for PHP, similar in architecture to Django and Rails from the Python and Ruby worlds respectively. We will follow through configuring our models using a stack of Laravel, Doctrine, and MongoDB. This section assumes that Doctrine is installed and working with Laravel 5.x.
Doctrine entities are POPO (Plain Old PHP Objects) that, unlike Eloquent, Laravel's default ORM doesn't need to inherit from the Model class. Doctrine uses the Data Mapper pattern, whereas Eloquent uses Active Record. Skipping the get() set() methods, a simple class would look like:
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="scientist")
*/
class Scientist
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $firstname;
/**
* @ORM\Column(type="string")
*/
protected $lastname;
/**
* @ORM\OneToMany(targetEntity="Theory", mappedBy="scientist", cascade={"persist"})
* @var ArrayCollection|Theory[]
*/
protected $theories;
/**
* @param $firstname
* @param $lastname
*/
public function __construct($firstname, $lastname)
{
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->theories = new ArrayCollection;
}
...
public function addTheory(Theory $theory)
{
if(!$this->theories->contains($theory)) {
$theory->setScientist($this);
$this->theories->add($theory);
}
}
This POPO-based model used annotations to define field types that need to be persisted in MongoDB. For example, @ORM\Column(type="string") defines a field in MongoDB with the string type firstname and lastname as the attribute names, in the respective lines.
There is a whole set of annotations available here http://doctrine-orm.readthedocs.io/en/latest/reference/annotations-reference.html . If we want to separate the POPO structure from annotations, we can also define them using YAML or XML instead of inlining them with annotations in our POPO model classes.
- 機器學習與大數(shù)據(jù)技術(shù)
- 群體智能與數(shù)據(jù)挖掘
- PostgreSQL Administration Essentials
- 人工智能與人工生命
- 永磁同步電動機變頻調(diào)速系統(tǒng)及其控制(第2版)
- AI 3.0
- Blender 3D Printing by Example
- Docker on Amazon Web Services
- 氣動系統(tǒng)裝調(diào)與PLC控制
- TensorFlow Reinforcement Learning Quick Start Guide
- Visual C++項目開發(fā)案例精粹
- Cloud Security Automation
- MATLAB-Simulink系統(tǒng)仿真超級學習手冊
- 計算智能算法及其生產(chǎn)調(diào)度應用
- 玩機器人 學單片機