You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 line
17KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db;
  8. /**
  9. * ActiveRecordInterface
  10. *
  11. * @author Qiang Xue <qiang.xue@gmail.com>
  12. * @author Carsten Brandt <mail@cebe.cc>
  13. * @since 2.0
  14. */
  15. interface ActiveRecordInterface
  16. {
  17. /**
  18. * Returns the primary key **name(s)** for this AR class.
  19. *
  20. * Note that an array should be returned even when the record only has a single primary key.
  21. *
  22. * For the primary key **value** see [[getPrimaryKey()]] instead.
  23. *
  24. * @return string[] the primary key name(s) for this AR class.
  25. */
  26. public static function primaryKey();
  27. /**
  28. * Returns the list of all attribute names of the record.
  29. * @return array list of attribute names.
  30. */
  31. public function attributes();
  32. /**
  33. * Returns the named attribute value.
  34. * If this record is the result of a query and the attribute is not loaded,
  35. * null will be returned.
  36. * @param string $name the attribute name
  37. * @return mixed the attribute value. Null if the attribute is not set or does not exist.
  38. * @see hasAttribute()
  39. */
  40. public function getAttribute($name);
  41. /**
  42. * Sets the named attribute value.
  43. * @param string $name the attribute name.
  44. * @param mixed $value the attribute value.
  45. * @see hasAttribute()
  46. */
  47. public function setAttribute($name, $value);
  48. /**
  49. * Returns a value indicating whether the record has an attribute with the specified name.
  50. * @param string $name the name of the attribute
  51. * @return boolean whether the record has an attribute with the specified name.
  52. */
  53. public function hasAttribute($name);
  54. /**
  55. * Returns the primary key value(s).
  56. * @param boolean $asArray whether to return the primary key value as an array. If true,
  57. * the return value will be an array with attribute names as keys and attribute values as values.
  58. * Note that for composite primary keys, an array will always be returned regardless of this parameter value.
  59. * @return mixed the primary key value. An array (attribute name => attribute value) is returned if the primary key
  60. * is composite or `$asArray` is true. A string is returned otherwise (null will be returned if
  61. * the key value is null).
  62. */
  63. public function getPrimaryKey($asArray = false);
  64. /**
  65. * Returns the old primary key value(s).
  66. * This refers to the primary key value that is populated into the record
  67. * after executing a find method (e.g. find(), findOne()).
  68. * The value remains unchanged even if the primary key attribute is manually assigned with a different value.
  69. * @param boolean $asArray whether to return the primary key value as an array. If true,
  70. * the return value will be an array with column name as key and column value as value.
  71. * If this is false (default), a scalar value will be returned for non-composite primary key.
  72. * @property mixed The old primary key value. An array (column name => column value) is
  73. * returned if the primary key is composite. A string is returned otherwise (null will be
  74. * returned if the key value is null).
  75. * @return mixed the old primary key value. An array (column name => column value) is returned if the primary key
  76. * is composite or `$asArray` is true. A string is returned otherwise (null will be returned if
  77. * the key value is null).
  78. */
  79. public function getOldPrimaryKey($asArray = false);
  80. /**
  81. * Returns a value indicating whether the given set of attributes represents the primary key for this model
  82. * @param array $keys the set of attributes to check
  83. * @return boolean whether the given set of attributes represents the primary key for this model
  84. */
  85. public static function isPrimaryKey($keys);
  86. /**
  87. * Creates an [[ActiveQueryInterface]] instance for query purpose.
  88. *
  89. * The returned [[ActiveQueryInterface]] instance can be further customized by calling
  90. * methods defined in [[ActiveQueryInterface]] before `one()` or `all()` is called to return
  91. * populated ActiveRecord instances. For example,
  92. *
  93. * ```php
  94. * // find the customer whose ID is 1
  95. * $customer = Customer::find()->where(['id' => 1])->one();
  96. *
  97. * // find all active customers and order them by their age:
  98. * $customers = Customer::find()
  99. * ->where(['status' => 1])
  100. * ->orderBy('age')
  101. * ->all();
  102. * ```
  103. *
  104. * This method is also called by [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to
  105. * create a relational query.
  106. *
  107. * You may override this method to return a customized query. For example,
  108. *
  109. * ```php
  110. * class Customer extends ActiveRecord
  111. * {
  112. * public static function find()
  113. * {
  114. * // use CustomerQuery instead of the default ActiveQuery
  115. * return new CustomerQuery(get_called_class());
  116. * }
  117. * }
  118. * ```
  119. *
  120. * The following code shows how to apply a default condition for all queries:
  121. *
  122. * ```php
  123. * class Customer extends ActiveRecord
  124. * {
  125. * public static function find()
  126. * {
  127. * return parent::find()->where(['deleted' => false]);
  128. * }
  129. * }
  130. *
  131. * // Use andWhere()/orWhere() to apply the default condition
  132. * // SELECT FROM customer WHERE `deleted`=:deleted AND age>30
  133. * $customers = Customer::find()->andWhere('age>30')->all();
  134. *
  135. * // Use where() to ignore the default condition
  136. * // SELECT FROM customer WHERE age>30
  137. * $customers = Customer::find()->where('age>30')->all();
  138. *
  139. * @return ActiveQueryInterface the newly created [[ActiveQueryInterface]] instance.
  140. */
  141. public static function find();
  142. /**
  143. * Returns a single active record model instance by a primary key or an array of column values.
  144. *
  145. * The method accepts:
  146. *
  147. * - a scalar value (integer or string): query by a single primary key value and return the
  148. * corresponding record (or null if not found).
  149. * - an array of name-value pairs: query by a set of attribute values and return a single record
  150. * matching all of them (or null if not found).
  151. *
  152. * Note that this method will automatically call the `one()` method and return an
  153. * [[ActiveRecordInterface|ActiveRecord]] instance. For example,
  154. *
  155. * ```php
  156. * // find a single customer whose primary key value is 10
  157. * $customer = Customer::findOne(10);
  158. *
  159. * // the above code is equivalent to:
  160. * $customer = Customer::find()->where(['id' => 10])->one();
  161. *
  162. * // find the first customer whose age is 30 and whose status is 1
  163. * $customer = Customer::findOne(['age' => 30, 'status' => 1]);
  164. *
  165. * // the above code is equivalent to:
  166. * $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
  167. * ```
  168. *
  169. * @param mixed $condition primary key value or a set of column values
  170. * @return static|null ActiveRecord instance matching the condition, or null if nothing matches.
  171. */
  172. public static function findOne($condition);
  173. /**
  174. * Returns a list of active record models that match the specified primary key value(s) or a set of column values.
  175. *
  176. * The method accepts:
  177. *
  178. * - a scalar value (integer or string): query by a single primary key value and return an array containing the
  179. * corresponding record (or an empty array if not found).
  180. * - an array of scalar values (integer or string): query by a list of primary key values and return the
  181. * corresponding records (or an empty array if none was found).
  182. * Note that an empty condition will result in an empty result as it will be interpreted as a search for
  183. * primary keys and not an empty `WHERE` condition.
  184. * - an array of name-value pairs: query by a set of attribute values and return an array of records
  185. * matching all of them (or an empty array if none was found).
  186. *
  187. * Note that this method will automatically call the `all()` method and return an array of
  188. * [[ActiveRecordInterface|ActiveRecord]] instances. For example,
  189. *
  190. * ```php
  191. * // find the customers whose primary key value is 10
  192. * $customers = Customer::findAll(10);
  193. *
  194. * // the above code is equivalent to:
  195. * $customers = Customer::find()->where(['id' => 10])->all();
  196. *
  197. * // find the customers whose primary key value is 10, 11 or 12.
  198. * $customers = Customer::findAll([10, 11, 12]);
  199. *
  200. * // the above code is equivalent to:
  201. * $customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
  202. *
  203. * // find customers whose age is 30 and whose status is 1
  204. * $customers = Customer::findAll(['age' => 30, 'status' => 1]);
  205. *
  206. * // the above code is equivalent to:
  207. * $customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
  208. * ```
  209. *
  210. * @param mixed $condition primary key value or a set of column values
  211. * @return array an array of ActiveRecord instance, or an empty array if nothing matches.
  212. */
  213. public static function findAll($condition);
  214. /**
  215. * Updates records using the provided attribute values and conditions.
  216. * For example, to change the status to be 1 for all customers whose status is 2:
  217. *
  218. * ~~~
  219. * Customer::updateAll(['status' => 1], ['status' => '2']);
  220. * ~~~
  221. *
  222. * @param array $attributes attribute values (name-value pairs) to be saved for the record.
  223. * Unlike [[update()]] these are not going to be validated.
  224. * @param array $condition the condition that matches the records that should get updated.
  225. * Please refer to [[QueryInterface::where()]] on how to specify this parameter.
  226. * An empty condition will match all records.
  227. * @return integer the number of rows updated
  228. */
  229. public static function updateAll($attributes, $condition = null);
  230. /**
  231. * Deletes records using the provided conditions.
  232. * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
  233. *
  234. * For example, to delete all customers whose status is 3:
  235. *
  236. * ~~~
  237. * Customer::deleteAll([status = 3]);
  238. * ~~~
  239. *
  240. * @param array $condition the condition that matches the records that should get deleted.
  241. * Please refer to [[QueryInterface::where()]] on how to specify this parameter.
  242. * An empty condition will match all records.
  243. * @return integer the number of rows deleted
  244. */
  245. public static function deleteAll($condition = null);
  246. /**
  247. * Saves the current record.
  248. *
  249. * This method will call [[insert()]] when [[getIsNewRecord()|isNewRecord]] is true, or [[update()]]
  250. * when [[getIsNewRecord()|isNewRecord]] is false.
  251. *
  252. * For example, to save a customer record:
  253. *
  254. * ~~~
  255. * $customer = new Customer; // or $customer = Customer::findOne($id);
  256. * $customer->name = $name;
  257. * $customer->email = $email;
  258. * $customer->save();
  259. * ~~~
  260. *
  261. * @param boolean $runValidation whether to perform validation before saving the record.
  262. * If the validation fails, the record will not be saved to database. `false` will be returned
  263. * in this case.
  264. * @param array $attributeNames list of attributes that need to be saved. Defaults to null,
  265. * meaning all attributes that are loaded from DB will be saved.
  266. * @return boolean whether the saving succeeds
  267. */
  268. public function save($runValidation = true, $attributeNames = null);
  269. /**
  270. * Inserts the record into the database using the attribute values of this record.
  271. *
  272. * Usage example:
  273. *
  274. * ```php
  275. * $customer = new Customer;
  276. * $customer->name = $name;
  277. * $customer->email = $email;
  278. * $customer->insert();
  279. * ```
  280. *
  281. * @param boolean $runValidation whether to perform validation before saving the record.
  282. * If the validation fails, the record will not be inserted into the database.
  283. * @param array $attributes list of attributes that need to be saved. Defaults to null,
  284. * meaning all attributes that are loaded from DB will be saved.
  285. * @return boolean whether the attributes are valid and the record is inserted successfully.
  286. */
  287. public function insert($runValidation = true, $attributes = null);
  288. /**
  289. * Saves the changes to this active record into the database.
  290. *
  291. * Usage example:
  292. *
  293. * ```php
  294. * $customer = Customer::findOne($id);
  295. * $customer->name = $name;
  296. * $customer->email = $email;
  297. * $customer->update();
  298. * ```
  299. *
  300. * @param boolean $runValidation whether to perform validation before saving the record.
  301. * If the validation fails, the record will not be inserted into the database.
  302. * @param array $attributeNames list of attributes that need to be saved. Defaults to null,
  303. * meaning all attributes that are loaded from DB will be saved.
  304. * @return integer|boolean the number of rows affected, or false if validation fails
  305. * or updating process is stopped for other reasons.
  306. * Note that it is possible that the number of rows affected is 0, even though the
  307. * update execution is successful.
  308. */
  309. public function update($runValidation = true, $attributeNames = null);
  310. /**
  311. * Deletes the record from the database.
  312. *
  313. * @return integer|boolean the number of rows deleted, or false if the deletion is unsuccessful for some reason.
  314. * Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
  315. */
  316. public function delete();
  317. /**
  318. * Returns a value indicating whether the current record is new (not saved in the database).
  319. * @return boolean whether the record is new and should be inserted when calling [[save()]].
  320. */
  321. public function getIsNewRecord();
  322. /**
  323. * Returns a value indicating whether the given active record is the same as the current one.
  324. * Two [[getIsNewRecord()|new]] records are considered to be not equal.
  325. * @param static $record record to compare to
  326. * @return boolean whether the two active records refer to the same row in the same database table.
  327. */
  328. public function equals($record);
  329. /**
  330. * Returns the relation object with the specified name.
  331. * A relation is defined by a getter method which returns an object implementing the [[ActiveQueryInterface]]
  332. * (normally this would be a relational [[ActiveQuery]] object).
  333. * It can be declared in either the ActiveRecord class itself or one of its behaviors.
  334. * @param string $name the relation name
  335. * @param boolean $throwException whether to throw exception if the relation does not exist.
  336. * @return ActiveQueryInterface the relational query object
  337. */
  338. public function getRelation($name, $throwException = true);
  339. /**
  340. * Establishes the relationship between two records.
  341. *
  342. * The relationship is established by setting the foreign key value(s) in one record
  343. * to be the corresponding primary key value(s) in the other record.
  344. * The record with the foreign key will be saved into database without performing validation.
  345. *
  346. * If the relationship involves a junction table, a new row will be inserted into the
  347. * junction table which contains the primary key values from both records.
  348. *
  349. * This method requires that the primary key value is not null.
  350. *
  351. * @param string $name the case sensitive name of the relationship.
  352. * @param static $model the record to be linked with the current one.
  353. * @param array $extraColumns additional column values to be saved into the junction table.
  354. * This parameter is only meaningful for a relationship involving a junction table
  355. * (i.e., a relation set with `[[ActiveQueryInterface::via()]]`.)
  356. */
  357. public function link($name, $model, $extraColumns = []);
  358. /**
  359. * Destroys the relationship between two records.
  360. *
  361. * The record with the foreign key of the relationship will be deleted if `$delete` is true.
  362. * Otherwise, the foreign key will be set null and the record will be saved without validation.
  363. *
  364. * @param string $name the case sensitive name of the relationship.
  365. * @param static $model the model to be unlinked from the current one.
  366. * @param boolean $delete whether to delete the model that contains the foreign key.
  367. * If false, the model's foreign key will be set null and saved.
  368. * If true, the model containing the foreign key will be deleted.
  369. */
  370. public function unlink($name, $model, $delete = false);
  371. /**
  372. * Returns the connection used by this AR class.
  373. * @return mixed the database connection used by this AR class.
  374. */
  375. public static function getDb();
  376. }