首先看完整代碼 然后分部分解釋
CommentModel.php
~~~
<?php
namespace plugins\d_comment\model;
use think\Model;
class CommentModel extends Model
{
/**
* 關聯 user表
* @return $this
*/
public function user()
{
return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
}
/**
* 關聯 user表
* @return $this
*/
public function toUser()
{
return $this->belongsTo('UserModel', 'to_user_id')->setEagerlyType(1);
}
/**
* content 自動轉化
* @param $value
* @return string
*/
public function getContentAttr($value)
{
return cmf_replace_content_file_url(htmlspecialchars_decode($value));
}
/**
* content 自動轉化
* @param $value
* @return string
*/
public function setContentAttr($value)
{
$config = \HTMLPurifier_Config::createDefault();
if (!file_exists(RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer')) {
mkdir(RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer');
}
$config->set('Cache.SerializerPath', RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer');
$purifier = new \HTMLPurifier($config);
$cleanHtml = $purifier->purify(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
return htmlspecialchars($cleanHtml);
}
}
~~~
解釋部分:
~~~
/**
* 關聯 user表
* @return $this
*/
public function user()
{
return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
}
/**
* 關聯 user表
* @return $this
*/
public function toUser()
{
return $this->belongsTo('UserModel', 'to_user_id')->setEagerlyType(1);
}
~~~
首先我們定義了兩個方法,用來取得用戶表信息。
對應Comment表的user_id,to_user_id這兩個外鍵字段
對于hasMany的講解
[thinkphp一對多關聯](http://www.hmoore.net/manual/thinkphp5/142358)
setEagerlyType
設置預載入方式
$type 預載入方式 0 JOIN查詢 1 IN查詢
[in和join的效率哪個高](http://blog.csdn.net/qq_33290787/article/details/51931261)
~~~
public function getContentAttr($value)
{
return cmf_replace_content_file_url(htmlspecialchars_decode($value));
}
~~~
PHP htmlspecialchars_decode() 函數
htmlspecialchars_decode() 函數是 htmlspecialchars() 函數的反函數。
htmlspecialchars_decode() 函數把一些預定義的 HTML 實體轉換為字符。
>
> 替換編輯器內容中的文件地址
> @param string $content 編輯器內容
> @param boolean $isForDbSave true:表示把絕對地址換成相對地址,用于數據庫保存,false:表示把相對地址換成絕對地址用于界面顯示
> @return string
>
> function cmf_replace_content_file_url($content, $isForDbSave = false)
~~~
public function setContentAttr($value)
{
$config = \HTMLPurifier_Config::createDefault();
if (!file_exists(RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer')) {
mkdir(RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer');
}
$config->set('Cache.SerializerPath', RUNTIME_PATH . 'HTMLPurifier_DefinitionCache_Serializer');
$purifier = new \HTMLPurifier($config);
$cleanHtml = $purifier->purify(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
return htmlspecialchars($cleanHtml);
}
~~~
Cache.SerializerPath是 PHP富文本HTML過濾器(HTMLPurifier HTML)的一個配置參數
作用是存儲沒有斜杠的,被序列化的絕對路徑
htmlspecialchars php函數
把預定義的字符 "<" (小于)和 ">" (大于)轉換為 HTML 實體
- php套路
- 套路之類結構
- thinkphp分塊解析之Collection
- thinkphp基礎之collection
- Collection在thinkphp中的運用
- thinkcmf模塊分析
- Controller按界面點擊順序排列表
- user模塊-Controller分析
- portal模塊-Controller分析
- admin模塊-Controller分析
- user模塊-腦圖
- portal模塊-腦圖
- admin模塊-腦圖
- cmf公共函數解析-common.php
- thinkcmf點滴記錄
- 自定義標簽詳解
- 插件
- 系統信息插件
- 插件演示插件
- 留言板插件
- 留言板1 建立胚胎
- 留言板1-1 數據庫變化
- 留言板1-2 自定義鉤子
- 留言板2 連接數據庫
- 留言板3 讀取后臺界面數據
- 留言板4 前端模板
- 留言板5 分離cssjs文件
- 留言板6 驗證
- 留言板7 圖形驗證碼
- 留言板8 后臺留言列表頁
- 留言板9 后記
- 評論插件
- 1 分析數據表
- 2 CommentModel.php
- 3 UserModel.php
- 4 DCommentPlugin.php
- 前端調用代碼
- 喜歡插件
- 1 分析
- 2 收藏功能
- 3 插件建模
- 4 數據庫設計
- 5 插入一條數據
- 多語言
- thinkphp多語言
- thinkcmf多語言