1. Go to this page and download the library: Download cakesuit/metatable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cakesuit / metatable example snippets
// ...
class UsersTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('username');
$this->setPrimaryKey('id');
$this->hasMany('MetaUsers', [
'foreignKey' => 'user_id',
]);
// Add behavior
$this->addBehavior('Cakesuit/MetaTable.Meta', [
/**
* Define meta table name
* Required: true
* Default: null
*/
'metaTableName' => 'MetaUsers',
/**
* Define the name for return de meta
* Required: false
* Default: meta
*/
'propertyName' => 'meta',
/**
* Define de key column
* Required: false
* Default: meta_key
*/
'keyField' => 'meta_key',
/**
* Default value
* Require: false
* Default Table::getDisplayField()
*/
'valueField' => 'meta_value',
/**
* Method for save meta
* Required: false
* Default: 'both'
* false: insert into meta
* true: insert into object entities
* both: meta & object entities
*/
'addProperties' => 'both',
]);
}
}
$usersTable = \Cake\ORM\TableRegistry::get('Users');
$user = $usersTable->get(1, [
'contain' => ['MetaUsers']
]);
echo $user->username; // Cakesuit
// With addProperties (true) in behavior config
echo $user->age; // 26
echo $user->sexe; // male
// Without addProperties (false) in behavior config
echo $user->meta->age; // 26
echo $user->meta->sexe; // male
// Get age value
echo $user->meta->get('age'); // 26
// Fetch the entity ID : fetch($key, default)
echo $user->meta->fetch('age'); // 26
echo $user->meta->fetch('age.id', null); // 1
// Check has ID : has($key)
echo $user->meta->has('sexe'); // true
echo $user->meta->has('sexe.id'); // true
echo $user->meta->has('sexe.other'); // false
// Check if empty value : isEmpty($key)
echo $user->meta->isEmpty('sexe'); // false
echo $user->meta->isEmpty('sexe.meta_value'); // false
echo $user->meta->isEmpty('other'); // true
// Check equal value : equalTo($expected, $key, strict = false)
echo $user->meta->equalTo('male', 'sexe', false); // true
echo $user->meta->equalTo(1, 'age.id', true); // true
if ($user->meta->equalTo('26', 'age', true)) {
// Return Cakesuit is 26 years old
echo sprintf(
'%s is %d years old',
$user->username,
$user->age // or $user->meta->gat('age')
);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.