CActiveRecord
鍖 | system.db.ar |
---|---|
缁ф壙 | abstract class CActiveRecord » CModel » CComponent |
瀹炵幇 | ArrayAccess, Traversable, IteratorAggregate |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CActiveRecord.php 3533 2012-01-08 22:07:55Z mdomba $ |
婧愮爜 | framework/db/ar/CActiveRecord.php |
瀹冨疄鐜颁簡娲诲姩璁板綍璁捐妯″紡锛屼竴绉嶆祦琛岀殑瀵硅薄鍏崇郴鏄犲皠锛圤RM锛夋妧鏈 璇风偣鍑the Guide鏇村鍏充簬杩欎釜绫荤殑 璇︾粏淇℃伅銆
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
attributes | array | 杩斿洖鎵鏈夊垪鐨勫睘鎬у笺 | CActiveRecord |
commandBuilder | CDbCommandBuilder | 杩斿洖姝R鐨勫懡浠ょ敓鎴愬櫒銆 | CActiveRecord |
db | CDbConnection | 鎵鏈夋椿鍔ㄨ褰曠被鐨勯粯璁ゆ暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝杩欐槸'db'搴旂敤绋嬪簭缁勪欢锛 | CActiveRecord |
dbConnection | CDbConnection | 杩斿洖娲诲姩璁板綍鎵浣跨敤鐨勬暟鎹簱杩炴帴銆 | CActiveRecord |
dbCriteria | CDbCriteria | 杩斿洖鍜岃繖涓ā鍨嬬浉鍏崇殑鏌ヨ鏉′欢銆 | CActiveRecord |
errors | array | Returns the errors for all attribute or a single attribute. | CModel |
isNewRecord | boolean | 杩斿洖褰撳墠璁板綍鏄惁鏄柊鐨勩 | CActiveRecord |
iterator | CMapIterator | Returns an iterator for traversing the attributes in the model. | CModel |
metaData | CActiveRecordMetaData | 杩斿洖杩欎釜AR鐨勫厓鏁版嵁銆 | CActiveRecord |
oldPrimaryKey | mixed | 杩斿洖鏃х殑涓婚敭鍊笺 | CActiveRecord |
primaryKey | mixed | 杩斿洖涓婚敭鍊笺 | CActiveRecord |
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 |
tableAlias | string | 杩斿洖鐢 find 鏂规硶浣跨敤琛ㄧ殑鍒悕銆 | CActiveRecord |
tableSchema | CDbTableSchema | 杩斿洖琛ㄧ殑鍏冩暟鎹紝杩欏睘浜嶢R绫汇 | CActiveRecord |
validatorList | CList | Returns all the validators declared in the model. | CModel |
validators | array | Returns the validators applicable to the current scenario. | CModel |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 鍥炶皟鐨勫懡鍚嶆柟娉曘 | CActiveRecord |
__construct() | 鏋勯犲嚱鏁般 | CActiveRecord |
__get() | PHP getter 榄旀湳鏂规硶銆 | CActiveRecord |
__isset() | 妫娴嬩竴涓睘鎬у兼槸鍚︿负null銆 | CActiveRecord |
__set() | PHP setter 榄旀湳鏂规硶銆 | CActiveRecord |
__sleep() | PHP sleep 榄旀湳鏂规硶銆 | CActiveRecord |
__unset() | 妫鏌ュ睘鎬у兼槸鍚︿负绌恒 | CActiveRecord |
addError() | Adds a new error to the specified attribute. | CModel |
addErrors() | Adds a list of errors. | CModel |
addRelatedRecord() | 涓嶈皟鐢ㄦ鏂规硶銆傛鏂规硶鐢ㄤ簬鍐呴儴CActiveFinder鏉ュ~鍏呯浉鍏崇殑瀵硅薄銆 | CActiveRecord |
afterFindInternal() | 璋冪敤afterFind銆 | CActiveRecord |
applyScopes() | 閫傜敤浜庣粰瀹氭潯浠剁殑鏌ヨ鑼冨洿銆 | CActiveRecord |
asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
attributeLabels() | Returns the attribute labels. | CModel |
attributeNames() | 杩斿洖妯″瀷鐨勬墍鏈夊睘鎬у悕绉扮殑鍒楄〃銆 | CActiveRecord |
beforeFindInternal() | 璋冪敤beforeFind銆 | CActiveRecord |
behaviors() | Returns a list of behaviors that this model should behave as. | CModel |
cache() | 璁剧疆鍏充簬鏌ヨ缂撳瓨鐨勫弬鏁. | CActiveRecord |
canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
clearErrors() | Removes errors for all attributes or a single attribute. | CModel |
count() | 鏌ユ壘婊¤冻鎸囧畾鏌ヨ鏉′欢鐨勮鏁般 | CActiveRecord |
countByAttributes() | 鏌ユ壘鍏锋湁鎸囧畾鐨勫睘鎬у肩殑琛屾暟銆 | CActiveRecord |
countBySql() | 鏌ユ壘鎸囧畾SQL璇彞褰卞搷鐨勮鏁般 | CActiveRecord |
createValidators() | Creates validator objects based on the specification in rules. | CModel |
defaultScope() | 杩斿洖榛樿鐨勫懡鍚嶈寖鍥达紝搴旈殣鍚繖绉嶆ā寮忛傜敤浜庢墍鏈夋煡璇€ | CActiveRecord |
delete() | 鍒犻櫎瀵瑰簲浜庢娲诲姩璁板綍鐨勮銆 | CActiveRecord |
deleteAll() | 鍒犻櫎鎸囧畾鏉′欢鐨勮銆 | CActiveRecord |
deleteAllByAttributes() | 鍒犻櫎鎸囧畾鐨勫睘鎬у肩浉鍖归厤鐨勮銆 | CActiveRecord |
deleteByPk() | 鍒犻櫎鎸囧畾涓婚敭鐨勮銆 | CActiveRecord |
detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
equals() | 褰撳墠娲诲姩璁板綍涓庡彟涓涓瘮杈冦 | CActiveRecord |
evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
exists() | 妫鏌ユ槸鍚︽弧瓒虫寚瀹氭潯浠剁殑琛屻 | CActiveRecord |
find() | 鏌ユ壘鎸囧畾鏉′欢鐨勫崟涓椿鍔ㄨ褰曘 | CActiveRecord |
findAll() | 鏌ユ壘婊¤冻鎸囧畾鏉′欢鐨勬墍鏈夋椿鍔ㄨ褰曘 | CActiveRecord |
findAllByAttributes() | 鏌ユ壘鍏锋湁鎸囧畾灞炴у肩殑鎵鏈夋椿鍔ㄨ褰曘 | CActiveRecord |
findAllByPk() | 鏌ユ壘鎸囧畾涓婚敭鐨勬墍鏈夋椿鍔ㄨ褰曘 | CActiveRecord |
findAllBySql() | 鏌ユ壘鎸囧畾SQL璇彞鐨勬墍鏈夋椿鍔ㄨ褰曘 | CActiveRecord |
findByAttributes() | 鏌ユ壘鍏锋湁鎸囧畾灞炴у肩殑鍗曚釜娲诲姩璁板綍銆 | CActiveRecord |
findByPk() | 鏌ユ壘鎸囧畾涓婚敭鐨勫崟涓娲诲姩璁板綍銆 | CActiveRecord |
findBySql() | 鏌ユ壘鎸囧畾SQL璇彞鐨勫崟涓椿鍔ㄨ褰曘 | CActiveRecord |
generateAttributeLabel() | Generates a user friendly attribute label. | CModel |
getActiveRelation() | 杩斿洖姝R绫诲0鏄庣殑鍛藉悕鍏崇郴銆 | CActiveRecord |
getAttribute() | 杩斿洖鍛藉悕鐨勫睘鎬у笺 | CActiveRecord |
getAttributeLabel() | 杩斿洖鎸囧畾灞炴х殑鏂囨湰鏍囩銆 | CActiveRecord |
getAttributes() | 杩斿洖鎵鏈夊垪鐨勫睘鎬у笺 | CActiveRecord |
getCommandBuilder() | 杩斿洖姝R鐨勫懡浠ょ敓鎴愬櫒銆 | CActiveRecord |
getDbConnection() | 杩斿洖娲诲姩璁板綍鎵浣跨敤鐨勬暟鎹簱杩炴帴銆 | CActiveRecord |
getDbCriteria() | 杩斿洖鍜岃繖涓ā鍨嬬浉鍏崇殑鏌ヨ鏉′欢銆 | CActiveRecord |
getError() | Returns the first error of the specified attribute. | CModel |
getErrors() | Returns the errors for all attribute or a single attribute. | CModel |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getIsNewRecord() | 杩斿洖褰撳墠璁板綍鏄惁鏄柊鐨勩 | CActiveRecord |
getIterator() | Returns an iterator for traversing the attributes in the model. | CModel |
getMetaData() | 杩斿洖杩欎釜AR鐨勫厓鏁版嵁銆 | CActiveRecord |
getOldPrimaryKey() | 杩斿洖鏃х殑涓婚敭鍊笺 | CActiveRecord |
getPrimaryKey() | 杩斿洖涓婚敭鍊笺 | CActiveRecord |
getRelated() | 杩斿洖鐩稿叧鐨勮褰曘 | CActiveRecord |
getSafeAttributeNames() | Returns the attribute names that are safe to be massively assigned. | CModel |
getScenario() | Returns the scenario that this model is used in. | CModel |
getTableAlias() | 杩斿洖鐢 find 鏂规硶浣跨敤琛ㄧ殑鍒悕銆 | CActiveRecord |
getTableSchema() | 杩斿洖琛ㄧ殑鍏冩暟鎹紝杩欏睘浜嶢R绫汇 | CActiveRecord |
getValidatorList() | Returns all the validators declared in the model. | CModel |
getValidators() | Returns the validators applicable to the current scenario. | CModel |
hasAttribute() | 妫鏌ユAR鏄惁鏈夊懡鍚嶇殑灞炴с | CActiveRecord |
hasErrors() | Returns a value indicating whether there is any validation error. | CModel |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
hasRelated() | 杩斿洖涓涓硷紝璇ュ兼寚绀烘槸鍚﹀凡鍔犺浇鍛藉悕鐨勭浉鍏冲璞° | CActiveRecord |
init() | 鍒濆鍖栬繖涓ā鍨嬨 | CActiveRecord |
insert() | 鎻掑叆涓琛屽埌鍩轰簬杩欎釜ActiveRecord鐨勫睘鎬ц〃銆 | CActiveRecord |
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 |
model() | 杩斿洖鎸囧畾AR绫荤殑闈欐佹ā鍨嬨 | CActiveRecord |
offsetExists() | 杩斿洖鏄惁鏈夋寚瀹氬亸绉婚噺鐨勫厓绱犮 | CActiveRecord |
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 |
onAfterDelete() | 鍦ㄥ垹闄よ褰曚箣鍚庯紝灏嗗紩鍙戞浜嬩欢銆 | CActiveRecord |
onAfterFind() | 寮曞彂姝や簨浠惰褰曞悗鎵惧埌鏂规硶鐨勫疄渚嬨 | CActiveRecord |
onAfterSave() | 淇濆瓨璁板綍涔嬪悗锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
onAfterValidate() | This event is raised after the validation is performed. | CModel |
onBeforeDelete() | 鍦ㄥ垹闄よ褰曚箣鍓嶏紝灏嗗紩鍙戞浜嬩欢銆 | CActiveRecord |
onBeforeFind() | AR finder鎵ц鏌ユ壘璋冪敤涔嬪墠锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
onBeforeSave() | 淇濆瓨璁板綍涔嬪墠锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
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 |
populateRecord() | 缁欐寚瀹氱殑灞炴у垱寤烘椿鍔ㄨ褰 | CActiveRecord |
populateRecords() | 鍒涘缓鍩轰簬杈撳叆鏁版嵁鐨勬椿鍔ㄨ褰曠殑鍒楄〃銆 | CActiveRecord |
primaryKey() | 杩斿洖鐩稿叧鏁版嵁搴撹〃鐨勪富閿 | CActiveRecord |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
refresh() | 鏈鏂扮殑鏁版嵁灏嗚濉厖鍒拌繖涓椿鍔ㄨ褰曘 | CActiveRecord |
refreshMetaData() | 鍒锋柊杩欎釜AR绫荤殑鍏冩暟鎹 | CActiveRecord |
relations() | 鐢虫槑鍏崇郴鐨勫璞★紝搴旈噸鍐欐鏂规硶銆 | CActiveRecord |
resetScope() | 閲嶇疆鎵鏈夊彉閲忎綔鐢ㄥ煙锛屽寘鎷粯璁ょ殑鑼冨洿銆 | CActiveRecord |
rules() | Returns the validation rules for attributes. | CModel |
save() | 淇濆瓨褰撳墠鐨勮褰曘 | CActiveRecord |
saveAttributes() | 淇濆瓨鎵閫夌殑灞炴у垪琛ㄣ | CActiveRecord |
saveCounters() | 淇濆瓨涓涓垨澶氬綋鍓岮R瀵硅薄鍒拌鏁板櫒銆 | CActiveRecord |
scopes() | 杩斿洖鐢虫槑鐨勫懡鍚嶈寖鍥淬 | CActiveRecord |
setAttribute() | 璁剧疆鍛藉悕鐨勫睘鎬у笺 | CActiveRecord |
setAttributes() | Sets the attribute values in a massive way. | CModel |
setDbCriteria() | 涓哄綋鍓嶆ā鍨嬭缃煡璇㈡潯浠. | CActiveRecord |
setIsNewRecord() | 璁剧疆璁板綍鏄惁鏄柊鐨勩 | CActiveRecord |
setOldPrimaryKey() | 璁剧疆鏃х殑涓婚敭鍊笺 | CActiveRecord |
setPrimaryKey() | 璁剧疆涓婚敭鍊笺 | CActiveRecord |
setScenario() | Sets the scenario for the model. | CModel |
setTableAlias() | 璁剧疆琛ㄥ湪鏌ヨ涓娇鐢ㄧ殑鍒悕銆 | CActiveRecord |
tableName() | 杩斿洖鍏宠仈鐨勬暟鎹簱琛ㄧ殑鍚嶇О銆 | CActiveRecord |
together() | 璁剧疆CDbCriteria::together灞炴т负true銆 | CActiveRecord |
unsetAttributes() | Sets the attributes to be null. | CModel |
update() | 鏇存柊姝ゆ椿鍔ㄨ褰曟墍琛ㄧず鐨勮銆 | CActiveRecord |
updateAll() | 鏇存柊浣跨敤鎸囧畾鏉′欢鐨勮褰曘 | CActiveRecord |
updateByPk() | 鏇存柊鎸囧畾涓婚敭璁板綍鐨勮銆 | CActiveRecord |
updateCounters() | 鏇存柊鐨勪竴涓垨澶氫釜璁℃暟鍣ㄣ | CActiveRecord |
validate() | Performs the validation. | CModel |
with() | 鎸囧畾鐩稿叧瀵硅薄搴斾竴璧疯浇鍏ャ | CActiveRecord |
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
afterConstruct() | This method is invoked after a model instance is created by new operator. | CModel |
afterDelete() | 鍒犻櫎璁板綍涔嬪悗璋冪敤姝ゆ柟娉曘 | CActiveRecord |
afterFind() | 姣忎釜璁板綍鐢 find 鏂规硶瀹炰緥鍖栧悗锛屽皢璋冪敤姝ゆ柟娉曘 | CActiveRecord |
afterSave() | 璁板綍琚垚鍔熶繚瀛樺悗锛岃皟鐢ㄦ鏂规硶銆 | CActiveRecord |
afterValidate() | This method is invoked after validation ends. | CModel |
beforeDelete() | 鍦ㄥ垹闄よ褰曚箣鍓嶈皟鐢ㄦ鏂规硶銆 | CActiveRecord |
beforeFind() | AR finder鎵ц鏌ユ壘璋冪敤涔嬪墠锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
beforeSave() | 淇濆瓨璁板綍涔嬪墠锛岃皟鐢ㄨ繖涓柟娉曪紙楠岃瘉鍚庯紝濡傛灉鏈夌殑璇濓級銆 | CActiveRecord |
beforeValidate() | This method is invoked before validation starts. | CModel |
instantiate() | 鍒涘缓娲诲姩璁板綍瀹炰緥銆 | CActiveRecord |
query() | 鎵ц瀹為檯鐨勬暟鎹簱鏌ヨ鍜屽~鍏匒R瀵硅薄鐨勬煡璇㈢粨鏋溿 | CActiveRecord |
浜嬩欢
浜嬩欢 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
onBeforeSave | 淇濆瓨璁板綍涔嬪墠锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
onAfterSave | 淇濆瓨璁板綍涔嬪悗锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
onBeforeDelete | 鍦ㄥ垹闄よ褰曚箣鍓嶏紝灏嗗紩鍙戞浜嬩欢銆 | CActiveRecord |
onAfterDelete | 鍦ㄥ垹闄よ褰曚箣鍚庯紝灏嗗紩鍙戞浜嬩欢銆 | CActiveRecord |
onBeforeFind | AR finder鎵ц鏌ユ壘璋冪敤涔嬪墠锛屽皢寮曞彂姝や簨浠躲 | CActiveRecord |
onAfterFind | 寮曞彂姝や簨浠惰褰曞悗鎵惧埌鏂规硶鐨勫疄渚嬨 | CActiveRecord |
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 |
灞炴ц缁
public void setAttributes(array $values, boolean $safeOnly=true)
杩斿洖鎵鏈夊垪鐨勫睘鎬у笺 娉ㄦ剰锛屼笉杩斿洖鐩稿叧鐨勫璞°
杩斿洖姝R鐨勫懡浠ょ敓鎴愬櫒銆
鎵鏈夋椿鍔ㄨ褰曠被鐨勯粯璁ゆ暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝杩欐槸'db'搴旂敤绋嬪簭缁勪欢锛
鍙傝
杩斿洖娲诲姩璁板綍鎵浣跨敤鐨勬暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝"db"搴旂敤绋嬪簭缁勪欢鐢ㄤ綔鏁版嵁搴撹繛鎺ャ 濡傛灉鎮ㄨ浣跨敤涓涓笉鍚岀殑鏁版嵁搴撹繛鎺ワ紝鎮ㄥ彲浠ラ噸鍐欐鏂规硶銆
public void setDbCriteria(CDbCriteria $criteria)
杩斿洖鍜岃繖涓ā鍨嬬浉鍏崇殑鏌ヨ鏉′欢銆
杩斿洖褰撳墠璁板綍鏄惁鏄柊鐨勩
杩斿洖杩欎釜AR鐨勫厓鏁版嵁銆
杩斿洖鏃х殑涓婚敭鍊笺 杩欐寚鐨勬槸涓婚敭鍊煎凡濉厖鍒拌褰曞悗锛 鎵ц鏌ユ壘鏂规硶 (渚嬪锛 find()锛宖indAll())锛 鍗充娇涓诲叧閿睘鎬ч兘浼氳鎵嬪姩鍒嗛厤涓涓笉鍚岀殑鍊硷紝鍊间繚鎸佷笉鍙樸
杩斿洖涓婚敭鍊笺
public void setTableAlias(string $alias)
杩斿洖鐢 find 鏂规硶浣跨敤琛ㄧ殑鍒悕銆 鍦ㄥ叧绯绘煡璇紝鏍规嵁瀵瑰簲鍏崇郴鐨勫0鏄庯紝 杩斿洖鐨勮〃鐨勫埆鍚嶅彲鑳戒細鏈夋墍涓嶅悓銆 姝ゅ锛岀敱setTableAlias鍙互鐢卞簲鐢ㄧ殑鑼冨洿瑕嗙洊銆
杩斿洖琛ㄧ殑鍏冩暟鎹紝杩欏睘浜嶢R绫汇
鏂规硶璇︾粏
public mixed __call(string $name, array $parameters)
| ||
$name | string | 鏂规硶鍚 |
$parameters | array | 鏂规硶鍙傛暟 |
{return} | mixed | 姝ゆ柟娉曡繑鍥炲 |
public function __call($name,$parameters)
{
if(isset($this->getMetaData()->relations[$name]))
{
if(empty($parameters))
return $this->getRelated($name,false);
else
return $this->getRelated($name,false,$parameters[0]);
}
$scopes=$this->scopes();
if(isset($scopes[$name]))
{
$this->getDbCriteria()->mergeWith($scopes[$name]);
return $this;
}
return parent::__call($name,$parameters);
}
鍥炶皟鐨勫懡鍚嶆柟娉曘 璇蜂笉瑕佽皟鐢ㄥ畠锛屽畠涓嶆槸绫荤殑鏂规硶銆 杩欐槸鎴戜滑閲嶅啓涓涓狿HP榄旀湳鏂规硶瀹炵幇浜嗚繖涓懡鍚嶈寖鍥寸殑鍔熻兘銆
public void __construct(string $scenario='insert')
| ||
$scenario | string | 鍦烘櫙鍚嶃傚弬瑙CModel::scenario鍏充簬杩欎釜鍙傛暟鐨勬洿澶氫俊鎭 |
public function __construct($scenario='insert')
{
if($scenario===null) // internally used by populateRecord() and model()
return;
$this->setScenario($scenario);
$this->setIsNewRecord(true);
$this->_attributes=$this->getMetaData()->attributeDefaults;
$this->init();
$this->attachBehaviors($this->behaviors());
$this->afterConstruct();
}
鏋勯犲嚱鏁般
public mixed __get(string $name)
| ||
$name | string | 灞炴у悕 |
{return} | mixed | 灞炴у |
public function __get($name)
{
if(isset($this->_attributes[$name]))
return $this->_attributes[$name];
else if(isset($this->getMetaData()->columns[$name]))
return null;
else if(isset($this->_related[$name]))
return $this->_related[$name];
else if(isset($this->getMetaData()->relations[$name]))
return $this->getRelated($name);
else
return parent::__get($name);
}
PHP getter 榄旀湳鏂规硶銆 閲嶅啓姝ゆ柟娉曪紝浠ヤ究AR灞炴ц兘鍍忓睘鎬ч偅鏍疯闂
鍙傝
public boolean __isset(string $name)
| ||
$name | string | 灞炴у悕鎴栦簨浠跺悕 |
{return} | boolean | 鏄惁灞炴у间负null |
public function __isset($name)
{
if(isset($this->_attributes[$name]))
return true;
else if(isset($this->getMetaData()->columns[$name]))
return false;
else if(isset($this->_related[$name]))
return true;
else if(isset($this->getMetaData()->relations[$name]))
return $this->getRelated($name)!==null;
else
return parent::__isset($name);
}
妫娴嬩竴涓睘鎬у兼槸鍚︿负null銆 閫氳繃妫娴嬶紝濡傛灉鍛戒护灞炴т负绌烘垨涓嶅瓨鍦紝 姝ゆ柟娉曢噸鍐欑埗绫汇
public void __set(string $name, mixed $value)
| ||
$name | string | 灞炴у悕 |
$value | mixed | 灞炴у |
public function __set($name,$value)
{
if($this->setAttribute($name,$value)===false)
{
if(isset($this->getMetaData()->relations[$name]))
$this->_related[$name]=$value;
else
parent::__set($name,$value);
}
}
PHP setter 榄旀湳鏂规硶銆 閲嶅啓姝ゆ柟娉曪紝浠ヤ究AR灞炴ц兘鍍忓睘鎬ч偅鏍疯闂
public array __sleep()
| ||
{return} | array |
public function __sleep()
{
$this->_md=null;
return array_keys((array)$this);
}
PHP sleep 榄旀湳鏂规硶銆 姝ゆ柟娉曠‘淇濇ā鍨嬪厓鏁版嵁寮曠敤琚缃负 null銆
public void __unset(string $name)
| ||
$name | string | 灞炴у悕鎴栦簨浠跺悕 |
public function __unset($name)
{
if(isset($this->getMetaData()->columns[$name]))
unset($this->_attributes[$name]);
else if(isset($this->getMetaData()->relations[$name]))
unset($this->_related[$name]);
else
parent::__unset($name);
}
妫鏌ュ睘鎬у兼槸鍚︿负绌恒 閫氳繃妫娴嬶紝濡傛灉鍛戒护灞炴т负绌烘垨涓嶅瓨鍦紝 姝ゆ柟娉曢噸鍐欑埗绫汇
public void addRelatedRecord(string $name, mixed $record, mixed $index)
| ||
$name | string | 灞炴у悕绉 |
$record | mixed | 鐩稿叧鐨勮褰 |
$index | mixed | 鐩稿叧瀵硅薄闆嗗悎涓殑绱㈠紩鍊笺 濡傛灉true锛岃繖鎰忓懗鐫浣跨敤浠庨浂寮濮嬬殑鏁存暟绱㈠紩銆 濡傛灉false锛屽畠鎰忓懗鐫HAS_ONE鎴朆ELONGS_TO瀵硅薄鍜岀储寮曚笉闇瑕併 |
public function addRelatedRecord($name,$record,$index)
{
if($index!==false)
{
if(!isset($this->_related[$name]))
$this->_related[$name]=array();
if($record instanceof CActiveRecord)
{
if($index===true)
$this->_related[$name][]=$record;
else
$this->_related[$name][$index]=$record;
}
}
else if(!isset($this->_related[$name]))
$this->_related[$name]=$record;
}
涓嶈皟鐢ㄦ鏂规硶銆傛鏂规硶鐢ㄤ簬鍐呴儴CActiveFinder鏉ュ~鍏呯浉鍏崇殑瀵硅薄銆 姝ゆ柟娉曟坊鍔犲埌姝よ褰曠殑鐩稿叧鐨勫璞°
protected void afterDelete()
|
protected function afterDelete()
{
if($this->hasEventHandler('onAfterDelete'))
$this->onAfterDelete(new CEvent($this));
}
鍒犻櫎璁板綍涔嬪悗璋冪敤姝ゆ柟娉曘 杩欎釜榛樿鎵ц寮曞彂onAfterDelete浜嬩欢銆 鎮ㄥ彲浠ラ噸鍐欐鏂规硶锛屽湪璁板綍琚垹闄ゅ悗鍋氬鐞嗐 璇风‘淇濇偍璋冪敤鐖跺疄渚嬶紝浠ヤ究姝g‘鍦板紩鍙戣浜嬩欢銆
protected void afterFind()
|
protected function afterFind()
{
if($this->hasEventHandler('onAfterFind'))
$this->onAfterFind(new CEvent($this));
}
姣忎釜璁板綍鐢 find 鏂规硶瀹炰緥鍖栧悗锛屽皢璋冪敤姝ゆ柟娉曘 杩欎釜榛樿鎵ц寮曞彂onAfterFind浜嬩欢銆 浣犲彲浠ラ噸鍐欒繖涓柟娉曪紝 鍋氬鐞嗗悗姣忎釜鏂版煡鎵剧殑璁板綍瀹炰緥銆 璇风‘淇濇偍璋冪敤鐖跺疄渚嬶紝浠ヤ究姝g‘鍦板紩鍙戣浜嬩欢銆
public void afterFindInternal()
|
public function afterFindInternal()
{
$this->afterFind();
}
璋冪敤afterFind銆 杩欎釜鏂规硶鍐呴儴浣跨敤銆
protected void afterSave()
|
protected function afterSave()
{
if($this->hasEventHandler('onAfterSave'))
$this->onAfterSave(new CEvent($this));
}
璁板綍琚垚鍔熶繚瀛樺悗锛岃皟鐢ㄦ鏂规硶銆 杩欎釜榛樿鎵ц寮曞彂onAfterSave浜嬩欢銆 鎮ㄥ彲閲嶅啓姝ゆ柟娉曪紝浠ヨ褰曚繚瀛樺悗鍋氬悗澶勭悊銆 璇风‘淇濇偍璋冪敤鐖跺疄渚嬶紝浠ヤ究姝g‘鍦板紩鍙戣浜嬩欢銆
public void applyScopes(CDbCriteria &$criteria)
| ||
$criteria | CDbCriteria | 鏌ヨ鏉′欢銆傞氳繃鍚堝苟dbCriteria鍙慨鏀规鍙傛暟銆 |
public function applyScopes(&$criteria)
{
if(!empty($criteria->scopes))
{
$scs=$this->scopes();
$c=$this->getDbCriteria();
foreach((array)$criteria->scopes as $k=>$v)
{
if(is_integer($k))
{
if(is_string($v))
{
if(isset($scs[$v]))
{
$c->mergeWith($scs[$v],true);
continue;
}
$scope=$v;
$params=array();
}
else if(is_array($v))
{
$scope=key($v);
$params=current($v);
}
}
else if(is_string($k))
{
$scope=$k;
$params=$v;
}
call_user_func_array(array($this,$scope),(array)$params);
}
}
if(isset($c) || ($c=$this->getDbCriteria(false))!==null)
{
$c->mergeWith($criteria);
$criteria=$c;
$this->_c=null;
}
}
閫傜敤浜庣粰瀹氭潯浠剁殑鏌ヨ鑼冨洿銆 杩欑鏂规硶鍚堝苟dbCriteria涓庣粰瀹氱殑鏉′欢鍙傛暟銆 瀹冨皢閲嶇疆dbCriteria涓簄ull銆
public array attributeNames()
| ||
{return} | array | 灞炴у悕绉板垪琛 |
public function attributeNames()
{
return array_keys($this->getMetaData()->columns);
}
杩斿洖妯″瀷鐨勬墍鏈夊睘鎬у悕绉扮殑鍒楄〃銆 杩欏皢杩斿洖AR绫讳笌姝ょ浉鍏宠仈鐨勬墍鏈夎〃鐨勫垪鍚嶃
protected boolean beforeDelete()
| ||
{return} | boolean | 鏄惁搴斿垹闄よ褰曘傞粯璁ゅ间负true銆 |
protected function beforeDelete()
{
if($this->hasEventHandler('onBeforeDelete'))
{
$event=new CModelEvent($this);
$this->onBeforeDelete($event);
return $event->isValid;
}
else
return true;
}
鍦ㄥ垹闄よ褰曚箣鍓嶈皟鐢ㄦ鏂规硶銆 杩欎釜榛樿鎵ц寮曞彂onBeforeDelete浜嬩欢銆 鎮ㄥ彲浠ラ噸鍐欐鏂规硶锛屼互鍋氫负璁板綍鍒犻櫎浠讳綍鍑嗗宸ヤ綔銆 璇风‘淇濇偍璋冪敤鐖跺疄渚嬶紝浠ヤ究姝g‘鍦板紩鍙戣浜嬩欢銆
protected void beforeFind()
|
protected function beforeFind()
{
if($this->hasEventHandler('onBeforeFind'))
{
$event=new CModelEvent($this);
// for backward compatibility
$event->criteria=func_num_args()>0 ? func_get_arg(0) : null;
$this->onBeforeFind($event);
}
}
AR finder鎵ц鏌ユ壘璋冪敤涔嬪墠锛屽皢寮曞彂姝や簨浠躲
鏌ユ壘璋冪敤鍖呮嫭find锛findAll锛findByPk锛
findAllByPk锛findByAttributes鍜findAllByAttributes銆
杩欎釜榛樿鎵ц寮曞彂onBeforeFind浜嬩欢銆
濡傛灉閲嶅啓姝ゆ柟娉曪紝
璇风‘淇濇偍璋冪敤鐖跺疄渚嬶紝浠ヤ究姝g‘鍦板紩鍙戣浜嬩欢銆
浠1.1.5鐗堟湰寮濮嬶紝
杩欑鏂规硶鍙О涓轰竴涓殣钘忕殑鍙傛暟CDbCriteria琛ㄧず褰撳墠鏌ヨ鏉′欢浼犻掑埌涓涓狝R鏂规硶銆
public void beforeFindInternal()
|
public function beforeFindInternal()
{
$this->beforeFind();
}
璋冪敤beforeFind銆 杩欎釜鏂规硶鍐呴儴浣跨敤銆
protected boolean beforeSave()
| ||
{return} | boolean | 鏄惁搴旇鎵ц鍙湪锛岄粯璁や负true銆 |
protected function beforeSave()
{
if($this->hasEventHandler('onBeforeSave'))
{
$event=new CModelEvent($this);
$this->onBeforeSave($event);
return $event->isValid;
}
else
return true;
}
淇濆瓨璁板綍涔嬪墠锛岃皟鐢ㄨ繖涓柟娉曪紙楠岃瘉鍚庯紝濡傛灉鏈夌殑璇濓級銆 榛樿瀹炵幇寮曞彂onBeforeSave浜嬩欢銆 鎮ㄥ彲浠ラ噸鍐欐鏂规硶鍋氫换浣曡褰曚繚瀛樼殑鍑嗗宸ヤ綔銆 浣跨敤isNewRecord 浠ョ‘璁ゆ槸鍚︽槸鎻掑叆鎴栦繚瀛樿褰曘 璇风‘淇濇偍璋冪敤鐖跺疄渚嬶紝浠ヤ究姝g‘鍦板紩鍙戣浜嬩欢銆
public CActiveRecord cache(integer $duration, CCacheDependency $dependency=NULL, integer $queryCount=1)
| ||
$duration | integer | 鏌ヨ缁撴灉鍙兘淇濇寔鍦ㄧ紦瀛樹腑鏈夋晥鐨勭鏁般 濡傛灉杩欐槸0锛岀紦瀛樺皢琚鐢ㄣ |
$dependency | CCacheDependency | 淇濆瓨鏌ヨ缁撴灉鍒扮紦瀛樻椂浣跨敤鐨勪緷璧栧叧绯汇 |
$queryCount | integer | 鍦ㄨ皟鐢ㄦ鏂规硶鍚庯紝闇瑕佺紦瀛樼殑SQL鏌ヨ鐨勬暟鐩 榛樿鍊间负 1锛岃繖鎰忓懗鐫涓嬩竴涓猄QL鏌ヨ灏嗚缂撳瓨銆 |
{return} | CActiveRecord | AR瀹炰緥鏈韩銆 |
public function cache($duration, $dependency=null, $queryCount=1)
{
$this->getDbConnection()->cache($duration, $dependency, $queryCount);
return $this;
}
璁剧疆鍏充簬鏌ヨ缂撳瓨鐨勫弬鏁. 杩欐槸CDbConnection::cache()鐨勪竴绉嶅揩鎹锋柟娉曘 瀹冩敼鍙樻煡璇㈢紦瀛樼殑dbConnection瀹炰緥鍙傛暟銆
public string count(mixed $condition='', array $params=array (
))
| ||
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | string | 婊¤冻鎸囧畾鐨勬煡璇㈡潯浠剁殑琛屾暟銆傛敞锛氱被鍨嬫槸瀛楃涓蹭繚鎸佹渶澶х簿搴︺ |
public function count($condition='',$params=array())
{
Yii::trace(get_class($this).'.count()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createCriteria($condition,$params);
$this->applyScopes($criteria);
if(empty($criteria->with))
return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();
else
{
$finder=new CActiveFinder($this,$criteria->with);
return $finder->count($criteria);
}
}
鏌ユ壘婊¤冻鎸囧畾鏌ヨ鏉′欢鐨勮鏁般 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public string countByAttributes(array $attributes, mixed $condition='', array $params=array (
))
| ||
$attributes | array | 鍒楄〃涓殑灞炴у硷紙灞炴у悕绱㈠紩锛夋椿鍔ㄨ褰曠浉鍖归厤銆 灞炴у煎彲浠ユ槸涓涓暟缁勶紝鐢ㄤ簬鐢熸垚鐨勬潯浠躲 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | string | 婊¤冻鎸囧畾鐨勬煡璇㈡潯浠剁殑琛屾暟銆傛敞锛氱被鍨嬫槸瀛楃涓蹭繚鎸佹渶澶х簿搴︺ |
public function countByAttributes($attributes,$condition='',$params=array())
{
Yii::trace(get_class($this).'.countByAttributes()','system.db.ar.CActiveRecord');
$prefix=$this->getTableAlias(true).'.';
$builder=$this->getCommandBuilder();
$criteria=$builder->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
$this->applyScopes($criteria);
if(empty($criteria->with))
return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();
else
{
$finder=new CActiveFinder($this,$criteria->with);
return $finder->count($criteria);
}
}
鏌ユ壘鍏锋湁鎸囧畾鐨勫睘鎬у肩殑琛屾暟銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public string countBySql(string $sql, array $params=array (
))
| ||
$sql | string | SQL璇彞 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | string | 婊¤冻鎸囧畾鐨勬煡璇㈡潯浠剁殑琛屾暟銆傛敞锛氱被鍨嬫槸瀛楃涓蹭繚鎸佹渶澶х簿搴︺ |
public function countBySql($sql,$params=array())
{
Yii::trace(get_class($this).'.countBySql()','system.db.ar.CActiveRecord');
return $this->getCommandBuilder()->createSqlCommand($sql,$params)->queryScalar();
}
鏌ユ壘鎸囧畾SQL璇彞褰卞搷鐨勮鏁般 杩欑浉褰撲簬璋冪敤CDbCommand::queryScalar锛 浠ユ寚瀹氱殑SQL澹版槑鍜屽弬鏁般
public array defaultScope()
| ||
{return} | array | 鏌ヨ鏉′欢銆 杩欏皢浣滀负CDbCriteria鍙傛暟鐨勬瀯閫犳柟娉曘 |
杩斿洖榛樿鐨勫懡鍚嶈寖鍥达紝搴旈殣鍚繖绉嶆ā寮忛傜敤浜庢墍鏈夋煡璇€ 娉ㄦ剰锛岄粯璁ょ殑鑼冨洿鍙傜敤浜嶴ELECT鏌ヨ銆傚拷瑙嗗INSERT锛孶PDATE鍜孌ELETE鏌ヨ銆 榛樿瀹炵幇鍙繑鍥炰竴涓┖鏁扮粍銆 濡傛灉妯″瀷闇瑕佷竴浜涢粯璁ょ殑鏍囧噯锛堝鍙湁娲诲姩璁板綍锛屽綋杩斿洖锛夋煡璇紝浣犲彲浠ラ噸鍐欐鏂规硶銆
public boolean delete()
| ||
{return} | boolean | 鏄惁鍒犻櫎鎴愬姛銆 |
public function delete()
{
if(!$this->getIsNewRecord())
{
Yii::trace(get_class($this).'.delete()','system.db.ar.CActiveRecord');
if($this->beforeDelete())
{
$result=$this->deleteByPk($this->getPrimaryKey())>0;
$this->afterDelete();
return $result;
}
else
return false;
}
else
throw new CDbException(Yii::t('yii','The active record cannot be deleted because it is new.'));
}
鍒犻櫎瀵瑰簲浜庢娲诲姩璁板綍鐨勮銆
public integer deleteAll(mixed $condition='', array $params=array (
))
| ||
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | integer | 鍒犻櫎鐨勮鏁 |
public function deleteAll($condition='',$params=array())
{
Yii::trace(get_class($this).'.deleteAll()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createCriteria($condition,$params);
$command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
return $command->execute();
}
鍒犻櫎鎸囧畾鏉′欢鐨勮銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public integer deleteAllByAttributes(array $attributes, mixed $condition='', array $params=array (
))
| ||
$attributes | array | 鍒楄〃涓殑灞炴у硷紙灞炴у悕绱㈠紩锛夋椿鍔ㄨ褰曠浉鍖归厤銆 灞炴у煎彲浠ユ槸涓涓暟缁勶紝鐢ㄤ簬鐢熸垚鐨勬潯浠躲 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | integer | 鎵ц褰卞搷鐨勮鏁般 |
public function deleteAllByAttributes($attributes,$condition='',$params=array())
{
Yii::trace(get_class($this).'.deleteAllByAttributes()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$table=$this->getTableSchema();
$criteria=$builder->createColumnCriteria($table,$attributes,$condition,$params);
$command=$builder->createDeleteCommand($table,$criteria);
return $command->execute();
}
鍒犻櫎鎸囧畾鐨勫睘鎬у肩浉鍖归厤鐨勮銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public integer deleteByPk(mixed $pk, mixed $condition='', array $params=array (
))
| ||
$pk | mixed | 涓婚敭鍊笺傚澶氫釜涓婚敭浣跨敤鏁扮粍銆傚鍚堥敭锛屽浜庢瘡涓敭鐨勫煎繀椤绘槸涓涓暟缁勶紙鍒楀悕=>鍒楀硷級銆 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | integer | 鍒犻櫎鐨勮鏁 |
public function deleteByPk($pk,$condition='',$params=array())
{
Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createPkCriteria($this->getTableSchema(),$pk,$condition,$params);
$command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
return $command->execute();
}
鍒犻櫎鎸囧畾涓婚敭鐨勮銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public boolean equals(CActiveRecord $record)
| ||
$record | CActiveRecord | 瑕佹瘮杈冪殑璁板綍 |
{return} | boolean | 涓や釜娲诲姩鐨勮褰曟槸鍚︽槸鎸囨暟鎹簱琛ㄤ腑鐨勫悓涓琛屻 |
public function equals($record)
{
return $this->tableName()===$record->tableName() && $this->getPrimaryKey()===$record->getPrimaryKey();
}
褰撳墠娲诲姩璁板綍涓庡彟涓涓瘮杈冦 閫氳繃姣旇緝琛ㄥ悕鍜屼袱涓椿鍔ㄨ褰曠殑涓婚敭鍊兼潵浣滄瘮杈冦
public boolean exists(mixed $condition='', array $params=array (
))
| ||
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | boolean | 鏄惁婊¤冻鎸囧畾鏉′欢鐨勮銆 |
public function exists($condition='',$params=array())
{
Yii::trace(get_class($this).'.exists()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createCriteria($condition,$params);
$table=$this->getTableSchema();
$criteria->select='1';
$criteria->limit=1;
$this->applyScopes($criteria);
if(empty($criteria->with))
return $builder->createFindCommand($table,$criteria)->queryRow()!==false;
else
{
$criteria->select='*';
$finder=new CActiveFinder($this,$criteria->with);
return $finder->count($criteria)>0;
}
}
妫鏌ユ槸鍚︽弧瓒虫寚瀹氭潯浠剁殑琛屻 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public CActiveRecord find(mixed $condition='', array $params=array (
))
| ||
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 濡傛灉涓涓瓧绗︿覆锛屽畠琚綔涓烘煡璇㈡潯浠 锛圵HERE 瀛愬彞锛 锛 濡傛灉涓涓暟缁勶紝瀹冭瑙嗕负鏋勫缓CDbCriteria瀵硅薄鍒濆鍊; 鍚﹀垯锛屽畠搴旇鏄CDbCriteria瀵硅薄鐨勪竴涓疄渚嬨 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁般 杩欎粎浠呮槸绗竴涓弬鏁版槸涓涓瓧绗︿覆锛堟煡璇㈡潯浠讹級鏃朵娇鐢ㄣ 鍦ㄥ叾浠栨儏鍐典笅锛岃浣跨敤CDbCriteria::params璁剧疆鍙傛暟銆 |
{return} | CActiveRecord | 鎵惧埌鐨勮褰曘傚鏋滄病鎵惧埌浠讳綍璁板綍锛屽垯涓 null銆 |
public function find($condition='',$params=array())
{
Yii::trace(get_class($this).'.find()','system.db.ar.CActiveRecord');
$criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
return $this->query($criteria);
}
鏌ユ壘鎸囧畾鏉′欢鐨勫崟涓椿鍔ㄨ褰曘
public array findAll(mixed $condition='', array $params=array (
))
| ||
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁般 |
{return} | array | 婊¤冻鎸囧畾鏉′欢鐨勬椿鍔ㄨ褰曞垪琛紝濡傛灉娌℃湁鎵惧埌灏嗚繑鍥炰竴涓┖鐨勬暟缁勩 |
public function findAll($condition='',$params=array())
{
Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord');
$criteria=$this->getCommandBuilder()->createCriteria($condition,$params);
return $this->query($criteria,true);
}
鏌ユ壘婊¤冻鎸囧畾鏉′欢鐨勬墍鏈夋椿鍔ㄨ褰曘 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public array findAllByAttributes(array $attributes, mixed $condition='', array $params=array (
))
| ||
$attributes | array | 鍒楄〃涓殑灞炴у硷紙灞炴у悕绱㈠紩锛夋椿鍔ㄨ褰曠浉鍖归厤銆 灞炴у煎彲浠ユ槸涓涓暟缁勫皢琚敤鏉ョ敓鎴愪竴涓潯浠躲 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁般 |
{return} | array | 鎵惧埌鐨勮褰曘傚鏋滄病鏈夋壘鍒拌繑鍥炰竴涓┖鏁扮粍銆 |
public function findAllByAttributes($attributes,$condition='',$params=array())
{
Yii::trace(get_class($this).'.findAllByAttributes()','system.db.ar.CActiveRecord');
$prefix=$this->getTableAlias(true).'.';
$criteria=$this->getCommandBuilder()->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
return $this->query($criteria,true);
}
鏌ユ壘鍏锋湁鎸囧畾灞炴у肩殑鎵鏈夋椿鍔ㄨ褰曘 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public array findAllByPk(mixed $pk, mixed $condition='', array $params=array (
))
| ||
$pk | mixed | 涓婚敭鍊笺傚澶氫釜涓婚敭浣跨敤鏁扮粍銆傚鍚堥敭锛屽浜庢瘡涓敭鐨勫煎繀椤绘槸涓涓暟缁勶紙鍒楀悕=>鍒楀硷級銆 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁般 |
{return} | array | 鎵惧埌鐨勮褰曘傚鏋滄病鏈夋壘鍒拌繑鍥炰竴涓┖鏁扮粍銆 |
public function findAllByPk($pk,$condition='',$params=array())
{
Yii::trace(get_class($this).'.findAllByPk()','system.db.ar.CActiveRecord');
$prefix=$this->getTableAlias(true).'.';
$criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
return $this->query($criteria,true);
}
鏌ユ壘鎸囧畾涓婚敭鐨勬墍鏈夋椿鍔ㄨ褰曘 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public array findAllBySql(string $sql, array $params=array (
))
| ||
$sql | string | SQL璇彞 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | array | 鎵惧埌鐨勮褰曘傚鏋滄病鎵惧埌浠讳綍璁板綍锛屽垯涓 null銆 |
public function findAllBySql($sql,$params=array())
{
Yii::trace(get_class($this).'.findAllBySql()','system.db.ar.CActiveRecord');
$this->beforeFind();
if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
{
$this->_c=null;
$finder=new CActiveFinder($this,$criteria->with);
return $finder->findAllBySql($sql,$params);
}
else
{
$command=$this->getCommandBuilder()->createSqlCommand($sql,$params);
return $this->populateRecords($command->queryAll());
}
}
鏌ユ壘鎸囧畾SQL璇彞鐨勬墍鏈夋椿鍔ㄨ褰曘
public CActiveRecord findByAttributes(array $attributes, mixed $condition='', array $params=array (
))
| ||
$attributes | array | 鍒楄〃涓殑灞炴у硷紙灞炴у悕绱㈠紩锛夋椿鍔ㄨ褰曠浉鍖归厤銆 灞炴у煎彲浠ユ槸涓涓暟缁勫皢琚敤鏉ョ敓鎴愪竴涓潯浠躲 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁般 |
{return} | CActiveRecord | 鎵惧埌鐨勮褰曘傚鏋滄病鎵惧埌浠讳綍璁板綍锛屽垯涓 null銆 |
public function findByAttributes($attributes,$condition='',$params=array())
{
Yii::trace(get_class($this).'.findByAttributes()','system.db.ar.CActiveRecord');
$prefix=$this->getTableAlias(true).'.';
$criteria=$this->getCommandBuilder()->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);
return $this->query($criteria);
}
鏌ユ壘鍏锋湁鎸囧畾灞炴у肩殑鍗曚釜娲诲姩璁板綍銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public CActiveRecord findByPk(mixed $pk, mixed $condition='', array $params=array (
))
| ||
$pk | mixed | 涓婚敭鍊笺傚澶氫釜涓婚敭浣跨敤鏁扮粍銆傚鍚堥敭锛屽浜庢瘡涓敭鐨勫煎繀椤绘槸涓涓暟缁勶紙鍒楀悕=>鍒楀硷級銆 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁般 |
{return} | CActiveRecord | 鎵惧埌鐨勮褰曘傚鏋滄病鎵惧埌浠讳綍璁板綍锛屽垯涓 null銆 |
public function findByPk($pk,$condition='',$params=array())
{
Yii::trace(get_class($this).'.findByPk()','system.db.ar.CActiveRecord');
$prefix=$this->getTableAlias(true).'.';
$criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
return $this->query($criteria);
}
鏌ユ壘鎸囧畾涓婚敭鐨勫崟涓娲诲姩璁板綍銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
public CActiveRecord findBySql(string $sql, array $params=array (
))
| ||
$sql | string | SQL璇彞 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | CActiveRecord | 鎵惧埌鐨勮褰曘傚鏋滄病鎵惧埌浠讳綍璁板綍锛屽垯涓 null銆 |
public function findBySql($sql,$params=array())
{
Yii::trace(get_class($this).'.findBySql()','system.db.ar.CActiveRecord');
$this->beforeFind();
if(($criteria=$this->getDbCriteria(false))!==null && !empty($criteria->with))
{
$this->_c=null;
$finder=new CActiveFinder($this,$criteria->with);
return $finder->findBySql($sql,$params);
}
else
{
$command=$this->getCommandBuilder()->createSqlCommand($sql,$params);
return $this->populateRecord($command->queryRow());
}
}
鏌ユ壘鎸囧畾SQL璇彞鐨勫崟涓椿鍔ㄨ褰曘
public CActiveRelation getActiveRelation(string $name)
| ||
$name | string | 鍏崇郴鍚嶇О |
{return} | CActiveRelation | 涓烘AR绫诲0鏄庡懡鍚嶇殑鍏崇郴銆傚鏋滃叧绯讳笉瀛樺湪鍒欎负null銆 |
public function getActiveRelation($name)
{
return isset($this->getMetaData()->relations[$name]) ? $this->getMetaData()->relations[$name] : null;
}
杩斿洖姝R绫诲0鏄庣殑鍛藉悕鍏崇郴銆
public mixed getAttribute(string $name)
| ||
$name | string | 灞炴у悕绉 |
{return} | mixed | 灞炴у笺傚鏋滃睘鎬ф湭璁剧疆鈥嬧嬫垨涓嶅瓨鍦ㄥ垯涓簄ull銆 |
public function getAttribute($name)
{
if(property_exists($this,$name))
return $this->$name;
else if(isset($this->_attributes[$name]))
return $this->_attributes[$name];
}
杩斿洖鍛藉悕鐨勫睘鎬у笺 濡傛灉杩欐槸涓涓柊鐨勮褰曪紝璇ュ睘鎬ф湭璁剧疆涔嬪墠锛 灏嗚繑鍥炵殑鍒楃殑榛樿鍊笺 濡傛灉璇ヨ褰曟槸鏌ヨ鐨勭粨鏋滃苟涓嶅姞杞借灞炴ф椂锛 灏嗚繑鍥瀗ull銆 浣犱篃鍙互浣跨敤$this->AttributeName鏉ヨ幏寰楀睘鎬у笺
鍙傝
public string getAttributeLabel(string $attribute)
| ||
$attribute | string | 灞炴у悕绉 |
{return} | string | 灞炴ф爣绛 |
public function getAttributeLabel($attribute)
{
$labels=$this->attributeLabels();
if(isset($labels[$attribute]))
return $labels[$attribute];
else if(strpos($attribute,'.')!==false)
{
$segs=explode('.',$attribute);
$name=array_pop($segs);
$model=$this;
foreach($segs as $seg)
{
$relations=$model->getMetaData()->relations;
if(isset($relations[$seg]))
$model=CActiveRecord::model($relations[$seg]->className);
else
break;
}
return $model->getAttributeLabel($name);
}
else
return $this->generateAttributeLabel($attribute);
}
杩斿洖鎸囧畾灞炴х殑鏂囨湰鏍囩銆 杩欎釜鏂规硶閲嶅啓鐖剁被锛 鏀寔杩斿洖鍏崇郴瀵硅薄涓畾涔夌殑鏍囩銆 鐗瑰埆娉ㄦ剰锛屽鏋滃睘鎬у悕绉版槸"post.author.name"鐨勫舰寮忥紝 閭d箞姝ゆ柟娉曚細浠"author"鍏崇郴"name"涓幏寰楀睘鎬ф爣绛俱
public array getAttributes(mixed $names=true)
| ||
$names | mixed | 灞炴х殑鍚嶇О锛屽叾鍊煎繀椤昏繑鍥炪 濡傛灉杩欐槸true锛堥粯璁わ級锛屽皢杩斿洖鎵鏈夊睘鎬э紝 鍖呮嫭閭d簺娌℃湁浠庢暟鎹簱鍔犺浇鐨勶紙杩欎簺灞炴у皢杩斿洖绌猴級銆 濡傛灉涓簄ull锛屽垯灏嗚繑鍥炴墍鏈夊睘鎬э紝鏁版嵁搴撴湭琚姞杞界殑闄ゅ銆 |
{return} | array | attribute values indexed by attribute names. |
public function getAttributes($names=true)
{
$attributes=$this->_attributes;
foreach($this->getMetaData()->columns as $name=>$column)
{
if(property_exists($this,$name))
$attributes[$name]=$this->$name;
else if($names===true && !isset($attributes[$name]))
$attributes[$name]=null;
}
if(is_array($names))
{
$attrs=array();
foreach($names as $name)
{
if(property_exists($this,$name))
$attrs[$name]=$this->$name;
else
$attrs[$name]=isset($attributes[$name])?$attributes[$name]:null;
}
return $attrs;
}
else
return $attributes;
}
杩斿洖鎵鏈夊垪鐨勫睘鎬у笺 娉ㄦ剰锛屼笉杩斿洖鐩稿叧鐨勫璞°
public CDbCommandBuilder getCommandBuilder()
| ||
{return} | CDbCommandBuilder | 杩欎釜鍛戒护鐢熸垚鍣ㄥ睘浜嶢R绫 |
public function getCommandBuilder()
{
return $this->getDbConnection()->getSchema()->getCommandBuilder();
}
杩斿洖姝R鐨勫懡浠ょ敓鎴愬櫒銆
public CDbConnection getDbConnection()
| ||
{return} | CDbConnection | 娲诲姩璁板綍浣跨敤鐨勬暟鎹簱杩炴帴銆 |
public function getDbConnection()
{
if(self::$db!==null)
return self::$db;
else
{
self::$db=Yii::app()->getDb();
if(self::$db instanceof CDbConnection)
return self::$db;
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}
}
杩斿洖娲诲姩璁板綍鎵浣跨敤鐨勬暟鎹簱杩炴帴銆 榛樿鎯呭喌涓嬶紝"db"搴旂敤绋嬪簭缁勪欢鐢ㄤ綔鏁版嵁搴撹繛鎺ャ 濡傛灉鎮ㄨ浣跨敤涓涓笉鍚岀殑鏁版嵁搴撹繛鎺ワ紝鎮ㄥ彲浠ラ噸鍐欐鏂规硶銆
public CDbCriteria getDbCriteria(boolean $createIfNull=true)
| ||
$createIfNull | boolean | 鏄惁鍒涘缓涓涓潯浠跺疄渚嬪鏋滃畠涓嶅瓨鍦ㄣ傞粯璁ゅ间负true銆 |
{return} | CDbCriteria | 鍜岃繖涓ā鍨嬬浉鍏崇殑鏌ヨ鏉′欢銆 杩欎釜鏉′欢涓昏琚 named scope浣跨敤锛 鐗瑰緛鏄Н绱笉鍚岀殑鏉′欢瑙勮寖銆 |
public function getDbCriteria($createIfNull=true)
{
if($this->_c===null)
{
if(($c=$this->defaultScope())!==array() || $createIfNull)
$this->_c=new CDbCriteria($c);
}
return $this->_c;
}
杩斿洖鍜岃繖涓ā鍨嬬浉鍏崇殑鏌ヨ鏉′欢銆
public boolean getIsNewRecord()
| ||
{return} | boolean | 璇ヨ褰曟槸鍚︽槸鏂扮殑锛屽氨鎻掑叆鏃惰皟鐢save銆 鍦ㄦ瀯閫犲嚱鏁板拰populateRecord鑷姩璁剧疆姝ゅ睘鎬с 榛樿鍊间负false锛 濡傛灉浣跨敤new杩愮畻绗﹀垱寤哄疄渚嬶紝瀹冧細琚缃负true銆 |
public function getIsNewRecord()
{
return $this->_new;
}
杩斿洖褰撳墠璁板綍鏄惁鏄柊鐨勩
public CActiveRecordMetaData getMetaData()
| ||
{return} | CActiveRecordMetaData | 杩欎釜AR绫荤殑鍏冩暟鎹 |
public function getMetaData()
{
if($this->_md!==null)
return $this->_md;
else
return $this->_md=self::model(get_class($this))->_md;
}
杩斿洖杩欎釜AR鐨勫厓鏁版嵁銆
public mixed getOldPrimaryKey()
| ||
{return} | mixed | 鏃х殑涓婚敭鍊笺傚垯杩斿洖鏁扮粍锛堝垪鍚=>鍒楀硷級濡傛灉涓婚敭鏄鍚堝瀷銆 If primary key is not defined, null will be returned. |
public function getOldPrimaryKey()
{
return $this->_pk;
}
杩斿洖鏃х殑涓婚敭鍊笺 杩欐寚鐨勬槸涓婚敭鍊煎凡濉厖鍒拌褰曞悗锛 鎵ц鏌ユ壘鏂规硶 (渚嬪锛 find()锛宖indAll())锛 鍗充娇涓诲叧閿睘鎬ч兘浼氳鎵嬪姩鍒嗛厤涓涓笉鍚岀殑鍊硷紝鍊间繚鎸佷笉鍙樸
public mixed getPrimaryKey()
| ||
{return} | mixed | 涓婚敭鍊笺傚垯杩斿洖鏁扮粍锛堝垪鍚=>鍒楀硷級濡傛灉涓婚敭鏄鍚堝瀷銆 濡傛灉娌℃湁瀹氫箟涓婚敭锛屽皢杩斿洖null銆 |
public function getPrimaryKey()
{
$table=$this->getMetaData()->tableSchema;
if(is_string($table->primaryKey))
return $this->{$table->primaryKey};
else if(is_array($table->primaryKey))
{
$values=array();
foreach($table->primaryKey as $name)
$values[$name]=$this->$name;
return $values;
}
else
return null;
}
杩斿洖涓婚敭鍊笺
public mixed getRelated(string $name, boolean $refresh=false, array $params=array (
))
| ||
$name | string | 鍏崇郴鍚 (鍙傝relations) |
$refresh | boolean | 鏄惁浠庢暟鎹簱閲嶆柊杞藉叆鐩稿叧瀵硅薄锛岄粯璁ゅ间负false銆 |
$params | array | 闄勫姞鍙傛暟锛屽湪鍏崇郴澹版槑涓寚瀹氳嚜瀹氫箟鏌ヨ鏉′欢銆 |
{return} | mixed | 鐩稿叧瀵硅薄. |
public function getRelated($name,$refresh=false,$params=array())
{
if(!$refresh && $params===array() && (isset($this->_related[$name]) || array_key_exists($name,$this->_related)))
return $this->_related[$name];
$md=$this->getMetaData();
if(!isset($md->relations[$name]))
throw new CDbException(Yii::t('yii','{class} does not have relation "{name}".',
array('{class}'=>get_class($this), '{name}'=>$name)));
Yii::trace('lazy loading '.get_class($this).'.'.$name,'system.db.ar.CActiveRecord');
$relation=$md->relations[$name];
if($this->getIsNewRecord() && !$refresh && ($relation instanceof CHasOneRelation || $relation instanceof CHasManyRelation))
return $relation instanceof CHasOneRelation ? null : array();
if($params!==array()) // dynamic query
{
$exists=isset($this->_related[$name]) || array_key_exists($name,$this->_related);
if($exists)
$save=$this->_related[$name];
$r=array($name=>$params);
}
else
$r=$name;
unset($this->_related[$name]);
$finder=new CActiveFinder($this,$r);
$finder->lazyFind($this);
if(!isset($this->_related[$name]))
{
if($relation instanceof CHasManyRelation)
$this->_related[$name]=array();
else if($relation instanceof CStatRelation)
$this->_related[$name]=$relation->defaultValue;
else
$this->_related[$name]=null;
}
if($params!==array())
{
$results=$this->_related[$name];
if($exists)
$this->_related[$name]=$save;
else
unset($this->_related[$name]);
return $results;
}
else
return $this->_related[$name];
}
杩斿洖鐩稿叧鐨勮褰曘 杩欎釜鏂规硶灏嗚繑鍥炲綋鍓嶈褰曠殑鐩稿叧璁板綍銆 濡傛灉鍏崇郴鏄 HAS_ONE 鎴 BELONGS_TO锛 濡傛灉瀵硅薄涓嶅瓨鍦紝灏嗚繑鍥炰竴涓崟涓璞℃垨null銆 濡傛灉鍏崇郴鏄疕AS_MANY 鎴 MANY_MANY锛 瀹冭繑鍥炰竴涓璞℃暟缁勬垨涓涓┖鏁扮粍銆
public string getTableAlias(boolean $quote=false, boolean $checkScopes=true)
| ||
$quote | boolean | 鏄惁瑕佸紩鐢ㄥ埆鍚 |
$checkScopes | boolean | 妫娴嬪埆鍚嶅綋鍓嶆槸鍚﹀湪搴旂敤鑼冨洿琚畾涔夈 璋冪敤defaultScope姝ゆ柟娉曟椂锛岃繖涓弬鏁板繀椤昏璁剧疆涓篺alse銆 鍚﹀垯灏嗗舰鎴愪竴涓棤闄愬惊鐜 |
{return} | string | 榛樿琛ㄥ埆鍚 |
public function getTableAlias($quote=false, $checkScopes=true)
{
if($checkScopes && ($criteria=$this->getDbCriteria(false))!==null && $criteria->alias!='')
$alias=$criteria->alias;
else
$alias=$this->_alias;
return $quote ? $this->getDbConnection()->getSchema()->quoteTableName($alias) : $alias;
}
杩斿洖鐢 find 鏂规硶浣跨敤琛ㄧ殑鍒悕銆 鍦ㄥ叧绯绘煡璇紝鏍规嵁瀵瑰簲鍏崇郴鐨勫0鏄庯紝 杩斿洖鐨勮〃鐨勫埆鍚嶅彲鑳戒細鏈夋墍涓嶅悓銆 姝ゅ锛岀敱setTableAlias鍙互鐢卞簲鐢ㄧ殑鑼冨洿瑕嗙洊銆
public CDbTableSchema getTableSchema()
| ||
{return} | CDbTableSchema | 杩欎釜琛ㄧ殑鍏冩暟鎹睘浜嶢R绫 |
public function getTableSchema()
{
return $this->getMetaData()->tableSchema;
}
杩斿洖琛ㄧ殑鍏冩暟鎹紝杩欏睘浜嶢R绫汇
public boolean hasAttribute(string $name)
| ||
$name | string | 灞炴у悕绉 |
{return} | boolean | 杩欐槸鍚︽槸AR鍛藉悕鐨勫睘鎬(table column). |
public function hasAttribute($name)
{
return isset($this->getMetaData()->columns[$name]);
}
妫鏌ユAR鏄惁鏈夊懡鍚嶇殑灞炴с
public boolean hasRelated(string $name)
| ||
$name | string | 鍏崇郴鍚 |
{return} | boolean | 杩斿洖涓涓兼寚绀烘槸鍚﹀懡鍚嶇浉鍏冲璞¤杞藉叆銆 |
public function hasRelated($name)
{
return isset($this->_related[$name]) || array_key_exists($name,$this->_related);
}
杩斿洖涓涓硷紝璇ュ兼寚绀烘槸鍚﹀凡鍔犺浇鍛藉悕鐨勭浉鍏冲璞°
public void init()
|
鍒濆鍖栬繖涓ā鍨嬨 褰撲竴涓狝R瀹炰緥鏄柊鍒涘缓鐨勬垨鏈夊畠鐨scenario璁剧疆锛 杩欎釜鏂规硶琚Е鍙戙 鎮ㄥ彲浠ラ噸鍐欐鏂规硶浠ユ彁渚涙墍闇鐨勫垵濮嬪寲鐨勪唬鐮佹ā鍨 (渚嬪锛氳缃垵濮嬬殑灞炴у)銆
public boolean insert(array $attributes=NULL)
| ||
$attributes | array | 闇瑕佷繚瀛樼殑灞炴у垪琛ㄣ傞粯璁や负null锛 杩欐剰鍛崇潃浠庢暟鎹簱涓姞杞界殑鎵鏈夊睘鎬у皢琚繚瀛樸 |
{return} | boolean | 灞炴ф槸鍚︽湁鏁堬紝骞跺皢璁板綍鎻掑叆鎴愬姛銆 |
public function insert($attributes=null)
{
if(!$this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.'));
if($this->beforeSave())
{
Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$table=$this->getMetaData()->tableSchema;
$command=$builder->createInsertCommand($table,$this->getAttributes($attributes));
if($command->execute())
{
$primaryKey=$table->primaryKey;
if($table->sequenceName!==null)
{
if(is_string($primaryKey) && $this->$primaryKey===null)
$this->$primaryKey=$builder->getLastInsertID($table);
else if(is_array($primaryKey))
{
foreach($primaryKey as $pk)
{
if($this->$pk===null)
{
$this->$pk=$builder->getLastInsertID($table);
break;
}
}
}
}
$this->_pk=$this->getPrimaryKey();
$this->afterSave();
$this->setIsNewRecord(false);
$this->setScenario('update');
return true;
}
}
return false;
}
鎻掑叆涓琛屽埌鍩轰簬杩欎釜ActiveRecord鐨勫睘鎬ц〃銆 濡傛灉琛ㄧ殑涓婚敭鏄嚜鍔ㄥ閲忓拰鎻掑叆鍓嶄负null锛 灏嗘彃鍏ュ悗鐨勫疄闄呭煎~鍏呫 娉ㄦ剰锛岄獙璇佷笉鎵ц姝ゆ柟娉曘備綘鍙互璋冪敤validate鎵ц楠岃瘉銆 璁板綍鎴愬姛鎻掑叆鍒版暟鎹簱鍚庯紝瀹冪殑isNewRecord灞炴у皢璁剧疆涓篺alse锛 鍜屽畠鐨scenario灞炴у皢璁剧疆涓衡榰pdate鈥欍
protected CActiveRecord instantiate(array $attributes)
| ||
$attributes | array | 娲诲姩璁板綍鐨勫睘鎬у煎垪琛ㄣ |
{return} | CActiveRecord | 娲诲姩璁板綍 |
protected function instantiate($attributes)
{
$class=get_class($this);
$model=new $class(null);
return $model;
}
鍒涘缓娲诲姩璁板綍瀹炰緥銆 杩欎釜鏂规硶璋冪敤populateRecord鍜populateRecords銆 濡傛灉姝e湪鍒涘缓鐨勫疄渚嬶紝 浣犲彲浠ラ噸鍐欐鏂规硶锛屽彇鍐充簬濉厖绾綍鐨勫睘鎬с 渚嬪锛岄氳繃鍒涘缓鍩轰簬鍒楃殑鍊肩殑璁板綍锛 浣犲彲浠ュ疄鐜版墍璋撶殑鍗曡〃缁ф壙鏄犲皠銆
public static CActiveRecord model(string $className='CActiveRecord')
| ||
$className | string | 娲诲姩璁板綍绫荤殑鍚嶇О銆 |
{return} | CActiveRecord | 娲诲姩璁板綍妯″瀷鐨勪竴涓疄渚嬨 |
public static function model($className=__CLASS__)
{
if(isset(self::$_models[$className]))
return self::$_models[$className];
else
{
$model=self::$_models[$className]=new $className(null);
$model->_md=new CActiveRecordMetaData($model);
$model->attachBehaviors($model->behaviors());
return $model;
}
}
杩斿洖鎸囧畾AR绫荤殑闈欐佹ā鍨嬨
杩斿洖鐨勬ā鍨嬫槸涓涓潤鎬佺殑AR绫荤殑瀹炰緥銆
瀹冩彁渚涗簡鐢ㄤ簬璋冪敤绫荤骇鍒殑鏂规硶锛堟煇浜涗技涔庝簬闈欐佺被鏂规硶銆傦級
姣忎釜娲剧敓绫诲繀椤婚噸鍐欐鏂规硶濡備笅锛
public static function model($className=__CLASS__) { return parent::model($className); }
public boolean offsetExists(mixed $offset)
| ||
$offset | mixed | 妫鏌ュ埌鐨勫亸绉婚噺 |
{return} | boolean |
public function offsetExists($offset)
{
return $this->__isset($offset);
}
杩斿洖鏄惁鏈夋寚瀹氬亸绉婚噺鐨勫厓绱犮 杩欑鏂规硶闇瑕佹帴鍙rrayAccess銆
public void onAfterDelete(CEvent $event)
| ||
$event | CEvent | 浜嬩欢鍙傛暟 |
public function onAfterDelete($event)
{
$this->raiseEvent('onAfterDelete',$event);
}
鍦ㄥ垹闄よ褰曚箣鍚庯紝灏嗗紩鍙戞浜嬩欢銆
public void onAfterFind(CEvent $event)
| ||
$event | CEvent | 浜嬩欢鍙傛暟 |
public function onAfterFind($event)
{
$this->raiseEvent('onAfterFind',$event);
}
寮曞彂姝や簨浠惰褰曞悗鎵惧埌鏂规硶鐨勫疄渚嬨
public void onAfterSave(CEvent $event)
| ||
$event | CEvent | 浜嬩欢鍙傛暟 |
public function onAfterSave($event)
{
$this->raiseEvent('onAfterSave',$event);
}
淇濆瓨璁板綍涔嬪悗锛屽皢寮曞彂姝や簨浠躲
public void onBeforeDelete(CModelEvent $event)
| ||
$event | CModelEvent | 浜嬩欢鍙傛暟 |
public function onBeforeDelete($event)
{
$this->raiseEvent('onBeforeDelete',$event);
}
鍦ㄥ垹闄よ褰曚箣鍓嶏紝灏嗗紩鍙戞浜嬩欢銆 閫氳繃璁剧疆CModelEvent::isValid涓篺alse锛屾甯delete()杩涚▼琚仠姝€
public void onBeforeFind(CModelEvent $event)
| ||
$event | CModelEvent | 浜嬩欢鍙傛暟 |
public function onBeforeFind($event)
{
$this->raiseEvent('onBeforeFind',$event);
}
AR finder鎵ц鏌ユ壘璋冪敤涔嬪墠锛屽皢寮曞彂姝や簨浠躲 鍦ㄨ繖绉嶆儏鍐典笅锛岃繖CModelEvent::criteria涓睘鎬т綔涓哄弬鏁颁紶閫掔粰姝ゆ柟娉曚綔涓烘煡璇㈡潯浠躲 濡傛灉浣犺璁块棶鑼冨洿涓寚瀹氱殑鏌ヨ鏉′欢锛 璇蜂娇鐢getDbCriteria()銆 浣犲彲浠ヤ慨鏀逛换浣曟潯浠讹紝鏍规嵁闇姹傚畾鍒躲
鍙傝
public void onBeforeSave(CModelEvent $event)
| ||
$event | CModelEvent | 浜嬩欢鍙傛暟 |
public function onBeforeSave($event)
{
$this->raiseEvent('onBeforeSave',$event);
}
淇濆瓨璁板綍涔嬪墠锛屽皢寮曞彂姝や簨浠躲 閫氳繃璁剧疆CModelEvent::isValid涓篺alse锛屾甯save()杩涚▼琚仠姝€
public CActiveRecord populateRecord(array $attributes, boolean $callAfterFind=true)
| ||
$attributes | array | 灞炴у硷紙鍒楀悕=>鍒楀硷級 |
$callAfterFind | boolean | 鏄惁瑕佽褰曞悗璋冪敤afterFind濉厖銆 |
{return} | CActiveRecord | 鏂板垱寤虹殑娲诲姩璁板綍銆傜被鐨勫璞℃槸鐩稿悓鐨勬ā鍨嬬被銆 濡傛灉杈撳叆鐨勬暟鎹槸false锛屽垯杩斿洖null銆 |
public function populateRecord($attributes,$callAfterFind=true)
{
if($attributes!==false)
{
$record=$this->instantiate($attributes);
$record->setScenario('update');
$record->init();
$md=$record->getMetaData();
foreach($attributes as $name=>$value)
{
if(property_exists($record,$name))
$record->$name=$value;
else if(isset($md->columns[$name]))
$record->_attributes[$name]=$value;
}
$record->_pk=$record->getPrimaryKey();
$record->attachBehaviors($record->behaviors());
if($callAfterFind)
$record->afterFind();
return $record;
}
else
return null;
}
缁欐寚瀹氱殑灞炴у垱寤烘椿鍔ㄨ褰 姝ゆ柟娉曠敱 find 鏂规硶鍐呴儴浣跨敤銆
public array populateRecords(array $data, boolean $callAfterFind=true, string $index=NULL)
| ||
$data | array | 娲诲姩璁板綍鐨勫睘鎬у煎垪琛ㄣ |
$callAfterFind | boolean | 鏄惁瑕佽褰曞悗璋冪敤afterFind濉厖銆 |
$index | string | 鍏跺煎皢鐢ㄤ綔鏌ヨ缁撴灉鏁扮粍绱㈠紩灞炴х殑鍚嶇О銆 濡傛灉涓簄ull锛岃繖鎰忓懗鐫灏嗕粠闆跺紑濮嬬殑鏁存暟绱㈠紩鐨勬暟缁勩 |
{return} | array | 娲诲姩璁板綍鐨勫垪琛 |
public function populateRecords($data,$callAfterFind=true,$index=null)
{
$records=array();
foreach($data as $attributes)
{
if(($record=$this->populateRecord($attributes,$callAfterFind))!==null)
{
if($index===null)
$records[]=$record;
else
$records[$record->$index]=$record;
}
}
return $records;
}
鍒涘缓鍩轰簬杈撳叆鏁版嵁鐨勬椿鍔ㄨ褰曠殑鍒楄〃銆 姝ゆ柟娉曠敱 find 鏂规硶鍐呴儴浣跨敤銆
public mixed primaryKey()
| ||
{return} | mixed | 鐩稿叧鏁版嵁琛ㄧ殑涓婚敭銆 濡傛灉璇ラ敭鏄竴涓崟涓鐨勫垪锛屽畠搴斿綋杩斿洖鍒楃殑鍚嶇О锛 濡傛灉閿槸鐢卞涓垪缁勬垚鐨勫鍚堥敭, 瀹冨簲璇ヨ繑鍥炴暟缁勭殑閿垪鍚嶇О銆 |
杩斿洖鐩稿叧鏁版嵁搴撹〃鐨勪富閿 杩欑鏂规硶鏄寚琛ㄦ病鏈夊畾涔変富閿椂锛堜竴浜沴egency鏁版嵁搴擄級鐨勬儏鍐典笅閲嶅啓銆 濡傛灉琛ㄥ凡瀹氫箟涓婚敭锛 浣犱笉闇瑕侀噸鍐欐鏂规硶銆傞粯璁ゅ疄鐜板彧杩斿洖null锛 鎰忔濇槸杩斿洖鍦ㄦ暟鎹簱涓畾涔夌殑涓婚敭銆
protected mixed query(CDbCriteria $criteria, boolean $all=false)
| ||
$criteria | CDbCriteria | the query criteria |
$all | boolean | whether to return all data |
{return} | mixed | the AR objects populated with the query result |
protected function query($criteria,$all=false)
{
$this->beforeFind();
$this->applyScopes($criteria);
if(empty($criteria->with))
{
if(!$all)
$criteria->limit=1;
$command=$this->getCommandBuilder()->createFindCommand($this->getTableSchema(),$criteria);
return $all ? $this->populateRecords($command->queryAll(), true, $criteria->index) : $this->populateRecord($command->queryRow());
}
else
{
$finder=new CActiveFinder($this,$criteria->with);
return $finder->query($criteria,$all);
}
}
鎵ц瀹為檯鐨勬暟鎹簱鏌ヨ鍜屽~鍏匒R瀵硅薄鐨勬煡璇㈢粨鏋溿 姝ゆ柟娉曠敱鍏朵粬 AR 鏌ヨ鏂规硶璋冪敤锛屼富瑕佹槸鍐呴儴浣跨敤銆
public boolean refresh()
| ||
{return} | boolean | 璇ヨ鏄惁浠嶇劧瀛樺湪浜庢暟鎹簱涓傚鏋滄儏鍐靛睘瀹烇紝鏈鏂扮殑鏁版嵁灏嗚濉厖鍒拌繖涓椿鍔ㄨ褰曘 |
public function refresh()
{
Yii::trace(get_class($this).'.refresh()','system.db.ar.CActiveRecord');
if(!$this->getIsNewRecord() && ($record=$this->findByPk($this->getPrimaryKey()))!==null)
{
$this->_attributes=array();
$this->_related=array();
foreach($this->getMetaData()->columns as $name=>$column)
{
if(property_exists($this,$name))
$this->$name=$record->$name;
else
$this->_attributes[$name]=$record->$name;
}
return true;
}
else
return false;
}
鏈鏂扮殑鏁版嵁灏嗚濉厖鍒拌繖涓椿鍔ㄨ褰曘
public void refreshMetaData()
|
public function refreshMetaData()
{
$finder=self::model(get_class($this));
$finder->_md=new CActiveRecordMetaData($finder);
if($this!==$finder)
$this->_md=$finder->_md;
}
鍒锋柊杩欎釜AR绫荤殑鍏冩暟鎹 閫氳繃璋冪敤杩欎釜鏂规硶锛岃繖涓狝R绫诲皢閲嶆柊鐢熸垚闇瑕佺殑鍏冩暟鎹 濡傛灉琛ㄧ殑缁撴瀯宸茬粡鏀瑰彉锛屼笖浣犳兂瑕佷娇鐢ㄦ渶鏂扮殑锛岃繖鏄潪甯告湁鐢ㄧ殑銆 鍦ㄤ綘璋冪敤杩欎釜鏂规硶鍓嶏紝纭繚浣犲凡缁忚皟鐢CDbSchema::refresh锛 瑕佷笉鐒讹紝鏃ц〃鐨勭殑鏁版嵁缁撴瀯浠嶈浣跨敤銆
public array relations()
| ||
{return} | array | 鐩稿叧瀵硅薄鐢虫槑鍒楄〃銆 榛樿鍊间负绌烘暟缁勩 |
鐢虫槑鍏崇郴鐨勫璞★紝搴旈噸鍐欐鏂规硶銆
鍙兘瀛樺湪涓や釜娲诲姩璁板綍瀵硅薄涔嬮棿鐨勫叧绯绘湁鍥涚绫诲瀷锛
- BELONGS_TO: 渚嬪锛屾垚鍛樺睘浜庝竴涓洟闃燂紱
- HAS_ONE: 渚嬪锛屾垚鍛樻湁涓汉璧勬枡锛
- HAS_MANY: 渚嬪锛屼竴涓洟闃熸湁寰堝鎴愬憳锛
- MANY_MANY: 渚嬪锛屾垚鍛樻湁寰堝鐨勬妧宸у拰鎶鑳芥槸灞炰簬杩欎釜鎴愬憳鐨勶紱
闄や簡涓婅堪鐨勫叧绯荤被鍨嬶紝 杩樻敮鎸佷竴绉嶇壒娈婄殑鍏崇郴绉颁负STAT锛屽彲浠ョ敤鏉ヨ繘琛岀粺璁℃煡璇紙鎴栬仛鍚堟煡璇級銆 瀹冩绱㈣仛鍚堝鐩稿叧瀵硅薄鐨勪俊鎭紝 濡傛瘡涓笘瀛愶紝瀵逛簬姣忎釜浜у搧锛屽钩鍧囧垎鏁颁负娉ㄩ噴鐨勬暟鐩瓑銆
鐩稿叧瀵硅薄鐨勬瘡涓被瀹氫箟鍦ㄦ鏂规硶涓綔涓哄叿鏈変互涓嬪厓绱犵殑鏁扮粍锛
'varName'=>array('relationType', 'className', 'foreign_key', ...additional options)鍏朵腑鈥榲arName鈥欐槸鎵瑰彉閲/灞炴х殑鍚嶇О锛屽彲浠ラ氳繃瀵硅薄鏉ヨ闂紱 鈥榬elationType鈥欐槸鎸囧叧绯荤殑绫诲瀷锛屽畠鍙互鏄互涓嬪洓绉嶅父閲忎箣涓锛 self::BELONGS_TO锛宻elf::HAS_ONE锛宻elf::HAS_MANY 鍜 self::MANY_MANY锛 鈥榗lassName鈥欐槸鎸囨椿鍔ㄨ褰曠被鐨勭浉鍏冲璞$殑鍚嶇О锛 鍜屸榝oreign_key鈥欒瀹氱殑澶栭敭娑夊強涓ょ娲诲姩璁板綍銆 璇锋敞鎰忓鍚堝閿紝 浠栦滑鍙互涓璧峰垪鍑猴紝浠ラ楀彿鍒嗛殧锛屾垨鑰呬綔涓轰竴涓暟缁勬寚瀹氭暟缁勭殑鏍煎紡锛堚榢ey1锛宬ey2鈥欙級銆 濡傛灉浣犻渶瑕佹寚瀹氳嚜瀹氫箟鐨刾k->fk鍏宠仈锛屼綘鍙互瀹氫箟鏁扮粍锛堚榝k鈥=> 鈥榩k'锛夈 瀵逛簬缁勫悎閿繖灏嗘槸鏁扮粍锛堚榝k_c1鈥=>鈥榩k_褋1鈥欙紝鈥榝k_c2鈥=>鈥榩k_c2鈥欙級銆 MANY_MANY鍏崇郴涓娇鐢ㄥ閿紝 鍔犲叆琛ㄥ繀椤诲0鏄庝负锛堜緥濡傦細鈥榡oin_table(fk1锛宖k2)鈥欙級銆
鍏朵綑鏁扮粍鍏冪礌鐨勫悕绉-鍊硷紝鍙互鎸囧畾鍏朵粬閫夐」锛
- 'select': string|array, 琚変腑鐨勫垪鍒楄〃銆傞粯璁ゅ间负 '*'锛岃繖鎰忓懗鐫鏄墍鏈夊垪銆 濡傛灉瀹冧滑鍑虹幇鍦ㄤ竴涓〃杈惧紡涓紙渚嬪锛 COUNT(relationName.name) AS name_count锛夈
- 'condition': string, WHERE瀛愬彞銆 榛樿鍊间负绌恒 娉ㄦ剰锛屽垪寮曠敤闇瑕佸姞鍏'relationName'鍓嶇紑 銆(渚嬪锛歳elationName.age>20)
- 'order': string, ORDER BY瀛愬彞銆 榛樿鍊间负绌恒 娉ㄦ剰锛屽垪寮曠敤闇瑕佸姞鍏'relationName'鍓嶇紑 銆(渚嬪锛歳elationName.age DESC)
- 'with': string|array, 搴旇涓庤繖涓璞′竴璧疯杞界殑瀛愬璞$浉鍏冲悕鍗曘 娉ㄦ剰锛岃繖浠呬粎鏄欢杩熷姞杞斤紝鑰屼笉鏄鍏堝姞杞姐
- 'joinType': 鑱旀帴绫诲瀷銆 榛樿鍊间负鈥楲EFT OUTER JOIN鈥欍
- 'alias': 鍏崇郴涓庡叧鑱旇〃鐨勫埆鍚嶃 瀹冮粯璁や负null锛 鎰忓懗鐫琛ㄧ殑鍒悕涓庡叧绯诲悕绉扮浉鍚屻
- 'params': 鍙傛暟缁戝畾鍒拌鐢熸垚鐨凷QL璇彞涓娿 杩欏簲浣滀负鍚嶇О-鍊肩殑鏁扮粍銆
- 'on': ON 瀛愬彞銆 姝ゅ鎸囧畾鐨勬潯浠跺皢杩藉姞鍒颁娇鐢 AND 杩愮畻绗︾殑鑱旀帴鏉′欢銆
- 'index': 鍏跺煎簲浣滀负瀛樺偍鐩稿叧鐨勫璞$殑鏁扮粍鐨勯敭鐨勫垪鐨勫悕绉般 姝ら夐」浠呴傜敤浜庯紝 HAS_MANY 鍜 MANY_MANY 鍏崇郴銆
- 'scopes': 搴旂敤鑼冨洿銆傚湪鍗曚竴鐨勮寖鍥存儏鍐典笅鍙互鍍'scopes'=>'scopeName'锛 鍦ㄥ涓綔鐢ㄥ煙鑼冨洿鎯呭喌涓嬪彲浠ヤ娇鐢ㄦ暟缁勫儚杩欐牱'scopes'=>array('scopeName1','scopeName2')銆 姝ら夐」鑷増鏈 1.1.9 鍙敤銆
寤惰繜鍔犺浇鏃舵湁涓瀹氱殑鍏崇郴锛屼笅鍒楅夐」鍙敤锛
- 'group': string, GROUP BY瀛愬彞銆 榛樿鍊间负绌恒 娉ㄦ剰锛屽垪寮曠敤闇瑕佸姞鍏'relationName'鍓嶇紑 銆(渚嬪锛 relationName.age)銆傛閫夐」浠呴傜敤浜嶩AS_MANY 鍜 MANY_MANY 鍏崇郴銆
- 'having': string, HAVING瀛愬彞銆 榛樿鍊间负绌恒 娉ㄦ剰锛屽垪寮曠敤闇瑕佸姞鍏'relationName'鍓嶇紑 銆(渚嬪锛 relationName.age)銆傛閫夐」浠呴傜敤浜嶩AS_MANY 鍜 MANY_MANY 鍏崇郴銆
- 'limit': 鏁版嵁琛岀殑limit閫夋嫨銆 杩欎釜閫夐」涓嶈兘搴旂敤鍒癇ELONGS_TO銆
- 'offset': 鏁版嵁琛岀殑鍋忕Щ閲忋 杩欎釜閫夐」涓嶈兘搴旂敤鍒癇ELONGS_TO銆
- 'through': 鑾峰彇鐩稿叧鐨勬暟鎹椂灏嗙敤浣滄ˉ鐨勬ā鍨嬬殑鍏崇郴鐨勫悕绉般傚彲浠ヨ缃粎涓 HAS_ONE 鍜 HAS_MANY銆傛閫夐」鑷増鏈 1.1.7 鍙敤銆
浠ヤ笅鏄竴涓緥瀛愶紝涓 'Post' 娲诲姩璁板綍绫荤浉鍏冲璞$殑涓涓ず渚嬶細
return array( 'author'=>array(self::BELONGS_TO, 'User', 'author_id'), 'comments'=>array(self::HAS_MANY, 'Comment', 'post_id', 'with'=>'author', 'order'=>'create_time DESC'), 'tags'=>array(self::MANY_MANY, 'Tag', 'post_tag(post_id, tag_id)', 'order'=>'name'), );
public CActiveRecord resetScope()
| ||
{return} | CActiveRecord |
public function resetScope()
{
$this->_c=new CDbCriteria();
return $this;
}
閲嶇疆鎵鏈夊彉閲忎綔鐢ㄥ煙锛屽寘鎷粯璁ょ殑鑼冨洿銆
public boolean save(boolean $runValidation=true, array $attributes=NULL)
| ||
$runValidation | boolean | 淇濆瓨璁板綍涔嬪墠鏄惁鎵ц楠岃瘉銆 濡傛灉楠岃瘉澶辫触锛岃褰曚笉浼氳淇濆瓨鍒版暟鎹簱銆 |
$attributes | array | 闇瑕佷繚瀛樼殑灞炴у垪琛ㄣ 榛樿涓簄ull锛岃繖鎰忓懗鐫浠庢暟鎹簱涓姞杞界殑鎵鏈夊睘鎬у皢琚繚瀛樸 |
{return} | boolean | whether the saving succeeds |
public function save($runValidation=true,$attributes=null)
{
if(!$runValidation || $this->validate($attributes))
return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
else
return false;
}
淇濆瓨褰撳墠鐨勮褰曘
鎻掑叆璁板綍鍒版暟鎹〃鐨勪竴琛岋紝
濡傛灉瀹冪殑isNewRecord灞炴т负true锛堥氬父鎯呭喌涓嬩娇鐢ㄧ殑鈥榥ew鈥欒繍绠楃鏉ュ垱寤鸿褰曪級銆
鍚﹀垯锛
灏嗚鐢ㄤ簬鏇存柊琛ㄤ腑鐨勭浉搴旇锛堥氬父鎯呭喌涓嬶紝浣跨敤鈥榝ind鈥欐柟娉曟潵鏌ユ壘璁板綍锛夈
淇濆瓨璁板綍涔嬪墠灏嗘墽琛岄獙璇併
濡傛灉楠岃瘉澶辫触锛岃璁板綍灏嗕笉浼氫繚瀛樸
浣犲彲浠ヨ皟鐢getErrors()妫绱㈤獙璇侀敊璇
濡傛灉璇ヨ褰曢氳繃鎻掑叆淇濆瓨锛
瀹isNewRecord鐨勫睘鎬т細琚缃负false锛屽拰瀹冪殑scenario灞炴ц璁剧疆涓衡榰pdate鈥欍
濡傛灉鍏朵富閿槸鑷姩澧為噺锛屽苟涓斿湪鎻掑叆涔嬪墠鏈缃紝
灏嗕娇鐢ㄨ嚜鍔ㄧ敓鎴愮殑鍊兼潵濉厖涓婚敭銆
public boolean saveAttributes(array $attributes)
| ||
$attributes | array | 瑕佹洿鏂扮殑灞炴с 姣忎釜鍏冪礌琛ㄧず鐨勫睘鎬у悕绉版垨鎸夊叾鍚嶇О绱㈠紩鐨勫睘鎬у笺 濡傛灉鏄悗鑰咃紝璁板綍鐨勫睘鎬у皢鎹涔嬪墠淇濆瓨鐨勬洿鏀广 |
{return} | boolean | 鏇存柊鏄惁鎴愬姛 |
public function saveAttributes($attributes)
{
if(!$this->getIsNewRecord())
{
Yii::trace(get_class($this).'.saveAttributes()','system.db.ar.CActiveRecord');
$values=array();
foreach($attributes as $name=>$value)
{
if(is_integer($name))
$values[$value]=$this->$value;
else
$values[$name]=$this->$name=$value;
}
if($this->_pk===null)
$this->_pk=$this->getPrimaryKey();
if($this->updateByPk($this->getOldPrimaryKey(),$values)>0)
{
$this->_pk=$this->getPrimaryKey();
return true;
}
else
return false;
}
else
throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));
}
淇濆瓨鎵閫夌殑灞炴у垪琛ㄣ 涓嶅悓浜save锛岃繖涓柟娉曞彧淇濆瓨鐜版湁鐨勫垪鏁版嵁闆嗙殑鎸囧畾鐨勫睘鎬э紝 骞朵笖涓嶈皟鐢beforeSave鎴afterSave銆 璇锋敞鎰忚繖涓柟娉曚笉杩囨护灞炴т篃涓嶅仛楠岃瘉銆 鎵浠ヤ笉瑕佽繖绉嶆柟娉曟帴鍙椾笉鍙椾俊浠荤殑鏁版嵁锛堝鐢ㄦ埛鍙戝竷鐨勬暟鎹級銆 濡傛灉浣犳兂杩欐牱鍋氾紝鍙互鑰冭檻浠ヤ笅鏂规硶浠f浛锛
$postRecord=Post::model()->findByPk($postID); $postRecord->attributes=$_POST['post']; $postRecord->save();
public boolean saveCounters(array $counters)
| ||
$counters | array | 瑕佹洿鏂拌鏁板櫒锛堝垪鍚=>澧為噺鍊硷級 |
{return} | boolean | 淇濆瓨鏄惁鎴愬姛 |
public function saveCounters($counters)
{
Yii::trace(get_class($this).'.saveCounters()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$table=$this->getTableSchema();
$criteria=$builder->createPkCriteria($table,$this->getOldPrimaryKey());
$command=$builder->createUpdateCounterCommand($this->getTableSchema(),$counters,$criteria);
if($command->execute())
{
foreach($counters as $name=>$value)
$this->$name=$this->$name+$value;
return true;
}
else
return false;
}
淇濆瓨涓涓垨澶氬綋鍓岮R瀵硅薄鍒拌鏁板櫒銆 璇锋敞鎰忥紝姝ゆ柟娉曚笉鍚屼簬updateCounters 锛 瀹冨彧淇濆瓨褰撳墠AR瀵硅薄銆 鐢ㄦ硶绀轰緥濡備笅鎵绀猴細
$postRecord=Post::model()->findByPk($postID); $postRecord->saveCounters(array('view_count'=>1));濡傛灉浣犳兂鍑忓皯璁℃暟鍣紝璇蜂娇鐢ㄨ礋鍊笺
鍙傝
public array scopes()
| ||
{return} | array | 鑼冨洿鐨勭晫瀹氥傛暟缁勪腑鐨勯敭鑼冨洿鍚嶇О; 鏁扮粍涓殑鍊兼槸鐩稿簲鐨勮寖鍥村畾涔夈 姣忎釜鑼冨洿瀹氫箟琚〃绀轰负涓涓暟缁勶紝鍏堕敭蹇呴』鏄CDbCriteria鐨勫睘鎬с |
杩斿洖鐢虫槑鐨勫懡鍚嶈寖鍥淬 浠h〃涓涓煡璇㈡潯浠剁殑鍛藉悕鑼冨洿锛屽彲浠ヤ覆杩炲湪涓璧蜂笌鍏朵粬鐨勫懡鍚嶈寖鍥村拰鏌ヨ銆 杩欑鏂规硶搴旇閲嶅啓锛 瀛愮被澹版槑涓虹壒瀹氱殑AR绫荤殑鍛藉悕鑼冨洿銆 渚嬪锛 涓嬮潰鐨勪唬鐮佸0鏄庝袱涓懡鍚嶈寖鍥粹榬ecently鈥欏拰鈥榩ublished鈥欍
return array( 'published'=>array( 'condition'=>'status=1', ), 'recently'=>array( 'order'=>'create_time DESC', 'limit'=>5, ), );濡傛灉涓婅堪鑼冨洿鐨勭敵鏄庡湪Post妯″瀷锛 鎴戜滑鍙互鎵ц浠ヤ笅鏌ヨ锛
$posts=Post::model()->published()->findAll(); $posts=Post::model()->published()->recently()->findAll(); $posts=Post::model()->published()->with('comments')->findAll();闇瑕佹敞鎰忕殑鏄渶鍚庝竴涓煡璇㈡槸涓涓叧鑱旀煡璇€
public boolean setAttribute(string $name, mixed $value)
| ||
$name | string | 灞炴у悕绉 |
$value | mixed | 灞炴у |
{return} | boolean | 灞炴ф槸鍚﹀瓨鍦ㄥ拰浠诲姟鎴愬姛杩涜 |
public function setAttribute($name,$value)
{
if(property_exists($this,$name))
$this->$name=$value;
else if(isset($this->getMetaData()->columns[$name]))
$this->_attributes[$name]=$value;
else
return false;
return true;
}
璁剧疆鍛藉悕鐨勫睘鎬у笺 浣犱篃鍙互浣跨敤$this->AttributeName鏉ヨ缃睘鎬у笺
鍙傝
public void setDbCriteria(CDbCriteria $criteria)
| ||
$criteria | CDbCriteria | 鏌ヨ鏉′欢 |
public function setDbCriteria($criteria)
{
$this->_c=$criteria;
}
涓哄綋鍓嶆ā鍨嬭缃煡璇㈡潯浠.
public void setIsNewRecord(boolean $value)
| ||
$value | boolean | 璇ヨ褰曟槸鍚︽槸鏂扮殑锛屽氨鎻掑叆鏃惰皟鐢save銆 |
public function setIsNewRecord($value)
{
$this->_new=$value;
}
璁剧疆璁板綍鏄惁鏄柊鐨勩
鍙傝
public void setOldPrimaryKey(mixed $value)
| ||
$value | mixed | 鏃т富閿笺 |
public function setOldPrimaryKey($value)
{
$this->_pk=$value;
}
璁剧疆鏃х殑涓婚敭鍊笺
public void setPrimaryKey(mixed $value)
| ||
$value | mixed | 鏂扮殑涓婚敭鍊笺傚鏋滀富閿槸澶嶅悎绫诲瀷锛 搴旀彁渚涗竴涓柊鍊煎仛涓烘暟缁勶紙鍒楀悕=>鍒楀硷級銆 |
public function setPrimaryKey($value)
{
$this->_pk=$this->getPrimaryKey();
$table=$this->getMetaData()->tableSchema;
if(is_string($table->primaryKey))
$this->{$table->primaryKey}=$value;
else if(is_array($table->primaryKey))
{
foreach($table->primaryKey as $name)
$this->$name=$value[$name];
}
}
璁剧疆涓婚敭鍊笺 璋冪敤姝ゆ柟娉曞悗锛屾棫鐨勪富閿硷紝鍙互浠oldPrimaryKey寰楀埌銆
public void setTableAlias(string $alias)
| ||
$alias | string | 鍦ㄦ煡璇腑浣跨敤琛ㄧ殑鍒悕銆傚埆鍚嶄笉搴旇鍔犲紩鍙枫 |
public function setTableAlias($alias)
{
$this->_alias=$alias;
}
璁剧疆琛ㄥ湪鏌ヨ涓娇鐢ㄧ殑鍒悕銆
public string tableName()
| ||
{return} | string | 杩斿洖琛ㄥ悕 |
public function tableName()
{
return get_class($this);
}
杩斿洖鍏宠仈鐨勬暟鎹簱琛ㄧ殑鍚嶇О銆 榛樿鎯呭喌涓嬶紝杩欎釜鏂规硶杩斿洖绫诲悕浣滀负琛ㄥ悕銆 浣犲彲浠ヨ鐩栬繖涓柟娉曪紝濡傛灉姝ゅ垪娌℃湁鍛藉悕鍦ㄨ繖娆$害瀹氫箣鍚庛
public CActiveRecord together()
| ||
{return} | CActiveRecord | AR瀵硅薄鏈韩銆 |
public function together()
{
$this->getDbCriteria()->together=true;
return $this;
}
璁剧疆CDbCriteria::together灞炴т负true銆 杩欏彧鐢ㄤ簬鍏崇郴AR鏌ヨ銆 璇峰弬瑙CDbCriteria::together鏇村璇︾粏銆
public boolean update(array $attributes=NULL)
| ||
$attributes | array | 闇瑕佷繚瀛樼殑灞炴у垪琛ㄣ傞粯璁や负null锛 杩欐剰鍛崇潃浠庢暟鎹簱涓姞杞界殑鎵鏈夊睘鎬у皢琚繚瀛樸 |
{return} | boolean | 鏇存柊鏄惁鎴愬姛 |
public function update($attributes=null)
{
if($this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));
if($this->beforeSave())
{
Yii::trace(get_class($this).'.update()','system.db.ar.CActiveRecord');
if($this->_pk===null)
$this->_pk=$this->getPrimaryKey();
$this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes));
$this->_pk=$this->getPrimaryKey();
$this->afterSave();
return true;
}
else
return false;
}
鏇存柊姝ゆ椿鍔ㄨ褰曟墍琛ㄧず鐨勮銆 宸插姞杞界殑鎵鏈夊睘鎬ч兘灏嗕繚瀛樺埌鏁版嵁搴撲腑銆 娉ㄦ剰锛岄獙璇佷笉鎵ц姝ゆ柟娉曘備綘鍙互璋冪敤validate鎵ц楠岃瘉銆
public integer updateAll(array $attributes, mixed $condition='', array $params=array (
))
| ||
$attributes | array | 鐢ㄤ簬鏇存柊鐨勫睘鎬у垪琛紙name=>$value锛夈 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | integer | 琚洿鏂扮殑琛屾暟 |
public function updateAll($attributes,$condition='',$params=array())
{
Yii::trace(get_class($this).'.updateAll()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createCriteria($condition,$params);
$command=$builder->createUpdateCommand($this->getTableSchema(),$attributes,$criteria);
return $command->execute();
}
鏇存柊浣跨敤鎸囧畾鏉′欢鐨勮褰曘 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆 娉ㄦ剰锛屽睘鎬ф病鏈夊仛瀹夊叏妫娴嬪拰鏍¢獙銆
public integer updateByPk(mixed $pk, array $attributes, mixed $condition='', array $params=array (
))
| ||
$pk | mixed | 涓婚敭鍊笺傚澶氫釜涓婚敭浣跨敤鏁扮粍銆傚鍚堥敭锛屽浜庢瘡涓敭鐨勫煎繀椤绘槸涓涓暟缁勶紙鍒楀悕=>鍒楀硷級銆 |
$attributes | array | 鐢ㄤ簬鏇存柊鐨勫睘鎬у垪琛紙name=>$value锛夈 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | integer | 琚洿鏂扮殑琛屾暟 |
public function updateByPk($pk,$attributes,$condition='',$params=array())
{
Yii::trace(get_class($this).'.updateByPk()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$table=$this->getTableSchema();
$criteria=$builder->createPkCriteria($table,$pk,$condition,$params);
$command=$builder->createUpdateCommand($table,$attributes,$criteria);
return $command->execute();
}
鏇存柊鎸囧畾涓婚敭璁板綍鐨勮銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆 娉ㄦ剰锛屽睘鎬ф病鏈夊仛瀹夊叏妫娴嬪拰鏍¢獙銆
public integer updateCounters(array $counters, mixed $condition='', array $params=array (
))
| ||
$counters | array | 瑕佹洿鏂扮殑璁℃暟鍣紙鍒楀悕=>澧為噺鍊硷級 |
$condition | mixed | 鏌ヨ鏉′欢鎴栨爣鍑嗐 |
$params | array | 瑕佺粦瀹氬埌鐨 SQL 璇彞鐨勫弬鏁 |
{return} | integer | 琚洿鏂扮殑琛屾暟 |
public function updateCounters($counters,$condition='',$params=array())
{
Yii::trace(get_class($this).'.updateCounters()','system.db.ar.CActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createCriteria($condition,$params);
$command=$builder->createUpdateCounterCommand($this->getTableSchema(),$counters,$criteria);
return $command->execute();
}
鏇存柊鐨勪竴涓垨澶氫釜璁℃暟鍣ㄣ 璇锋敞鎰忥紝闄ら潪鎸囧畾涓涓潯浠舵垨鏍囧噯锛岃繖灏嗘洿鏂版墍鏈夋暟鎹銆 鍙傝find()璇︾粏璇存槑鍏充簬$condition鍜$params銆
鍙傝
public CActiveRecord with()
| ||
{return} | CActiveRecord | AR瀵硅薄鏈韩銆 |
public function with()
{
if(func_num_args()>0)
{
$with=func_get_args();
if(is_array($with[0])) // the parameter is given as an array
$with=$with[0];
if(!empty($with))
$this->getDbCriteria()->mergeWith(array('with'=>$with));
}
return $this;
}
鎸囧畾鐩稿叧瀵硅薄搴斾竴璧疯浇鍏ャ 杩欎釜鏂规硶閲囧彇鍙橀噺鐨勫弬鏁般 姣忎釜鍙傛暟鎸囧畾鍏崇郴鎴栧瓙鍏崇郴鐨勫悕绉般備緥濡傦紝
// find all posts together with their author and comments Post::model()->with('author','comments')->findAll(); // find all posts together with their author and the author's profile Post::model()->with('author','author.profile')->findAll();搴旂敤涓0鏄庣殑鍏崇郴relations()銆
榛樿鎯呭喌涓嬶紝鍦ㄦ寚瀹氱殑relations()閫夐」灏嗚鐢ㄦ潵鍋氬叧鑱旀煡璇€ 涓轰簡鑷畾涔夊姩鎬侀夐」锛屾垜浠簲灏嗕竴涓暟缁勫弬鏁颁紶閫掔粰 with() 鏂规硶銆 鏁扮粍鐨勯敭鏄叧绯荤殑鍚嶇О锛 鍜屾暟缁勫煎搴旂殑鏌ヨ閫夐」銆 渚嬪锛
Post::model()->with(array( 'author'=>array('select'=>'id, name'), 'comments'=>array('condition'=>'approved=1', 'order'=>'create_time'), ))->findAll();