TechFanDan
TechFanDan

Reputation: 3478

Get behavior related configuration from code

Are behavior related configurations accessible? In this specific case, a behavior was attached to a table. I would like to know if it's possible to get the fields property in some way, later on in code?

<?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',
                ]
        ]);
    }
    ...
}
?>

If I load the table and dump the content, I do not see the behavior listed:

$table = TableRegistry::get('MyRandomTable');
var_dump($table);

Partial dump content:

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)

What I'd like to do, in a controller, is to get the fields and pass those on to a view.

Edit #1

Using CakePHP v3.3.16

Edit #2

I'm seeing behavior information as I had missed the plugin prefix when loading the table:

$table = TableRegistry::get('PluginName.MyRandomTable');

Shows:

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)

Upvotes: 1

Views: 265

Answers (2)

ndm
ndm

Reputation: 60463

First of all your table class file is incorrect, it needs a namespace, otherwise it cannot be found, and you'll end up with an instance of \Cake\ORM\Table (a so called auto/generic-table) instead of concrete subclass thereof, hence your behavior is missing.

That being said, it depends on how the behavior has been programmed. If it follows the default configuration pattern, then you can access the configuration via its config() or getConfig() (as of CakePHP 3.4) methods.

Of course you have to access the behavior, not just the table class to which it is attached. This is done using the behavior registry, which is available via the Table::behaviors() method:

$fields = $table->behaviors()->get('Cipher')->config('fields');

See also

Upvotes: 2

shubham715
shubham715

Reputation: 3302

You can get Column names of a table by schema()->columns().

Example -

$getColumnArray = $this->Users->schema()->columns();//return Users Table Colums Name Array
$getColumnArray = $this->Users->associations()->keys()//return Users assocation table key

Upvotes: 0

Related Questions