<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                Behaviors are shared conducts that several models may adopt in order to re-use code, the ORM provides an API to implement behaviors in your models. Also, you can use the events and callbacks as seen before as an alternative to implement Behaviors with more freedom. 行為是共享的,為了重用代碼,一些模型可能會采用,ORM提供了一個API來實現模型中的行為。另外,您可以使用前面所見的事件和回調,以實現更自由的行為。 A behavior must be added in the model initializer, a model can have zero or more behaviors: 必須在模型初始化器中添加一個行為,一個模型可以有零個或更多的行為: ~~~ <?php use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Behavior\Timestampable; class Users extends Model { public $id; public $name; public $created_at; public function initialize() { $this->addBehavior( new Timestampable( [ "beforeCreate" => [ "field" => "created_at", "format" => "Y-m-d", ] ] ) ); } } ~~~ The following built-in behaviors are provided by the framework: 以下的內置行為是由框架提供的: | Name 名字 | Description 描述 | | --- | --- | | Timestampable 生成時間戳 | Allows to automatically update a model’s attribute saving the datetime when a record is created or updated 當創建或更新記錄時,允許自動更新一個模型的屬性保存datetime | | SoftDelete | Instead of permanently delete a record it marks the record as deleted changing the value of a flag column 不再永久刪除記錄,而是將記錄標記為刪除標記列的值 | #### 生成時間戳(Timestampable) This behavior receives an array of options, the first level key must be an event name indicating when the column must be assigned: 該行為接收到一系列選項,第一個級別的鍵必須是一個事件名稱,指示何時必須分配列: ~~~ <?php use Phalcon\Mvc\Model\Behavior\Timestampable; public function initialize() { $this->addBehavior( new Timestampable( [ "beforeCreate" => [ "field" => "created_at", "format" => "Y-m-d", ] ] ) ); } ~~~ Each event can have its own options, ‘field’ is the name of the column that must be updated, if ‘format’ is a string it will be used as format of the PHP’s function date, format can also be an anonymous function providing you the free to generate any kind timestamp: 每個事件都可以有自己的選項,“field”是必須更新的列的名稱,如果“format”是一個字符串,它將用作PHP函數日期的格式,格式也可以是一個匿名函數,它可以為您提供任何類型的時間戳: ~~~ <?php use DateTime; use DateTimeZone; use Phalcon\Mvc\Model\Behavior\Timestampable; public function initialize() { $this->addBehavior( new Timestampable( [ "beforeCreate" => [ "field" => "created_at", "format" => function () { $datetime = new Datetime( new DateTimeZone("Europe/Stockholm") ); return $datetime->format("Y-m-d H:i:sP"); } ] ] ) ); } ~~~ If the option ‘format’ is omitted a timestamp using the PHP’s function time, will be used. 如果使用PHP的函數時間來省略選項的“format”,將會使用。 #### 軟刪除(SoftDelete) This behavior can be used in the following way: 這種行為可以用以下方式來使用: ~~~ <?php use Phalcon\Mvc\Model; use Phalcon\Mvc\Model\Behavior\SoftDelete; class Users extends Model { const DELETED = "D"; const NOT_DELETED = "N"; public $id; public $name; public $status; public function initialize() { $this->addBehavior( new SoftDelete( [ "field" => "status", "value" => Users::DELETED, ] ) ); } } ~~~ This behavior accepts two options: ‘field’ and ‘value’, ‘field’ determines what field must be updated and ‘value’ the value to be deleted. Let’s pretend the table ‘users’ has the following data: 該行為接受兩個選項:“field”和“value”,“field”確定必須更新哪些字段,并“value”被刪除的值。讓我們假設表“users”有以下數據: ~~~ mysql> select * from users; +----+---------+--------+ | id | name | status | +----+---------+--------+ | 1 | Lana | N | | 2 | Brandon | N | +----+---------+--------+ 2 rows in set (0.00 sec) ~~~ If we delete any of the two records the status will be updated instead of delete the record: 如果我們刪除這兩個記錄中的任何一個,狀態將被更新,而不是刪除記錄: ~~~ <?php Users::findFirst(2)->delete(); ~~~ The operation will result in the following data in the table: 該操作將導致表中的以下數據: ~~~ mysql> select * from users; +----+---------+--------+ | id | name | status | +----+---------+--------+ | 1 | Lana | N | | 2 | Brandon | D | +----+---------+--------+ 2 rows in set (0.01 sec) ~~~ Note that you need to specify the deleted condition in your queries to effectively ignore them as deleted records, this behavior doesn’t support that. 注意,您需要在查詢中指定已刪除的條件,以有效地忽略它們作為已刪除的記錄,這種行為不支持這一點。 #### 創建行為(Creating your own behaviors) The ORM provides an API to create your own behaviors. A behavior must be a class implementing the Phalcon\Mvc\Model\BehaviorInterface. Also, Phalcon\Mvc\Model\Behavior provides most of the methods needed to ease the implementation of behaviors. ORM提供了一個API來創建您自己的行為。行為必須是一個實現了 Phalcon\Mvc\Model\BehaviorInterface 接口的類。此外,對于實現行為的實現,Phalcon\Mvc\Model\Behavior行為提供了大部分的方法。 The following behavior is an example, it implements the Blameable behavior which helps identify the user that is performed operations over a model: 下面的行為是一個例子,它實現了可Blameable行為,它幫助識別在模型上執行操作的用戶: ~~~ <?php use Phalcon\Mvc\Model\Behavior; use Phalcon\Mvc\Model\BehaviorInterface; class Blameable extends Behavior implements BehaviorInterface { public function notify($eventType, $model) { switch ($eventType) { case "afterCreate": case "afterDelete": case "afterUpdate": $userName = // ... get the current user from session // Store in a log the username, event type and primary key file_put_contents( "logs/blamable-log.txt", $userName . " " . $eventType . " " . $model->id ); break; default: /* ignore the rest of events */ } } } ~~~ The former is a very simple behavior, but it illustrates how to create a behavior, now let’s add this behavior to a model: 前者是一個非常簡單的行為,但是它演示了如何創建一個行為,現在讓我們將這個行為添加到一個模型中: ~~~ <?php use Phalcon\Mvc\Model; class Profiles extends Model { public function initialize() { $this->addBehavior( new Blameable() ); } } ~~~ A behavior is also capable of intercepting missing methods on your models: 一個行為也可以攔截你的模型中缺失的方法: ~~~ <?php use Phalcon\Tag; use Phalcon\Mvc\Model\Behavior; use Phalcon\Mvc\Model\BehaviorInterface; class Sluggable extends Behavior implements BehaviorInterface { public function missingMethod($model, $method, $arguments = []) { // If the method is 'getSlug' convert the title if ($method === "getSlug") { return Tag::friendlyTitle($model->title); } } } ~~~ Call that method on a model that implements Sluggable returns a SEO friendly title: 在一個實現了“Sluggable”的模型上調用該方法,將會返回一個SEO友好的標題: ~~~ <?php $title = $post->getSlug(); ~~~ #### 使用 Traits 實現行為(Using Traits as behaviors) Starting from PHP 5.4 you can use Traits to re-use code in your classes, this is another way to implement custom behaviors. The following trait implements a simple version of the Timestampable behavior: 從PHP 5.4開始,可以使用Traits在類中重用代碼,這是實現自定義行為的另一種方法。下面的Traits實現了一個簡單的時間戳行為: ~~~ <?php trait MyTimestampable { public function beforeCreate() { $this->created_at = date("r"); } public function beforeUpdate() { $this->updated_at = date("r"); } } ~~~ Then you can use it in your model as follows: 然后你可以在你的模型中使用它: ~~~ <?php use Phalcon\Mvc\Model; class Products extends Model { use MyTimestampable; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看