Browse Source

Save table instance reference as behavior property.

Many userland behaviors too would need a table reference so better
to save it in core Behavior's constructor itself and avoid overriding constructor.
ADmad 11 years ago
parent
commit
06b0e1398f

+ 0 - 19
src/Model/Behavior/CounterCacheBehavior.php

@@ -80,25 +80,6 @@ use Cake\ORM\Table;
 class CounterCacheBehavior extends Behavior {
 
 /**
- * Keeping a reference to the table in order to,
- * be able to retrieve associations and fetch records for counting.
- *
- * @var \Cake\ORM\Table
- */
-	protected $_table;
-
-/**
- * Constructor
- *
- * @param \Cake\ORM\Table $table The table this behavior is attached to.
- * @param array $config The config for this behavior.
- */
-	public function __construct(Table $table, array $config = []) {
-		parent::__construct($table, $config);
-		$this->_table = $table;
-	}
-
-/**
  * afterSave callback.
  *
  * Makes sure to update counter cache when a new record is created or updated.

+ 0 - 8
src/Model/Behavior/TranslateBehavior.php

@@ -39,13 +39,6 @@ use Cake\ORM\TableRegistry;
 class TranslateBehavior extends Behavior {
 
 /**
- * Table instance
- *
- * @var \Cake\ORM\Table
- */
-	protected $_table;
-
-/**
  * The locale name that will be used to override fields in the bound table
  * from the translations table
  *
@@ -78,7 +71,6 @@ class TranslateBehavior extends Behavior {
 		$config += ['defaultLocale' => I18n::defaultLocale()];
 		parent::__construct($table, $config);
 
-		$this->_table = $table;
 		$config = $this->_config;
 		$this->setupFieldAssociations($config['fields'], $config['translationTable']);
 	}

+ 0 - 18
src/Model/Behavior/TreeBehavior.php

@@ -36,13 +36,6 @@ use Cake\ORM\Table;
 class TreeBehavior extends Behavior {
 
 /**
- * Table instance
- *
- * @var \Cake\ORM\Table
- */
-	protected $_table;
-
-/**
  * Cached copy of the first column in a table's primary key.
  *
  * @var string
@@ -76,17 +69,6 @@ class TreeBehavior extends Behavior {
 	];
 
 /**
- * Constructor
- *
- * @param \Cake\ORM\Table $table The table this behavior is attached to.
- * @param array $config The config for this behavior.
- */
-	public function __construct(Table $table, array $config = []) {
-		parent::__construct($table, $config);
-		$this->_table = $table;
-	}
-
-/**
  * Before save listener.
  * Transparently manages setting the lft and rght fields if the parent field is
  * included in the parameters to be saved.

+ 8 - 0
src/ORM/Behavior.php

@@ -104,6 +104,13 @@ class Behavior implements EventListenerInterface {
 	use InstanceConfigTrait;
 
 /**
+ * Table instance.
+ *
+ * @var \Cake\ORM\Table
+ */
+	protected $_table;
+
+/**
  * Reflection method cache for behaviors.
  *
  * Stores the reflected method + finder methods per class.
@@ -145,6 +152,7 @@ class Behavior implements EventListenerInterface {
 			$config
 		);
 		$this->config($config);
+		$this->_table = $table;
 	}
 
 /**