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.

415 lines
18KB

  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. * - a non-associative array: query by a list of primary key values and return the
  150. * first record (or `null` if not found).
  151. * - an associative array of name-value pairs: query by a set of attribute values and return a single record
  152. * matching all of them (or `null` if not found). Note that `['id' => 1, 2]` is treated as a non-associative array.
  153. *
  154. * That this method will automatically call the `one()` method and return an [[ActiveRecordInterface|ActiveRecord]]
  155. * instance. For example,
  156. *
  157. * ```php
  158. * // find a single customer whose primary key value is 10
  159. * $customer = Customer::findOne(10);
  160. *
  161. * // the above code is equivalent to:
  162. * $customer = Customer::find()->where(['id' => 10])->one();
  163. *
  164. * // find the first customer whose age is 30 and whose status is 1
  165. * $customer = Customer::findOne(['age' => 30, 'status' => 1]);
  166. *
  167. * // the above code is equivalent to:
  168. * $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
  169. * ```
  170. *
  171. * @param mixed $condition primary key value or a set of column values
  172. * @return static ActiveRecord instance matching the condition, or `null` if nothing matches.
  173. */
  174. public static function findOne($condition);
  175. /**
  176. * Returns a list of active record models that match the specified primary key value(s) or a set of column values.
  177. *
  178. * The method accepts:
  179. *
  180. * - a scalar value (integer or string): query by a single primary key value and return an array containing the
  181. * corresponding record (or an empty array if not found).
  182. * - a non-associative array: query by a list of primary key values and return the
  183. * corresponding records (or an empty array if none was found).
  184. * Note that an empty condition will result in an empty result as it will be interpreted as a search for
  185. * primary keys and not an empty `WHERE` condition.
  186. * - an associative array of name-value pairs: query by a set of attribute values and return an array of records
  187. * matching all of them (or an empty array if none was found). Note that `['id' => 1, 2]` is treated as
  188. * a non-associative array.
  189. *
  190. * This method will automatically call the `all()` method and return an array of [[ActiveRecordInterface|ActiveRecord]]
  191. * instances. For example,
  192. *
  193. * ```php
  194. * // find the customers whose primary key value is 10
  195. * $customers = Customer::findAll(10);
  196. *
  197. * // the above code is equivalent to:
  198. * $customers = Customer::find()->where(['id' => 10])->all();
  199. *
  200. * // find the customers whose primary key value is 10, 11 or 12.
  201. * $customers = Customer::findAll([10, 11, 12]);
  202. *
  203. * // the above code is equivalent to:
  204. * $customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
  205. *
  206. * // find customers whose age is 30 and whose status is 1
  207. * $customers = Customer::findAll(['age' => 30, 'status' => 1]);
  208. *
  209. * // the above code is equivalent to:
  210. * $customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
  211. * ```
  212. *
  213. * @param mixed $condition primary key value or a set of column values
  214. * @return array an array of ActiveRecord instance, or an empty array if nothing matches.
  215. */
  216. public static function findAll($condition);
  217. /**
  218. * Updates records using the provided attribute values and conditions.
  219. * For example, to change the status to be 1 for all customers whose status is 2:
  220. *
  221. * ```php
  222. * Customer::updateAll(['status' => 1], ['status' => '2']);
  223. * ```
  224. *
  225. * @param array $attributes attribute values (name-value pairs) to be saved for the record.
  226. * Unlike [[update()]] these are not going to be validated.
  227. * @param array $condition the condition that matches the records that should get updated.
  228. * Please refer to [[QueryInterface::where()]] on how to specify this parameter.
  229. * An empty condition will match all records.
  230. * @return integer the number of rows updated
  231. */
  232. public static function updateAll($attributes, $condition = null);
  233. /**
  234. * Deletes records using the provided conditions.
  235. * WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
  236. *
  237. * For example, to delete all customers whose status is 3:
  238. *
  239. * ```php
  240. * Customer::deleteAll([status = 3]);
  241. * ```
  242. *
  243. * @param array $condition the condition that matches the records that should get deleted.
  244. * Please refer to [[QueryInterface::where()]] on how to specify this parameter.
  245. * An empty condition will match all records.
  246. * @return integer the number of rows deleted
  247. */
  248. public static function deleteAll($condition = null);
  249. /**
  250. * Saves the current record.
  251. *
  252. * This method will call [[insert()]] when [[getIsNewRecord()|isNewRecord]] is true, or [[update()]]
  253. * when [[getIsNewRecord()|isNewRecord]] is false.
  254. *
  255. * For example, to save a customer record:
  256. *
  257. * ```php
  258. * $customer = new Customer; // or $customer = Customer::findOne($id);
  259. * $customer->name = $name;
  260. * $customer->email = $email;
  261. * $customer->save();
  262. * ```
  263. *
  264. * @param boolean $runValidation whether to perform validation (calling [[Model::validate()|validate()]])
  265. * before saving the record. Defaults to `true`. If the validation fails, the record
  266. * will not be saved to the database and this method will return `false`.
  267. * @param array $attributeNames list of attribute names that need to be saved. Defaults to `null`,
  268. * meaning all attributes that are loaded from DB will be saved.
  269. * @return boolean whether the saving succeeded (i.e. no validation errors occurred).
  270. */
  271. public function save($runValidation = true, $attributeNames = null);
  272. /**
  273. * Inserts the record into the database using the attribute values of this record.
  274. *
  275. * Usage example:
  276. *
  277. * ```php
  278. * $customer = new Customer;
  279. * $customer->name = $name;
  280. * $customer->email = $email;
  281. * $customer->insert();
  282. * ```
  283. *
  284. * @param boolean $runValidation whether to perform validation (calling [[Model::validate()|validate()]])
  285. * before saving the record. Defaults to `true`. If the validation fails, the record
  286. * will not be saved to the database and this method will return `false`.
  287. * @param array $attributes list of attributes that need to be saved. Defaults to `null`,
  288. * meaning all attributes that are loaded from DB will be saved.
  289. * @return boolean whether the attributes are valid and the record is inserted successfully.
  290. */
  291. public function insert($runValidation = true, $attributes = null);
  292. /**
  293. * Saves the changes to this active record into the database.
  294. *
  295. * Usage example:
  296. *
  297. * ```php
  298. * $customer = Customer::findOne($id);
  299. * $customer->name = $name;
  300. * $customer->email = $email;
  301. * $customer->update();
  302. * ```
  303. *
  304. * @param boolean $runValidation whether to perform validation (calling [[Model::validate()|validate()]])
  305. * before saving the record. Defaults to `true`. If the validation fails, the record
  306. * will not be saved to the database and this method will return `false`.
  307. * @param array $attributeNames list of attributes that need to be saved. Defaults to `null`,
  308. * meaning all attributes that are loaded from DB will be saved.
  309. * @return integer|boolean the number of rows affected, or `false` if validation fails
  310. * or updating process is stopped for other reasons.
  311. * Note that it is possible that the number of rows affected is 0, even though the
  312. * update execution is successful.
  313. */
  314. public function update($runValidation = true, $attributeNames = null);
  315. /**
  316. * Deletes the record from the database.
  317. *
  318. * @return integer|boolean the number of rows deleted, or `false` if the deletion is unsuccessful for some reason.
  319. * Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
  320. */
  321. public function delete();
  322. /**
  323. * Returns a value indicating whether the current record is new (not saved in the database).
  324. * @return boolean whether the record is new and should be inserted when calling [[save()]].
  325. */
  326. public function getIsNewRecord();
  327. /**
  328. * Returns a value indicating whether the given active record is the same as the current one.
  329. * Two [[getIsNewRecord()|new]] records are considered to be not equal.
  330. * @param static $record record to compare to
  331. * @return boolean whether the two active records refer to the same row in the same database table.
  332. */
  333. public function equals($record);
  334. /**
  335. * Returns the relation object with the specified name.
  336. * A relation is defined by a getter method which returns an object implementing the [[ActiveQueryInterface]]
  337. * (normally this would be a relational [[ActiveQuery]] object).
  338. * It can be declared in either the ActiveRecord class itself or one of its behaviors.
  339. * @param string $name the relation name
  340. * @param boolean $throwException whether to throw exception if the relation does not exist.
  341. * @return ActiveQueryInterface the relational query object
  342. */
  343. public function getRelation($name, $throwException = true);
  344. /**
  345. * Populates the named relation with the related records.
  346. * Note that this method does not check if the relation exists or not.
  347. * @param string $name the relation name (case-sensitive)
  348. * @param ActiveRecordInterface|array|null $records the related records to be populated into the relation.
  349. * @since 2.0.8
  350. */
  351. public function populateRelation($name, $records);
  352. /**
  353. * Establishes the relationship between two records.
  354. *
  355. * The relationship is established by setting the foreign key value(s) in one record
  356. * to be the corresponding primary key value(s) in the other record.
  357. * The record with the foreign key will be saved into database without performing validation.
  358. *
  359. * If the relationship involves a junction table, a new row will be inserted into the
  360. * junction table which contains the primary key values from both records.
  361. *
  362. * This method requires that the primary key value is not `null`.
  363. *
  364. * @param string $name the case sensitive name of the relationship.
  365. * @param static $model the record to be linked with the current one.
  366. * @param array $extraColumns additional column values to be saved into the junction table.
  367. * This parameter is only meaningful for a relationship involving a junction table
  368. * (i.e., a relation set with [[ActiveQueryInterface::via()]]).
  369. */
  370. public function link($name, $model, $extraColumns = []);
  371. /**
  372. * Destroys the relationship between two records.
  373. *
  374. * The record with the foreign key of the relationship will be deleted if `$delete` is true.
  375. * Otherwise, the foreign key will be set `null` and the record will be saved without validation.
  376. *
  377. * @param string $name the case sensitive name of the relationship.
  378. * @param static $model the model to be unlinked from the current one.
  379. * @param boolean $delete whether to delete the model that contains the foreign key.
  380. * If false, the model's foreign key will be set `null` and saved.
  381. * If true, the model containing the foreign key will be deleted.
  382. */
  383. public function unlink($name, $model, $delete = false);
  384. /**
  385. * Returns the connection used by this AR class.
  386. * @return mixed the database connection used by this AR class.
  387. */
  388. public static function getDb();
  389. }