本文整理汇总了PHP中Cake\ORM\Table::patchEntity方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::patchEntity方法的具体用法?PHP Table::patchEntity怎么用?PHP Table::patchEntity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Table
的用法示例。
在下文中一共展示了Table::patchEntity方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: entityGenerator
/**
* Helper generator for use with importTable().
*
* Yields a single new Entity instance approciate for $Table for each
* of $records where the values are merged with $defaults.
*
* Will skip any records that fail to validate, dumping validation
* errors to the console in the process.
*
* Used by imporTables().
*
* @param Cake\ORM\Table $Table A Table instance to save records into.
* @param array $records An array of Entity records to save into the Table.
* @param array $defaults Optional array of default field values to merge into each record.
* @param array $options Optional array of newEntity() options to use.
* @return void
*/
public function entityGenerator(Table $Table, array $records, array $defaults = [], array $options = [])
{
$defaultOptions = ['validate' => true, 'accessibleFields' => ['*' => true]];
$options = $options + $defaultOptions;
$keyField = $Table->primaryKey();
foreach ($records as $r) {
$r = Hash::merge($defaults, $r);
$id = !empty($r[$keyField]) ? $r[$keyField] : false;
if ($id) {
$entity = $Table->find()->where([$keyField => $id])->first();
if ($entity) {
$entity = $Table->patchEntity($entity, $r, $options);
if (!$entity->dirty()) {
$this->verbose("<success>{$Table->alias()} ({$id}): No changes.</success>");
continue;
}
} else {
$entity = $Table->newEntity($r, $options);
$entity->isNew(true);
}
} else {
$entity = $Table->newEntity($r, $options);
}
$errors = $entity->errors();
if ($errors) {
$this->printValidationErrors($Table->alias(), $id, $errors);
continue;
}
(yield $entity);
}
}
示例2: patchEntity
/**
* {@inheritDoc}
*/
public function patchEntity(EntityInterface $type, array $data, array $options = [])
{
$return = parent::patchEntity($type, $data);
if (!empty($data['permissions'])) {
$roles = [];
foreach ($data['permissions'] as $rule => $ids) {
foreach ($ids as $roleId) {
if (!empty($roleId)) {
$role = $this->Roles->get($roleId);
$role->set('_joinData', $this->ContentTypePermissions->newEntity(['action' => $rule]));
$roles[] = $role;
}
}
}
$return->set('permissions', $roles);
}
return $return;
}
示例3: changePassword
/**
* Let the logged in user change his password.
*
* @param array $options
* @return void
*/
public function changePassword($options = [])
{
$options = Hash::merge($this->_defaultConfig['changePassword'], $options);
$entity = $this->UserTable->newEntity();
$entity->accessible(['id', 'old_password', 'new_password', 'confirm_password'], true);
if ($this->request->is(['post', 'put'])) {
$entity = $this->UserTable->patchEntity($entity, $this->request->data);
$entity->id = $this->_controller->Auth->user('id');
$entity->isNew(false);
if ($this->UserTable->changePassword($entity)) {
$this->request->data = [];
$entity = $this->UserTable->newEntity();
$entity->id = $this->_controller->Auth->user('id');
$entity->isNew(false);
$this->handleFlashAndRedirect('success', $options);
} else {
$this->handleFlashAndRedirect('error', $options);
}
}
$this->_controller->set('entity', $entity);
}