Browse Source

Update getting the primary key and display field.

mark_story 12 years ago
parent
commit
f934878366

+ 32 - 34
src/Console/Command/Task/ModelTask.php

@@ -111,6 +111,8 @@ class ModelTask extends BakeTask {
 
 		$object = $this->getTableObject($model, $table);
 		$associations = $this->getAssociations($object);
+		$primaryKey = $this->getPrimaryKey($model);
+		$displayField = $this->getDisplayField($model);
 
 		if ($this->bake($object, false)) {
 			if ($this->_checkUnitTest()) {
@@ -302,6 +304,32 @@ class ModelTask extends BakeTask {
 	}
 
 /**
+ * Get the display field from the model or parameters
+ *
+ * @param Cake\ORM\Table $model The model to introspect.
+ * @return string
+ */
+	public function getDisplayField($model) {
+		if (!empty($this->params['display-field'])) {
+			return $this->params['display-field'];
+		}
+		return $model->displayField();
+	}
+
+/**
+ * Get the primary key field from the model or parameters
+ *
+ * @param Cake\ORM\Table $model The model to introspect.
+ * @return array The columns in the primary key
+ */
+	public function getPrimaryKey($model) {
+		if (!empty($this->params['primary-key'])) {
+			return (array)$this->params['primary-key'];
+		}
+		return (array)$model->primaryKey();
+	}
+
+/**
  * Generate a key value list of options and a prompt.
  *
  * @param array $options Array of options to use for the selections. indexes must start at 0
@@ -453,40 +481,6 @@ class ModelTask extends BakeTask {
 	}
 
 /**
- * Finds a primary Key in a list of fields.
- *
- * @param array $fields Array of fields that might have a primary key.
- * @return string Name of field that is a primary key.
- */
-	public function findPrimaryKey($fields) {
-		$name = 'id';
-		foreach ($fields as $name => $field) {
-			if (isset($field['key']) && $field['key'] === 'primary') {
-				break;
-			}
-		}
-		return $this->in(__d('cake_console', 'What is the primaryKey?'), null, $name);
-	}
-
-/**
- * interact with the user to find the displayField value for a model.
- *
- * @param array $fields Array of fields to look for and choose as a displayField
- * @return mixed Name of field to use for displayField or false if the user declines to choose
- */
-	public function findDisplayField($fields) {
-		$fieldNames = array_keys($fields);
-		$prompt = __d('cake_console', "A displayField could not be automatically detected\nwould you like to choose one?");
-		$continue = $this->in($prompt, ['y', 'n']);
-		if (strtolower($continue) === 'n') {
-			return false;
-		}
-		$prompt = __d('cake_console', 'Choose a field from the options above:');
-		$choice = $this->inOptions($fieldNames, $prompt);
-		return $fieldNames[$choice];
-	}
-
-/**
  * Handles Generation and user interaction for creating validation.
  *
  * @param Model $model Model to have validations generated for.
@@ -971,6 +965,10 @@ class ModelTask extends BakeTask {
 			'help' => __d('cake_console', 'Disable generating accessible fields in the entity.')
 		])->addOption('fields', [
 			'help' => __d('cake_console', 'A comma separated list of fields to make accessible.')
+		])->addOption('primary-key', [
+			'help' => __d('cake_console', 'The primary key if you would like to manually set one.')
+		])->addOption('display-field', [
+			'help' => __d('cake_console', 'The displayField if you would like to choose one.')
 		])->epilog(
 			__d('cake_console', 'Omitting all arguments and options will list ' .
 				'the table names you can generate models for')

+ 0 - 56
tests/TestCase/Console/Command/Task/ModelTaskTest.php

@@ -556,62 +556,6 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
- * test that finding primary key works
- *
- * @return void
- */
-	public function testFindPrimaryKey() {
-		$this->markTestIncomplete('Not done here yet');
-		$fields = array(
-			'one' => array(),
-			'two' => array(),
-			'key' => array('key' => 'primary')
-		);
-		$anything = new \PHPUnit_Framework_Constraint_IsAnything();
-		$this->Task->expects($this->once())->method('in')
-			->with($anything, null, 'key')
-			->will($this->returnValue('my_field'));
-
-		$result = $this->Task->findPrimaryKey($fields);
-		$expected = 'my_field';
-		$this->assertEquals($expected, $result);
-	}
-
-/**
- * test finding Display field
- *
- * @return void
- */
-	public function testFindDisplayFieldNone() {
-		$this->markTestIncomplete('Not done here yet');
-		$fields = array(
-			'id' => array(), 'tagname' => array(), 'body' => array(),
-			'created' => array(), 'modified' => array()
-		);
-		$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
-		$result = $this->Task->findDisplayField($fields);
-		$this->assertFalse($result);
-	}
-
-/**
- * Test finding a displayname from user input
- *
- * @return void
- */
-	public function testFindDisplayName() {
-		$this->markTestIncomplete('Not done here yet');
-		$fields = array(
-			'id' => array(), 'tagname' => array(), 'body' => array(),
-			'created' => array(), 'modified' => array()
-		);
-		$this->Task->expects($this->any())->method('in')
-			->will($this->onConsecutiveCalls('y', 2));
-
-		$result = $this->Task->findDisplayField($fields);
-		$this->assertEquals('tagname', $result);
-	}
-
-/**
  * test non interactive doAssociations
  *
  * @return void