Skip to content
This repository
Browse code

Added ability to pass ID as parameter to Model::exists()

  • Loading branch information...
commit 4edb378ef8126bb8239d529f9c200f043c291433 1 parent fd92720
ADmad authored February 23, 2012
14  lib/Cake/Model/Model.php
@@ -2537,19 +2537,23 @@ protected function _collectForeignKeys($type = 'belongsTo') {
2537 2537
 	}
2538 2538
 
2539 2539
 /**
2540  
- * Returns true if a record with the currently set ID exists.
  2540
+ * Returns true if a record with particular ID exists.
2541 2541
  *
2542  
- * Internally calls Model::getID() to obtain the current record ID to verify,
  2542
+ * If $id is not passed it calls Model::getID() to obtain the current record ID,
2543 2543
  * and then performs a Model::find('count') on the currently configured datasource
2544 2544
  * to ascertain the existence of the record in persistent storage.
2545 2545
  *
  2546
+ * @param mixed $id ID of record to check for existence
2546 2547
  * @return boolean True if such a record exists
2547 2548
  */
2548  
-	public function exists() {
2549  
-		if ($this->getID() === false) {
  2549
+	public function exists($id = null) {
  2550
+		if ($id === null) {
  2551
+			$id = $this->getID();
  2552
+		}
  2553
+		if ($id === false) {
2550 2554
 			return false;
2551 2555
 		}
2552  
-		$conditions = array($this->alias . '.' . $this->primaryKey => $this->getID());
  2556
+		$conditions = array($this->alias . '.' . $this->primaryKey => $id);
2553 2557
 		$query = array('conditions' => $conditions, 'recursive' => -1, 'callbacks' => false);
2554 2558
 		return ($this->find('count', $query) > 0);
2555 2559
 	}
29  lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -25,6 +25,25 @@
25 25
 class ModelReadTest extends BaseModelTest {
26 26
 
27 27
 /**
  28
+ * testExists function
  29
+ * @retun void
  30
+ */
  31
+	public function testExists() {
  32
+		$this->loadFixtures('User');
  33
+		$TestModel = new User();
  34
+
  35
+		$this->assertTrue($TestModel->exists(1));
  36
+
  37
+		$TestModel->id = 2;
  38
+		$this->assertTrue($TestModel->exists());
  39
+
  40
+		$TestModel->delete();
  41
+		$this->assertFalse($TestModel->exists());
  42
+
  43
+		$this->assertFalse($TestModel->exists(2));
  44
+	}
  45
+
  46
+/**
28 47
  * testFetchingNonUniqueFKJoinTableRecords()
29 48
  *
30 49
  * Tests if the results are properly returned in the case there are non-unique FK's
@@ -7784,18 +7803,18 @@ public function testGetVirtualField() {
7784 7803
 		$this->assertEquals($Post->getVirtualField('other_field'), $Post->virtualFields['other_field']);
7785 7804
 		$this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']);
7786 7805
 	}
7787  
-	
7788  
-	
  7806
+
  7807
+
7789 7808
 /**
7790 7809
  * test that checks for error when NOT condition passed in key and a 1 element array value
7791 7810
  *
7792 7811
  * @return void
7793  
- */	
  7812
+ */
7794 7813
 	public function testNotInArrayWithOneValue() {
7795 7814
 		$this->loadFixtures('Article');
7796 7815
 		$Article = new Article();
7797 7816
 		$Article->recursive = -1;
7798  
-		
  7817
+
7799 7818
 		$result = $Article->find(
7800 7819
 			'all',
7801 7820
 			array(
@@ -7804,7 +7823,7 @@ public function testNotInArrayWithOneValue() {
7804 7823
 				)
7805 7824
 			)
7806 7825
 		);
7807  
-		
  7826
+
7808 7827
 		$this->assertTrue(is_array($result) && !empty($result));
7809 7828
     }
7810 7829
 }

0 notes on commit 4edb378

Please sign in to comment.
Something went wrong with that request. Please try again.