行为相关配置是否可访问?在此特定情况下,行为附加到表。我想知道是否有可能以某种方式获取fields属性,稍后在代码中?
<?php
class MyRandomTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
...
// Add Cipher behavior
$this->addBehavior('CipherBehavior.Cipher', [
'fields' => [
'original' => 'string',
'changed' => 'string',
]
]);
}
...
}
?>
如果我加载表并转储内容,我看不到列出的行为:
$table = TableRegistry::get('MyRandomTable');
var_dump($table);
部分转储内容:
protected '_behaviors' =>
object(Cake\ORM\BehaviorRegistry)[170]
protected '_table' =>
&object(Cake\ORM\Table)[172]
protected '_methodMap' =>
array (size=0)
empty
protected '_finderMap' =>
array (size=0)
empty
protected '_loaded' =>
array (size=0)
empty
protected '_eventManager' =>
object(Cake\Event\EventManager)[165]
protected '_listeners' =>
array (size=0)
...
protected '_isGlobal' => boolean false
protected '_eventList' => null
protected '_trackEvents' => boolean false
protected '_eventClass' => string '\Cake\Event\Event' (length=17)
我想在控制器中做的是获取字段并将其传递给视图。
编辑#1
使用CakePHP v3.3.16
编辑#2
我看到了行为信息,因为我在加载表时错过了插件前缀:
$table = TableRegistry::get('PluginName.MyRandomTable');
节目:
protected '_behaviors' =>
object(Cake\ORM\BehaviorRegistry)[143]
protected '_table' =>
&object(PluginName\Model\Table\MyRandomTable)[94]
protected '_methodMap' =>
array (size=4)
'timestamp' =>
array (size=2)
...
'touch' =>
array (size=2)
...
'encrypt' =>
array (size=2)
...
'decrypt' =>
array (size=2)
...
protected '_finderMap' =>
array (size=0)
empty
protected '_loaded' =>
array (size=2)
'Timestamp' =>
object(Cake\ORM\Behavior\TimestampBehavior)[181]
...
'Cipher' =>
object(CipherBehavior\Model\Behavior\CipherBehavior)[192]
...
protected '_eventManager' =>
object(Cake\Event\EventManager)[175]
protected '_listeners' =>
array (size=4)
...
protected '_isGlobal' => boolean false
protected '_eventList' => null
protected '_trackEvents' => boolean false
protected '_eventClass' => string '\Cake\Event\Event' (length=17)
答案 0 :(得分:2)
首先,您的表类文件不正确,它需要一个命名空间,否则无法找到它,并且您最终会得到一个\Cake\ORM\Table
实例(一个所谓的auto / generic-table) )而不是其具体的子类,因此你的行为缺失。
话虽如此,这取决于行为的编程方式。如果它遵循默认配置模式,那么您可以通过其config()
或getConfig()
(从CakePHP 3.4开始)方法访问配置。
当然,您必须访问行为,而不仅仅是它所附加的表类。这是使用行为注册表完成的,该注册表可通过Table::behaviors()
方法获得:
$fields = $table->behaviors()->get('Cipher')->config('fields');
另见
答案 1 :(得分:0)
您可以通过schema()->columns()
获取表格的列名。
示例 -
$getColumnArray = $this->Users->schema()->columns();//return Users Table Colums Name Array
$getColumnArray = $this->Users->associations()->keys()//return Users assocation table key