Browse Source

Basic template and functionality for baking entity classes.

mark_story 12 years ago
parent
commit
e255d90822

+ 23 - 18
src/Console/Command/Task/ModelTask.php

@@ -504,26 +504,31 @@ class ModelTask extends BakeTask {
 		if (!empty($this->params['no-entity'])) {
 			return;
 		}
-		$data += [
-			'fields' => [],
-		];
+		$name = $model->alias();
+
+		$ns = Configure::read('App.namespace');
 		$pluginPath = '';
 		if ($this->plugin) {
+			$ns = $this->plugin;
 			$pluginPath = $this->plugin . '.';
 		}
 
-		$this->Template->set($data);
-		$this->Template->set([
-			'appNamespace' => Configure::read('App.namespace'),
+		$data += [
+			'name' => Inflector::singularize($name),
+			'namespace' => $ns,
 			'plugin' => $this->plugin,
-			'pluginPath' => $pluginPath
-		]);
+			'pluginPath' => $pluginPath,
+			'fields' => [],
+		];
+
+		$this->Template->set($data);
 		$out = $this->Template->generate('classes', 'entity');
 
 		$path = $this->getPath();
 		$filename = $path . 'Entity/' . $name . '.php';
 		$this->out("\n" . __d('cake_console', 'Baking entity class for %s...', $name), 1, Shell::QUIET);
 		$this->createFile($filename, $out);
+		return $out;
 	}
 
 /**
@@ -538,8 +543,18 @@ class ModelTask extends BakeTask {
 			return;
 		}
 
+		$ns = Configure::read('App.namespace');
+		$pluginPath = '';
+		if ($this->plugin) {
+			$ns = $this->plugin;
+			$pluginPath = $this->plugin . '.';
+		}
+
 		$name = $model->alias();
 		$data += [
+			'plugin' => $this->plugin,
+			'pluginPath' => $pluginPath,
+			'namespace' => $ns,
 			'name' => $name,
 			'associations' => [],
 			'primaryKey' => 'id',
@@ -549,17 +564,7 @@ class ModelTask extends BakeTask {
 			'behaviors' => [],
 		];
 
-		$pluginPath = '';
-		if ($this->plugin) {
-			$pluginPath = $this->plugin . '.';
-		}
-
 		$this->Template->set($data);
-		$this->Template->set([
-			'appNamespace' => Configure::read('App.namespace'),
-			'plugin' => $this->plugin,
-			'pluginPath' => $pluginPath
-		]);
 		$out = $this->Template->generate('classes', 'table');
 
 		$path = $this->getPath();

+ 22 - 0
src/Console/Templates/default/classes/entity.ctp

@@ -0,0 +1,22 @@
+<?= "<?php\n"; ?>
+namespace <?= $namespace ?>\Model\Entity;
+
+use Cake\ORM\Entity;
+
+/**
+ * <?= $name ?> Entity.
+ */
+class <?= $name ?> extends Entity {
+<?php if (!empty($fields)): ?>
+<?php
+$fields = array_map(function($el) { return "'$el'"; }, $fields);
+?>
+/**
+ * Fields that can be mass assigned using newEntity() or patchEntity().
+ *
+ * @var array
+ */
+	protected $_accessible = [<?= implode(', ', $fields) ?>];
+
+<?php endif ?>
+}

+ 1 - 1
src/Console/Templates/default/classes/table.ctp

@@ -20,7 +20,7 @@ use Cake\Utility\Inflector;
 
 echo "<?php\n";
 ?>
-namespace <?= $appNamespace ?>\Model\Table;
+namespace <?= $namespace ?>\Model\Table;
 
 use Cake\ORM\Table;
 <?php if (!empty($validation)): ?>

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

@@ -461,6 +461,9 @@ class ModelTaskTest extends TestCase {
 		$model = TableRegistry::get('BakeArticles');
 		$result = $this->Task->bakeTable($model, compact('validation'));
 
+		$this->assertContains('namespace App\Model\Table;', $result);
+		$this->assertContains('use Cake\ORM\Table;', $result);
+		$this->assertContains('use Cake\Validation\Validator;', $result);
 		$this->assertContains('class BakeArticlesTable extends Table {', $result);
 		$this->assertContains('public function validationDefault(Validator $validator) {', $result);
 		$this->assertContains("->add('name', 'valid', ['rule' => 'notEmpty'])", $result);
@@ -488,6 +491,7 @@ class ModelTaskTest extends TestCase {
 		$this->assertContains("this->displayField('title');\n", $result);
 		$this->assertContains("this->addBehavior('Timestamp');\n", $result);
 		$this->assertContains("this->table('articles');\n", $result);
+		$this->assertNotContains('use Cake\Validation\Validator;', $result);
 	}
 
 /**
@@ -536,6 +540,39 @@ class ModelTaskTest extends TestCase {
 	}
 
 /**
+ * test baking an entity class
+ *
+ * @return void
+ */
+	public function testBakeEntity() {
+		$config = [
+			'fields' => []
+		];
+		$model = TableRegistry::get('BakeArticles');
+		$result = $this->Task->bakeEntity($model, $config);
+		$this->assertContains('namespace App\Model\Entity;', $result);
+		$this->assertContains('use Cake\ORM\Entity;', $result);
+		$this->assertContains('class BakeArticle extends Entity {', $result);
+		$this->assertNotContains('$_accessible', $result);
+	}
+
+/**
+ * test baking an entity class
+ *
+ * @return void
+ */
+	public function testBakeEntityFields() {
+		$config = [
+			'fields' => ['title', 'body', 'published']
+		];
+		$model = TableRegistry::get('BakeArticles');
+		$result = $this->Task->bakeEntity($model, $config);
+		$this->assertContains("protected \$_accessible = ['title', 'body', 'published']", $result);
+	}
+
+
+
+/**
  * test bake() with a -plugin param
  *
  * @return void