99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Phalcon7 模型的使用

2018-10-21 07:15 更新

模型代表了應用程序中的信息(數(shù)據(jù))和處理數(shù)據(jù)的規(guī)則。模型主要用于管理與相應數(shù)據(jù)庫表進行交互的規(guī)則。 大多數(shù)情況中,在應用程序中,數(shù)據(jù)庫中每個表將對應一個模型。 應用程序中的大部分業(yè)務邏輯都將集中在模型里。

Phalcon\Mvc\Model 是 Phalcon 應用程序中所有模型的基類。它保證了數(shù)據(jù)庫的獨立性,基本的 CURD 操作, 高級的查詢功能,多表關聯(lián)等功能。Phalcon\Mvc\Model 不需要直接使用 SQL 語句,因為它的轉換方法,會動態(tài)的調用相應的數(shù)據(jù)庫引擎進行處理。

模型是數(shù)據(jù)庫的高級抽象層。如果您想進行低層次的數(shù)據(jù)庫操作,您可以查看 Phalcon\Db 組件文檔。

創(chuàng)建模型

模型是一個繼承自 Phalcon\Mvc\Model 的一個類。 它必須放到 models 文件夾。一個模型文件必須包含一個類, 同時它的類名必須符合駝峰命名法:

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{

}

上面的例子顯示了 “Robots” 模型的實現(xiàn)。 需要注意的是 Robots 繼承自 Phalcon\Mvc\Model 。 因此,Robots 模型擁有了大量繼承自該組件功能,包括基本的數(shù)據(jù)庫 CRUD (Create, Read, Update, Delete) 操作,數(shù)據(jù)驗證以及復雜的搜索支持,并且可以同時關聯(lián)多個模型。

默認情況下,模型 “Robots” 對應的是數(shù)據(jù)庫表 “robots”, 如果想映射到其他數(shù)據(jù)庫表,可以使用 getSource() 方法:

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public function getSource()
    {
        return "the_robots";
    }
}

模型 Robots 現(xiàn)在映射到了 “the_robots” 表。initialize() 方法可以幫助在模型中建立自定義行為,例如指定不同的數(shù)據(jù)庫表。 initialize() 方法在請求期間只被調用一次。

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public function initialize()
    {
        $this->setSource("the_robots");
    }
}

initialize() 方法在請求期間僅會被調用一次,目的是為應用中所有該模型的實例進行初始化。如果需要為每一個實例在創(chuàng)建的時候單獨進行初始化, 可以使用 ‘onConstruct’ 事件:

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public function onConstruct()
    {
        // ...
    }
}

公共屬性對比設置與取值 Setters/Getters

模型可以通過公共屬性的方式實現(xiàn),意味著模型的所有屬性在實例化該模型的地方可以無限制的讀取和更新。

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public $id;

    public $name;

    public $price;
}

通過使用 getters/setters 方法,可以控制哪些屬性可以公開訪問,并且對屬性值執(zhí)行不同的形式的轉換,同時可以保存在模型中的數(shù)據(jù)添加相應的驗證規(guī)則。

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    protected $id;

    protected $name;

    protected $price;

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        // The name is too short?
        if (strlen($name) < 10) {
            throw new \InvalidArgumentException('The name is too short');
        }
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setPrice($price)
    {
        // Negative prices aren't allowed
        if ($price < 0) {
            throw new \InvalidArgumentException('Price can\'t be negative');
        }
        $this->price = $price;
    }

    public function getPrice()
    {
        // Convert the value to double before be used
        return (double) $this->price;
    }
}

公共屬性的方式可以在開發(fā)中降低復雜度。而 getters/setters 的實現(xiàn)方式可以顯著的增強應用的可測試性、擴展性和可維護性。 開發(fā)人員可以自己決定哪一種策略更加適合自己開發(fā)的應用。ORM同時兼容這兩種方法。

模型放入命名空間

命名空間可以用來避免類名的沖突。ORM通過類名來映射相應的表名。比如 ‘Robots’:

<?php

namespace Store\Toys;

use Phalcon\Mvc\Model;

class Robots extends Model
{
    // ...
}

Namespaces make part of model names when they are within strings:

<?php

namespace Store\Toys;

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public $id;

    public $name;

    public function initialize()
    {
        $this->hasMany('id', 'Store\Toys\RobotsParts', 'robots_id');
    }
}

理解記錄對象

每個模型的實例對應一條數(shù)據(jù)表中的記錄??梢苑奖愕耐ㄟ^讀取對象的屬性來訪問相應的數(shù)據(jù)。比如, 一個表 “robots” 有如下數(shù)據(jù):

mysql> select * from robots;
+----+------------+------------+------+
| id | name       | type       | year |
+----+------------+------------+------+
|  1 | Robotina   | mechanical | 1972 |
|  2 | Astro Boy  | mechanical | 1952 |
|  3 | Terminator | cyborg     | 2029 |
+----+------------+------------+------+
3 rows in set (0.00 sec)

你可以通過主鍵找到某一條記錄并且打印它的名稱:

<?php

// Find record with id = 3
$robot = Robots::findFirst(3);

// Prints "Terminator"
echo $robot->name;

一旦記錄被加載到內存中之后,你可以修改它的數(shù)據(jù)并保存所做的修改:

<?php

$robot       = Robots::findFirst(3);
$robot->name = "RoboCop";
$robot->save();

如上所示,不需要寫任何SQL語句。Phalcon\Mvc\Model 為web應用提供了高層數(shù)據(jù)庫抽象。

查找記錄

Phalcon\Mvc\Model 為數(shù)據(jù)查詢提供了多種方法。下面的例子將演示如何從一個模型中查找一條或者多條記錄:

<?php

// How many robots are there?
$robots = Robots::find();
echo "There are ", count($robots), "\n";

// How many mechanical robots are there?
$robots = Robots::find("type = 'mechanical'");
echo "There are ", count($robots), "\n";

// Get and print virtual robots ordered by name
$robots = Robots::find(
    array(
        "type = 'virtual'",
        "order" => "name"
    )
);
foreach ($robots as $robot) {
    echo $robot->name, "\n";
}

// Get first 100 virtual robots ordered by name
$robots = Robots::find(
    array(
        "type = 'virtual'",
        "order" => "name",
        "limit" => 100
    )
);
foreach ($robots as $robot) {
   echo $robot->name, "\n";
}
如果需要通過外部數(shù)據(jù)(比如用戶輸入)或變量來查詢記錄,則必須要用`Binding Parameters`(綁定參數(shù))的方式來防止SQL注入.

你可以使用 findFirst() 方法獲取第一條符合查詢條件的結果:

<?php

// What's the first robot in robots table?
$robot = Robots::findFirst();
echo "The robot name is ", $robot->name, "\n";

// What's the first mechanical robot in robots table?
$robot = Robots::findFirst("type = 'mechanical'");
echo "The first mechanical robot name is ", $robot->name, "\n";

// Get first virtual robot ordered by name
$robot = Robots::findFirst(
    array(
        "type = 'virtual'",
        "order" => "name"
    )
);
echo "The first virtual robot name is ", $robot->name, "\n";

find() 和 findFirst() 方法都接受關聯(lián)數(shù)組作為查詢條件:

<?php

$robot = Robots::findFirst(
    array(
        "type = 'virtual'",
        "order" => "name DESC",
        "limit" => 30
    )
);

$robots = Robots::find(
    array(
        "conditions" => "type = ?1",
        "bind"       => array(1 => "virtual")
    )
);

可用的查詢選項如下:

參數(shù)描述舉例
conditions查詢操作的搜索條件。用于提取只有那些滿足指定條件的記錄。默認情況下Phalcon\Mvc\Model 假定第一個參數(shù)就是查詢條件。"conditions" => "name LIKE'steve%'"
columns只返回指定的字段,而不是模型所有的字段。 當用這個選項時,返回的是一個不完整的對象。"columns" => "id, name"
bind綁定與選項一起使用,通過替換占位符以及轉義字段值從而增加安全性。"bind" => array("status" =>"A", "type" => "some-time")
bindTypes當綁定參數(shù)時,可以使用這個參數(shù)為綁定參數(shù)定義額外的類型限制從而更加增強安全性。"bindTypes" =>array(Column::BIND_PARAM_STR,Column::BIND_PARAM_INT)
order用于結果排序。使用一個或者多個字段,逗號分隔。"order" => "name DESC,status"
limit限制查詢結果的數(shù)量在一定范圍內。"limit" => 10
offsetOffset the results of the query by a certain amount"offset" => 5
group從多條記錄中獲取數(shù)據(jù)并且根據(jù)一個或多個字段對結果進行分組。"group" => "name, status"
for_update通過這個選項, Phalcon\Mvc\Model 讀取最新的可用數(shù)據(jù),并且為讀到的每條記錄設置獨占鎖。"for_update" => true
shared_lock通過這個選項, Phalcon\Mvc\Model 讀取最新的可用數(shù)據(jù),并且為讀到的每條記錄設置共享鎖。"shared_lock" => true
cache緩存結果集,減少了連續(xù)訪問數(shù)據(jù)庫。"cache" => array("lifetime"=> 3600, "key" => "my-find-key")
hydrationSets the hydration strategy to represent each returned record in the result"hydration" =>Resultset::HYDRATE_OBJECTS

如果你愿意,除了使用數(shù)組作為查詢參數(shù)外,還可以通過一種面向對象的方式來創(chuàng)建查詢:

<?php

$robots = Robots::query()
    ->where("type = :type:")
    ->andWhere("year < 2000")
    ->bind(array("type" => "mechanical"))
    ->order("name")
    ->execute();

靜態(tài)方法 query() 返回一個對IDE自動完成友好的 Phalcon\Mvc\Model\Criteria 對象。

所有查詢在內部都以 PHQL 查詢的方式處理。PHQL是一個高層的、面向對象的類SQL語言。通過PHQL語言你可以使用更多的比如join其他模型、定義分組、添加聚集等特性。

最后,還有一個 findFirstBy<property-name>() 方法。這個方法擴展了前面提及的 findFirst() 方法。它允許您利用方法名中的屬性名稱,通過將要搜索的該字段的內容作為參數(shù)傳給它,來快速從一個表執(zhí)行檢索操作。

還是用上面用過的 Robots 模型來舉例說明:

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public $id;

    public $name;

    public $price;
}

我們這里有3個屬性:$id$name 和 $price。因此,我們以想要查詢第一個名稱為 ‘Terminator’ 的記錄為例,可以這樣寫:

<?php

$name  = "Terminator";
$robot = Robots::findFirstByName($name);

if ($robot) {
    $this->flash->success("The first robot with the name " . $name . " cost " . $robot->price ".");
} else {
    $this->flash->error("There were no robots found in our table with the name " . $name ".");
}

請注意我們在方法調用中用的是 ‘Name’,并向它傳遞了變量 $name, $name 的值就是我們想要找的記錄的名稱。另外注意,當我們的查詢找到了符合的記錄后,這個記錄的其他屬性也都是可用的。

模型結果集

findFirst() 方法直接返回一個被調用對象的實例(如果有結果返回的話),而 find() 方法返回一個 Phalcon\Mvc\Model\Resultset\Simple 對象。這個對象也封裝進了所有結果集的功能,比如遍歷、查找特定的記錄、統(tǒng)計等等。

這些對象比一般數(shù)組功能更強大。最大的特點是 Phalcon\Mvc\Model\Resultset 每時每刻只有一個結果在內存中。這對操作大數(shù)據(jù)量時的內存管理相當有幫助。

<?php

// Get all robots
$robots = Robots::find();

// Traversing with a foreach
foreach ($robots as $robot) {
    echo $robot->name, "\n";
}

// Traversing with a while
$robots->rewind();
while ($robots->valid()) {
    $robot = $robots->current();
    echo $robot->name, "\n";
    $robots->next();
}

// Count the resultset
echo count($robots);

// Alternative way to count the resultset
echo $robots->count();

// Move the internal cursor to the third robot
$robots->seek(2);
$robot = $robots->current();

// Access a robot by its position in the resultset
$robot = $robots[5];

// Check if there is a record in certain position
if (isset($robots[3])) {
   $robot = $robots[3];
}

// Get the first record in the resultset
$robot = $robots->getFirst();

// Get the last record
$robot = $robots->getLast();

Phalcon 的結果集模擬了可滾動的游標,你可以通過位置,或者內部指針去訪問任何一條特定的記錄。注意有一些數(shù)據(jù)庫系統(tǒng)不支持滾動游標,這就使得查詢會被重復執(zhí)行, 以便回放光標到最開始的位置,然后獲得相應的記錄。類似地,如果多次遍歷結果集,那么必須執(zhí)行相同的查詢次數(shù)。

將大數(shù)據(jù)量的查詢結果存儲在內存會消耗很多資源,正因為如此,分成每32行一塊從數(shù)據(jù)庫中獲得結果集,以減少重復執(zhí)行查詢請求的次數(shù),在一些情況下也節(jié)省內存。

注意結果集可以序列化后保存在一個后端緩存里面。 Phalcon\Cache 可以用來實現(xiàn)這個。但是,序列化數(shù)據(jù)會導致 Phalcon\Mvc\Model 將從數(shù)據(jù)庫檢索到的所有數(shù)據(jù)以一個數(shù)組的方式保存,因此在這樣執(zhí)行的地方會消耗更多的內存。

<?php

// Query all records from model parts
$parts = Parts::find();

// Store the resultset into a file
file_put_contents("cache.txt", serialize($parts));

// Get parts from file
$parts = unserialize(file_get_contents("cache.txt"));

// Traverse the parts
foreach ($parts as $part) {
    echo $part->id;
}

過濾結果集(Filtering Resultsets)?

過濾數(shù)據(jù)最有效的方法是設置一些查詢條件,數(shù)據(jù)庫會利用表的索引快速返回數(shù)據(jù)。Phalcon 額外的允許你通過任何數(shù)據(jù)庫不支持的方式過濾數(shù)據(jù)。

<?php

$customers = Customers::find()->filter(
    function ($customer) {

        // Return only customers with a valid e-mail
        if (filter_var($customer->email, FILTER_VALIDATE_EMAIL)) {
            return $customer;
        }
    }
);

將結果集轉為數(shù)組

<?php

$customers = Customers::find();
$arr = $customers->toArray();

綁定參數(shù)

在 Phalcon\Mvc\Model 中也支持綁定參數(shù)。即使使用綁定參數(shù)對性能有一點很小的影響,還是強烈建議您使用這種方法,以消除代碼受SQL注入攻擊的可能性。 綁定參數(shù)支持字符串和整數(shù)占位符。實現(xiàn)方法如下:

<?php

// Query robots binding parameters with string placeholders
$conditions = "name = :name: AND type = :type:";

// Parameters whose keys are the same as placeholders
$parameters = array(
    "name" => "Robotina",
    "type" => "maid"
);

// Perform the query
$robots = Robots::find(
    array(
        $conditions,
        "bind" => $parameters
    )
);

// Query robots binding parameters with integer placeholders
$conditions = "name = ?1 AND type = ?2";
$parameters = array(1 => "Robotina", 2 => "maid");
$robots     = Robots::find(
    array(
        $conditions,
        "bind" => $parameters
    )
);

// Query robots binding parameters with both string and integer placeholders
$conditions = "name = :name: AND type = ?1";

// Parameters whose keys are the same as placeholders
$parameters = array(
    "name" => "Robotina",
    1      => "maid"
);

// Perform the query
$robots = Robots::find(
    array(
        $conditions,
        "bind" => $parameters
    )
);
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號