<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 模型行為 行為是多個模型可以采用的共享結構,以便重用代碼。ORM提供了一個API來實現模型中的行為。此外,您可以使用前面提到的事件和回調作為更自由地實現行為的替代方法。 必須在模型初始值設定項中添加行為,模型可以包含零個或多個行為: ```php <?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', ] ] ) ); } } ``` 框架提供以下內置行為: | 名稱 |描述 | | ------------- | ---------------------------------------------------------------------------------------------------------- | | Timestampable |允許在創建或更新記錄時自動更新模型的屬性,以保存日期時間 | | SoftDelete | 它不是永久刪除記錄,而是將記錄標記為已刪除,從而更改標志列的值 | ## 時間戳 此行為接收一組選項,第一級鍵必須是一個事件名稱,指示何時必須分配列: ```php <?php use Phalcon\Mvc\Model\Behavior\Timestampable; public function initialize() { $this->addBehavior( new Timestampable( [ 'beforeCreate' => [ 'field' => 'created_at', 'format' => 'Y-m-d', ] ] ) ); } ``` 每個事件都有自己的選項,`field`是必須更新的列的名稱,如果`format`是一個字符串,它將被用作PHP [date](http://php.net/manual/en/function.date.php) 函數的格式,`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'); } ] ] ) ); } ``` 如果省略選項`format` ,將使用PHP [time](http://php.net/manual/en/function.time.php) 函數的時間戳。 ## 軟刪除 此行為可以使用如下: ```php <?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, ] ) ); } } ``` 此行為接受兩個選項:`field`和`value`,`field`確定必須更新的字段并為要刪除的`value`賦值。讓我們假裝`users`表有以下數據: ```sql mysql> select * from users; +----+---------+--------+ | id | name | status | +----+---------+--------+ | 1 | Lana | N | | 2 | Brandon | N | +----+---------+--------+ 2 rows in set (0.00 sec) ``` 如果我們刪除兩條記錄中的任何一條,狀態將被更新而不是刪除記錄: ```php <?php Users::findFirst(2)->delete(); ``` 該操作將導致表中的以下數據: ```sql mysql> select * from users; +----+---------+--------+ | id | name | status | +----+---------+--------+ | 1 | Lana | N | | 2 | Brandon | D | +----+---------+--------+ 2 rows in set (0.01 sec) ``` 請注意,您需要在查詢中指定已刪除的條件,以便將它們有效地忽略為已刪除的記錄,此行為不支持此操作。 ## 創建自己的行為 ORM提供了一個API來創建自己的行為。行為必須是實現`Phalcon\Mvc\Model\BehaviorInterface`的類。此外, `Phalcon\Mvc\Model\Behavior`提供了簡化行為實現所需的大多數方法。 以下行為是一個示例,它實現了Blameable行為,該行為有助于識別對模型執行操作的用戶: ```php <?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 */ } } } ``` 前者是一個非常簡單的行為,但它說明了如何創建行為,現在讓我們將此行為添加到模型中: ```php <?php use Phalcon\Mvc\Model; class Profiles extends Model { public function initialize() { $this->addBehavior( new Blameable() ); } } ``` 行為還能夠攔截模型上缺少的方法: ```php <?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); } } } ``` 在實現Sluggable的模型上調用該方法返回一個SEO友好標題: ```php <?php $title = $post->getSlug(); ``` ## 使用Traits作為行為 您可以使用[Traits](http://php.net/manual/en/language.oop5.traits.php)重用類中的代碼,這是實現自定義行為的另一種方法。以下特征實現了Timestampable行為的簡單版本: ```php <?php trait MyTimestampable { public function beforeCreate() { $this->created_at = date('r'); } public function beforeUpdate() { $this->updated_at = date('r'); } } ``` 然后您可以在模型中使用它,如下所示: ```php <?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>

                              哎呀哎呀视频在线观看