Yii Framework v1.1.10 绫诲弬鑰

CCodeModel

system.gii
缁ф壙 abstract class CCodeModel » CFormModel » CModel » CComponent
瀹炵幇 IteratorAggregate, Traversable, ArrayAccess
婧愯嚜 1.1.2
鐗堟湰 $Id: CCodeModel.php 3547 2012-01-24 10:07:28Z mdomba $
婧愮爜 framework/gii/CCodeModel.php
CCodeModel鏄唬鐮佺敓鎴愭椂鐢ㄥ埌鐨勫悇绉嶆ā鍨嬬被鐨勫熀绫汇

姣忎竴涓唬鐮佺敓鎴愬櫒鑷冲皯闇瑕佹湁涓涓粠姝ょ被鎵╁睍鑰屾潵鐨勪唬鐮佹ā鍨嬬被銆 涓涓唬鐮佹ā鍨嬬殑浣滅敤鏄〃绀虹敤鎴锋彁渚涚殑鍙傛暟锛屽苟浣跨敤瀹冧滑鏉ョ敓鎴愯嚜 瀹氫箟鐨勪唬鐮併

娲剧敓绫婚渶瑕佸疄鐜prepare鏂规硶锛屾鏂规硶鐨勪富瑕佷换鍔℃槸 鍩轰簬鐢ㄦ埛鍙傛暟濉厖files灞炴с

files灞炴у皢琚嫢骞CCodeFile瀹炰緥缁勬垚鐨勯泦鍚堝~鍏咃紝 姣忎釜瀹炰緥琛ㄧず灏嗙敓鎴愮殑涓涓崟涓鐨勪唬鐮佹枃浠躲

CCodeModel瀹炵幇浜嗙矘杩炲睘鎬э紙sticky attributes锛夌殑鍔熻兘銆備竴涓矘杩 灞炴у彲浠ヨ蹇嗗畠鐨勬渶鍚庣殑鏈夋晥鍊硷紝鍗充娇鐢ㄦ埛宸茬粡鍏抽棴鐒跺悗鍐嶆墦寮娴忚鍣ㄣ 瑕佸畾涔変竴涓睘鎬т负绮樿繛灞炴э紝鍙渶瑕佺畝鍗曞湴灏嗗畠鍔犲叆鍒板悕涓衡渟ticky鈥 鐨勯獙璇佸櫒鐨勯獙璇佽鍒欏垪琛ㄤ腑銆

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
answers array 鐢ㄦ埛瀵规槸鍚︿互鏂扮敓鎴愮殑浠g爜鏂囦欢瑕嗙洊鍘熸湁鐨勪唬鐮佹枃浠剁殑纭淇℃伅銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆 CCodeModel
attributes array Returns all attribute values. CModel
errors array Returns the errors for all attribute or a single attribute. CModel
files array CCodeFile瀵硅薄鐨勫垪琛紝琛ㄧず灏嗚鐢熸垚鐨勪唬鐮佹枃浠躲 prepare()鏂规硶璐熻矗濉厖姝ゅ睘鎬с CCodeModel
iterator CMapIterator Returns an iterator for traversing the attributes in the model. CModel
keywords CCodeModel
safeAttributeNames array Returns the attribute names that are safe to be massively assigned. CModel
scenario string Returns the scenario that this model is used in. CModel
status integer 姝ゆā鍨嬬殑鐘舵併 姝ゅ睘鎬у奸氳繃CCodeGenerator鍐呴儴绠$悊銆 CCodeModel
stickyFile string 瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆 CCodeModel
template string 鐢ㄦ埛閫夋嫨鐨勪唬鐮佹ā鏉跨殑鍚嶅瓧銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆 CCodeModel
templatePath string 鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘 CCodeModel
templates array 杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 CCodeModel
validatorList CList Returns all the validators declared in the model. CModel
validators array Returns the validators applicable to the current scenario. CModel

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犳柟娉曘 CFormModel
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
addError() Adds a new error to the specified attribute. CModel
addErrors() Adds a list of errors. CModel
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
attributeLabels() 澹版槑姝ゆā鍨嬪睘鎬х殑鏍囩銆 CCodeModel
attributeNames() 杩斿洖灞炴у悕鍒楄〃銆 CFormModel
behaviors() Returns a list of behaviors that this model should behave as. CModel
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
class2id() 灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓 HTML ID銆 CCodeModel
class2name() 灏嗕竴涓被鐨勫悕瀛楄浆鎹负浠ョ┖鐧藉垎闅旂殑鍗曡瘝銆 CCodeModel
class2var() 灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓瀛楁瘝灏忓啓鐨勫彉閲忓悕銆 CCodeModel
classExists() 妫娴嬬粰瀹氬悕瀛楃殑绫绘槸鍚﹀瓨鍦紙瀵瑰ぇ灏忓啓鏁忔劅锛夈 CCodeModel
clearErrors() Removes errors for all attributes or a single attribute. CModel
confirmed() CCodeModel
createValidators() Creates validator objects based on the specification in rules. CModel
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
errorMessage() 杩斿洖褰撲唬鐮佹枃浠朵繚瀛樻湡闂村彂鐢熼敊璇椂闇瑕佹樉绀虹殑淇℃伅銆 CCodeModel
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
generateAttributeLabel() Generates a user friendly attribute label. CModel
getAttributeLabel() Returns the text label for the specified attribute. CModel
getAttributes() Returns all attribute values. CModel
getError() Returns the first error of the specified attribute. CModel
getErrors() Returns the errors for all attribute or a single attribute. CModel
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getIterator() Returns an iterator for traversing the attributes in the model. CModel
getSafeAttributeNames() Returns the attribute names that are safe to be massively assigned. CModel
getScenario() Returns the scenario that this model is used in. CModel
getStickyFile() 杩斿洖瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆 CCodeModel
getTemplatePath() 杩斿洖鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘 CCodeModel
getTemplates() 杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 CCodeModel
getValidatorList() Returns all the validators declared in the model. CModel
getValidators() Returns the validators applicable to the current scenario. CModel
hasErrors() Returns a value indicating whether there is any validation error. CModel
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() 鍒濆鍖栨ā鍨嬨 CFormModel
isAttributeRequired() Returns a value indicating whether the attribute is required. CModel
isAttributeSafe() Returns a value indicating whether the attribute is safe for massive assignments. CModel
loadStickyAttributes() 浠庢枃浠朵腑杞藉叆绮樿繛灞炴у肩劧鍚庡皢瀹冧滑濉厖鍒版ā鍨嬩腑銆 CCodeModel
offsetExists() Returns whether there is an element at the specified offset. CModel
offsetGet() Returns the element at the specified offset. CModel
offsetSet() Sets the element at the specified offset. CModel
offsetUnset() Unsets the element at the specified offset. CModel
onAfterConstruct() This event is raised after the model instance is created by new operator. CModel
onAfterValidate() This event is raised after the validation is performed. CModel
onBeforeValidate() This event is raised before the validation is performed. CModel
onUnsafeAttribute() This method is invoked when an unsafe attribute is being massively assigned. CModel
pluralize() 灏嗕竴涓崟璇嶈浆鎹负澶嶆暟褰㈠紡銆 CCodeModel
prepare() 鍑嗗灏嗙敓鎴愮殑浠g爜鏂囦欢銆 CCodeModel
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
render() 浣跨敤鎸囧畾鐨勪唬鐮佹ā鏉挎枃浠剁敓鎴愪唬鐮併 CCodeModel
renderResults() CCodeModel
requiredTemplates() 杩斿洖蹇呴渶鐨勪唬鐮佹ā鏉跨殑鍒楄〃銆 CCodeModel
rules() 澹版槑姝ゆā鍨嬬殑楠岃瘉瑙勫垯銆 CCodeModel
save() 灏嗙敓鎴愮殑浠g爜淇濆瓨鍒拌嫢骞叉枃浠朵腑銆 CCodeModel
saveStickyAttributes() 淇濆瓨绮樿繛灞炴у煎埌涓涓枃浠朵腑銆 CCodeModel
setAttributes() Sets the attribute values in a massive way. CModel
setScenario() Sets the scenario for the model. CModel
sticky() 绮樿繛楠岃瘉鍣紙The "sticky" validator锛夈 CCodeModel
successMessage() 杩斿洖褰撴柊鐢熸垚鐨勪唬鐮佹垚鍔熶繚瀛樺埌鏂囦欢涓椂闇瑕佹樉绀虹殑淇℃伅銆 CCodeModel
unsetAttributes() Sets the attributes to be null. CModel
validate() Performs the validation. CModel
validateReservedWord() 楠岃瘉涓涓睘鎬ф病鏈変娇鐢≒HP鐨勪繚鐣欏叧閿瓧銆 CCodeModel
validateTemplate() 楠岃瘉妯℃澘鐨勯夋嫨銆 CCodeModel

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
afterConstruct() This method is invoked after a model instance is created by new operator. CModel
afterValidate() This method is invoked after validation ends. CModel
beforeValidate() This method is invoked before validation starts. CModel

浜嬩欢

闅愯棌缁ф壙浜嬩欢

浜嬩欢鎻忚堪瀹氫箟鍦
onAfterConstruct This event is raised after the model instance is created by new operator. CModel
onBeforeValidate This event is raised before the validation is performed. CModel
onAfterValidate This event is raised after the validation is performed. CModel
onUnsafeAttribute This method is invoked when an unsafe attribute is being massively assigned. CModel

灞炴ц缁

answers 灞炴
public array $answers;

鐢ㄦ埛瀵规槸鍚︿互鏂扮敓鎴愮殑浠g爜鏂囦欢瑕嗙洊鍘熸湁鐨勪唬鐮佹枃浠剁殑纭淇℃伅銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆

files 灞炴
public array $files;

CCodeFile瀵硅薄鐨勫垪琛紝琛ㄧず灏嗚鐢熸垚鐨勪唬鐮佹枃浠躲 prepare()鏂规硶璐熻矗濉厖姝ゅ睘鎬с

keywords 灞炴
public static $keywords;

status 灞炴
public integer $status;

姝ゆā鍨嬬殑鐘舵併 姝ゅ睘鎬у奸氳繃CCodeGenerator鍐呴儴绠$悊銆

stickyFile 灞炴 鍙
public string getStickyFile()

瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆

template 灞炴
public string $template;

鐢ㄦ埛閫夋嫨鐨勪唬鐮佹ā鏉跨殑鍚嶅瓧銆 姝ゅ睘鎬х殑鍊奸氳繃姝ょ被鍜CCodeGenerator鍐呴儴绠$悊銆

templatePath 灞炴 鍙
public string getTemplatePath()

鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘

templates 灞炴 鍙
public array getTemplates()

杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 姝ゆ柟娉曠畝鍗曞湴杩斿洖CCodeGenerator::templates灞炴у笺

鏂规硶璇︾粏

attributeLabels() 鏂规硶
public array attributeLabels()
{return} array 灞炴х殑鏍囩
婧愮爜锛 framework/gii/CCodeModel.php#215 (鏄剧ず)
public function attributeLabels()
{
    return array(
        
'template'=>'Code Template',
    );
}

澹版槑姝ゆā鍨嬪睘鎬х殑鏍囩銆 瀛愮被蹇呴』鎸変笅鍒楁牸寮忚鐩栨鏂规硶锛

return array_merge(parent::attributeLabels(), array(
    ...labels for the child class attributes...
));

class2id() 鏂规硶
public string class2id(string $name)
$name string 闇瑕佽浆鎹㈢殑瀛楃涓
{return} string 杞崲鐨勭粨鏋
婧愮爜锛 framework/gii/CCodeModel.php#440 (鏄剧ず)
public function class2id($name)
{
    return 
trim(strtolower(str_replace('_','-',preg_replace('/(?<![A-Z])[A-Z]/''-\0'$name))),'-');
}

灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓 HTML ID銆 渚嬪锛'PostTag'灏嗚浆鎹负'post-tag'銆

class2name() 鏂规硶
public string class2name(string $name, boolean $ucwords=true)
$name string 闇瑕佹暟鐨勭殑瀛楃涓层
$ucwords boolean 鏄惁灏嗘瘡涓崟璇嶇殑棣栧瓧姣嶈浆涓哄ぇ鍐
{return} string 杞崲鐨勭粨鏋
婧愮爜锛 framework/gii/CCodeModel.php#452 (鏄剧ず)
public function class2name($name,$ucwords=true)
{
    
$result=trim(strtolower(str_replace('_',' ',preg_replace('/(?<![A-Z])[A-Z]/'' \0'$name))));
    return 
$ucwords ucwords($result) : $result;
}

灏嗕竴涓被鐨勫悕瀛楄浆鎹负浠ョ┖鐧藉垎闅旂殑鍗曡瘝銆 渚嬪锛'PostTag'灏嗚浆鎹负'Post Tag'銆

class2var() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.4锛
public string class2var(string $name)
$name string 绫荤殑鍚嶅瓧
{return} string 浠庣被鐨勫悕瀛楄浆鎹㈣屾潵鐨勫彉閲忓悕瀛
婧愮爜锛 framework/gii/CCodeModel.php#465 (鏄剧ず)
public function class2var($name)
{
    
$name[0]=strtolower($name[0]);
    return 
$name;
}

灏嗕竴涓被鐨勫悕瀛楄浆鎹负涓涓瀛楁瘝灏忓啓鐨勫彉閲忓悕銆 涔嬫墍浠ユ彁渚涙鏂规硶锛屾槸鍥犱负PHP鍑芥暟lcfirst()鍙湪 PHP5.3+ 鍙敤銆

classExists() 鏂规硶
public boolean classExists(string $name)
$name string 闇瑕佹娴嬬殑绫荤殑鍚嶅瓧
{return} boolean 姝ょ被鏄惁瀛樺湪
婧愮爜锛 framework/gii/CCodeModel.php#200 (鏄剧ず)
public function classExists($name)
{
    return 
class_exists($name,false) && in_array($nameget_declared_classes());
}

妫娴嬬粰瀹氬悕瀛楃殑绫绘槸鍚﹀瓨鍦紙瀵瑰ぇ灏忓啓鏁忔劅锛夈

confirmed() 鏂规硶
public void confirmed(CCodeFile $file)
$file CCodeFile 灏嗚淇濆瓨鐨勪唬鐮佹枃浠
婧愮爜锛 framework/gii/CCodeModel.php#296 (鏄剧ず)
public function confirmed($file)
{
    return 
$this->answers===null && $file->operation===CCodeFile::OP_NEW
        
|| is_array($this->answers) && isset($this->answers[md5($file->path)]);
}

errorMessage() 鏂规硶
public string errorMessage()
{return} string 褰撲唬鐮佹枃浠朵繚瀛樻湡闂村彂鐢熼敊璇椂闇瑕佹樉绀虹殑淇℃伅銆
婧愮爜锛 framework/gii/CCodeModel.php#262 (鏄剧ず)
public function errorMessage()
{
    return 
'There was some error when generating the code. Please check the following messages.';
}

杩斿洖褰撲唬鐮佹枃浠朵繚瀛樻湡闂村彂鐢熼敊璇椂闇瑕佹樉绀虹殑淇℃伅銆 濡傛灉瀛愮被闇瑕佽嚜瀹氫箟閿欒鎻愮ず淇℃伅灏辫鐩栨鏂规硶銆

getStickyFile() 鏂规硶
public string getStickyFile()
{return} string 瀛樺偍绮樿繛灞炴у肩殑鏂囦欢璺緞銆
婧愮爜锛 framework/gii/CCodeModel.php#393 (鏄剧ず)
public function getStickyFile()
{
    return 
Yii::app()->runtimePath.'/gii-'.Yii::getVersion().'/'.get_class($this).'.php';
}

getTemplatePath() 鏂规硶
public string getTemplatePath()
{return} string 鍖呭惈妯℃澘鏂囦欢鐨勭洰褰曘
婧愮爜锛 framework/gii/CCodeModel.php#281 (鏄剧ず)
public function getTemplatePath()
{
    
$templates=$this->getTemplates();
    if(isset(
$templates[$this->template]))
        return 
$templates[$this->template];
    else if(empty(
$templates))
        throw new 
CHttpException(500,'No templates are available.');
    else
        throw new 
CHttpException(500,'Invalid template selection.');

}

getTemplates() 鏂规硶
public array getTemplates()
{return} array 鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆
婧愮爜锛 framework/gii/CCodeModel.php#272 (鏄剧ず)
public function getTemplates()
{
    return 
Yii::app()->controller->templates;
}

杩斿洖鍙敤鐨勪唬鐮佹ā鏉跨殑鍒楄〃(name=>directory)銆 姝ゆ柟娉曠畝鍗曞湴杩斿洖CCodeGenerator::templates灞炴у笺

loadStickyAttributes() 鏂规硶
public void loadStickyAttributes()
婧愮爜锛 framework/gii/CCodeModel.php#361 (鏄剧ず)
public function loadStickyAttributes()
{
    
$this->_stickyAttributes=array();
    
$path=$this->getStickyFile();
    if(
is_file($path))
    {
        
$result=@include($path);
        if(
is_array($result))
        {
            
$this->_stickyAttributes=$result;
            foreach(
$this->_stickyAttributes as $name=>$value)
            {
                if(
property_exists($this,$name) || $this->canSetProperty($name))
                    
$this->$name=$value;
            }
        }
    }
}

浠庢枃浠朵腑杞藉叆绮樿繛灞炴у肩劧鍚庡皢瀹冧滑濉厖鍒版ā鍨嬩腑銆

pluralize() 鏂规硶
public string pluralize(string $name)
$name string 闇瑕佽浆涓哄鏁扮殑鍗曡瘝
{return} string 鍗曡瘝鐨勫鏁
婧愮爜锛 framework/gii/CCodeModel.php#405 (鏄剧ず)
public function pluralize($name)
{
    
$rules=array(
        
'/move$/i' => 'moves',
        
'/foot$/i' => 'feet',
        
'/child$/i' => 'children',
        
'/human$/i' => 'humans',
        
'/man$/i' => 'men',
        
'/tooth$/i' => 'teeth',
        
'/person$/i' => 'people',
        
'/([m|l])ouse$/i' => '\1ice',
        
'/(x|ch|ss|sh|us|as|is|os)$/i' => '\1es',
        
'/([^aeiouy]|qu)y$/i' => '\1ies',
        
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
        
'/(shea|lea|loa|thie)f$/i' => '\1ves',
        
'/([ti])um$/i' => '\1a',
        
'/(tomat|potat|ech|her|vet)o$/i' => '\1oes',
        
'/(bu)s$/i' => '\1ses',
        
'/(ax|test)is$/i' => '\1es',
        
'/s$/' => 's',
    );
    foreach(
$rules as $rule=>$replacement)
    {
        if(
preg_match($rule,$name))
            return 
preg_replace($rule,$replacement,$name);
    }
    return 
$name.'s';
}

灏嗕竴涓崟璇嶈浆鎹负澶嶆暟褰㈠紡銆 娉ㄦ剰锛氭鏂规硶浠呴拡瀵硅嫳璇紒 渚嬪锛'apple'灏嗚浆鎹负'apples'锛岃'child'灏嗚浆鎹负'children'銆

prepare() 鏂规硶
abstract public void prepare()
婧愮爜锛 framework/gii/CCodeModel.php#151 (鏄剧ず)
abstract public function prepare();

鍑嗗灏嗙敓鎴愮殑浠g爜鏂囦欢銆 杩欐槸瀛愮被闇瑕佸疄鐜扮殑涓昏鏂规硶锛屽畠鍖呭惈浜嗕娇鐢ㄥ皢瑕佺敓鎴 鐨勪唬鐮佹枃浠剁殑鍒楄〃濉厖files灞炴х殑閫昏緫銆

render() 鏂规硶
public string render(string $templateFile, array $_params_=NULL)
$templateFile string 浠g爜妯℃澘鏂囦欢璺緞
$_params_ array 琚彁鍙栧埌浠g爜妯℃澘涓娇鐢ㄧ殑鍙傛暟鐨勯泦鍚
{return} string 鐢熸垚鐨勪唬鐮
婧愮爜锛 framework/gii/CCodeModel.php#309 (鏄剧ず)
public function render($templateFile,$_params_=null)
{
    if(!
is_file($templateFile))
        throw new 
CException("The template file '$templateFile' does not exist.");

    if(
is_array($_params_))
        
extract($_params_,EXTR_PREFIX_SAME,'params');
    else
        
$params=$_params_;
    
ob_start();
    
ob_implicit_flush(false);
    require(
$templateFile);
    return 
ob_get_clean();
}

浣跨敤鎸囧畾鐨勪唬鐮佹ā鏉挎枃浠剁敓鎴愪唬鐮併 姝ゆ柟娉曚富瑕佸湪generate涓鐢ㄤ簬鐢熸垚浠g爜銆

renderResults() 鏂规硶
public string renderResults()
{return} string 浠g爜鐢熸垚鐨勭粨鏋滄棩蹇椼
婧愮爜锛 framework/gii/CCodeModel.php#327 (鏄剧ず)
public function renderResults()
{
    
$output='Generating code using template "'.$this->templatePath."\"...\n";
    foreach(
$this->files as $file)
    {
        if(
$file->error!==null)
            
$output.="<span class=\"error\">generating {$file->relativePath}<br/>           {$file->error}</span>\n";
        else if(
$file->operation===CCodeFile::OP_NEW && $this->confirmed($file))
            
$output.=' generated '.$file->relativePath."\n";
        else if(
$file->operation===CCodeFile::OP_OVERWRITE && $this->confirmed($file))
            
$output.=' overwrote '.$file->relativePath."\n";
        else
            
$output.='   skipped '.$file->relativePath."\n";
    }
    
$output.="done!\n";
    return 
$output;
}

requiredTemplates() 鏂规硶
public array requiredTemplates()
{return} array 蹇呴渶鐨勪唬鐮佹ā鏉跨殑鍒楄〃锛屽畠浠槸涓 templatePath鐩稿叧鑱旂殑鏂囦欢璺緞銆
婧愮爜锛 framework/gii/CCodeModel.php#228 (鏄剧ず)
public function requiredTemplates()
{
    return array();
}

杩斿洖蹇呴渶鐨勪唬鐮佹ā鏉跨殑鍒楄〃銆 娲剧敓绫婚氬父闇瑕佽鐩栨鏂规硶銆

rules() 鏂规硶
public array rules()
{return} array 楠岃瘉瑙勫垯
婧愮爜锛 framework/gii/CCodeModel.php#163 (鏄剧ず)
public function rules()
{
    return array(
        array(
'template''required'),
        array(
'template''validateTemplate''skipOnError'=>true),
        array(
'template''sticky'),
    );
}

澹版槑姝ゆā鍨嬬殑楠岃瘉瑙勫垯銆 瀛愮被蹇呴』鎸変笅鍒楁牸寮忚鐩栨鏂规硶锛

return array_merge(parent::rules(), array(
    ...rules for the child class...
));

save() 鏂规硶
public void save()
婧愮爜锛 framework/gii/CCodeModel.php#236 (鏄剧ず)
public function save()
{
    
$result=true;
    foreach(
$this->files as $file)
    {
        if(
$this->confirmed($file))
            
$result=$file->save() && $result;
    }
    return 
$result;
}

灏嗙敓鎴愮殑浠g爜淇濆瓨鍒拌嫢骞叉枃浠朵腑銆

saveStickyAttributes() 鏂规硶
public void saveStickyAttributes()
婧愮爜锛 framework/gii/CCodeModel.php#383 (鏄剧ず)
public function saveStickyAttributes()
{
    
$path=$this->getStickyFile();
    @
mkdir(dirname($path),0755,true);
    
file_put_contents($path,"<?php\nreturn ".var_export($this->_stickyAttributes,true).";\n");
}

淇濆瓨绮樿繛灞炴у煎埌涓涓枃浠朵腑銆

sticky() 鏂规硶
public void sticky(string $attribute, array $params)
$attribute string 闇瑕侀獙璇佺殑灞炴
$params array 楠岃瘉鍙傛暟
婧愮爜锛 framework/gii/CCodeModel.php#352 (鏄剧ず)
public function sticky($attribute,$params)
{
    if(!
$this->hasErrors())
        
$this->_stickyAttributes[$attribute]=$this->$attribute;
}

绮樿繛楠岃瘉鍣紙The "sticky" validator锛夈 姝ら獙璇佸櫒骞朵笉鐪熺殑瀵瑰睘鎬ц繘琛岄獙璇侊紝 瀹冨疄闄呬笂鏄皢姝ゅ睘鎬х殑鍊间繚瀛樺湪涓涓枃浠朵腑浠ヤ娇灞炴ф敮鎸佺矘杩炲姛鑳姐

successMessage() 鏂规硶
public string successMessage()
{return} string 褰撴柊鐢熸垚鐨勪唬鐮佹垚鍔熶繚瀛樺埌鏂囦欢涓椂闇瑕佹樉绀虹殑淇℃伅銆
婧愮爜锛 framework/gii/CCodeModel.php#252 (鏄剧ず)
public function successMessage()
{
    return 
'The code has been generated successfully.';
}

杩斿洖褰撴柊鐢熸垚鐨勪唬鐮佹垚鍔熶繚瀛樺埌鏂囦欢涓椂闇瑕佹樉绀虹殑淇℃伅銆 濡傛灉瀛愮被闇瑕佽嚜瀹氫箟鎻愮ず淇℃伅灏辫鐩栨鏂规硶銆

validateReservedWord() 鏂规硶
public void validateReservedWord(string $attribute, array $params)
$attribute string 闇瑕侀獙璇佺殑灞炴
$params array 楠岃瘉鍙傛暟
婧愮爜锛 framework/gii/CCodeModel.php#476 (鏄剧ず)
public function validateReservedWord($attribute,$params)
{
    
$value=$this->$attribute;
    if(
in_array(strtolower($value),self::$keywords))
        
$this->addError($attribute$this->getAttributeLabel($attribute).' cannot take a reserved PHP keyword.');
}

楠岃瘉涓涓睘鎬ф病鏈変娇鐢≒HP鐨勪繚鐣欏叧閿瓧銆

validateTemplate() 鏂规硶
public void validateTemplate(string $attribute, array $params)
$attribute string 闇瑕侀獙璇佺殑灞炴
$params array 楠岃瘉鍙傛暟
婧愮爜锛 framework/gii/CCodeModel.php#179 (鏄剧ず)
public function validateTemplate($attribute,$params)
{
    
$templates=$this->templates;
    if(!isset(
$templates[$this->template]))
        
$this->addError('template''Invalid template selection.');
    else
    {
        
$templatePath=$this->templatePath;
        foreach(
$this->requiredTemplates() as $template)
        {
            if(!
is_file($templatePath.'/'.$template))
                
$this->addError('template'"Unable to find the required code template file '$template'.");
        }
    }
}

楠岃瘉妯℃澘鐨勯夋嫨銆 姝ゆ柟娉曢獙璇佺敤鎴锋槸鍚﹂夋嫨浜嗕竴涓瓨鍦ㄧ殑妯℃澘锛屼笖姝ゆā鏉垮寘鍚湁 鎵鏈夊湪requiredTemplates涓寚瀹氱殑蹇呴渶鐨勬ā鏉挎枃浠躲

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.