本文整理汇总了PHP中Cake\ORM\Entity::dirty方法的典型用法代码示例。如果您正苦于以下问题:PHP Entity::dirty方法的具体用法?PHP Entity::dirty怎么用?PHP Entity::dirty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Entity
的用法示例。
在下文中一共展示了Entity::dirty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: beforeSave
/**
* Modifies the entity before it is saved so that uploaded file data is persisted
* in the database too.
*
* @param \Cake\Event\Event $event The beforeSave event that was fired
* @param \Cake\ORM\Entity $entity The entity that is going to be saved
* @param \ArrayObject $options the options passed to the save method
* @return void|false
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
foreach ($this->config() as $field => $settings) {
if (Hash::get((array) $entity->get($field), 'error') !== UPLOAD_ERR_OK) {
if (Hash::get($settings, 'restoreValueOnFailure', true)) {
$entity->set($field, $entity->getOriginal($field));
$entity->dirty($field, false);
}
continue;
}
$data = $entity->get($field);
$path = $this->getPathProcessor($entity, $data, $field, $settings);
$basepath = $path->basepath();
$filename = $path->filename();
$data['name'] = $filename;
$files = $this->constructFiles($entity, $data, $field, $settings, $basepath);
$writer = $this->getWriter($entity, $data, $field, $settings);
$success = $writer->write($files);
if ((new Collection($success))->contains(false)) {
return false;
}
$entity->set($field, $filename);
$entity->set(Hash::get($settings, 'fields.dir', 'dir'), $basepath);
$entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']);
$entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']);
}
}
示例2: beforeSave
/**
* save
*/
public function beforeSave(Event $event, Entity $entity)
{
$config = $this->config();
$fields = $config['fields'];
foreach ($fields as $key => $value) {
$historic[$value] = $entity->get($value);
}
if (!$entity->isNew() && ($historicOld = $this->_table->Historics->find()->where([$this->foreignKey => $entity->id])->toArray())) {
$this->_table->Historics->patchEntity($historicOld[0], ['is_active' => 0]);
$entity->set('historics', [$this->_table->Historics->newEntity($historic), $historicOld[0]]);
} else {
$entity->set('historics', [$this->_table->Historics->newEntity($historic)]);
}
$entity->dirty('historics', true);
}
示例3: beforeSave
/**
* Injects configured field values into entity if those fields are not dirty.
*
* @param \Cake\Event\Event $event Event.
* @param \Cake\ORM\Entity $entity Entity.
* @param \ArrayObject $options Options.
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$eventName = $event->name();
$events = $this->config('events');
$new = $entity->isNew() !== false;
foreach ($events[$eventName] as $field => $when) {
if (!in_array($when, ['always', 'new', 'existing'])) {
throw new UnexpectedValueException(sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when));
}
if ($entity->dirty($field)) {
continue;
}
if ($when === 'always' || $when === 'new' && $new || $when === 'existing' && !$new) {
$entity->set($field, current(Hash::extract((array) $options, $this->config('propertiesMap.' . $field))));
}
}
}
示例4: beforeSave
/**
* This event is fired before each entity is saved
*
* @param \Cake\Event\Event $event The event data
* @param \Cake\Datasource\EntityInterface $entity The entity being saved
* @return void
*/
public function beforeSave(Event $event, Entity $entity)
{
$config = $this->config();
foreach (array_keys($config) as $field) {
if (!$entity->dirty($field)) {
continue;
}
$newPos = $entity->{$field};
$oldEntity = $this->getOldEntity($entity, $field);
if (is_null($oldEntity)) {
// Adding new entity
$oldPos = null;
} else {
$oldPos = $oldEntity->{$field};
}
list($conditions, $expression) = $this->getQueryData($oldPos, $newPos, $field);
$this->reorder($conditions, $expression);
}
}
示例5: afterSave
/**
* Save the before and after state of the modified entity to the Revisions table.
*
* @param Event $event [description]
* @param Entity $entity [description]
* @return void
*/
public function afterSave(Event $event, Entity $entity)
{
# get the current configuration
$config = $this->config();
# pessimistic - expect not to have to update anything
$trigger = false;
# if watch is set, use it
switch (true) {
# if `watch` is set AND one of the fields we're watching has been changed, trigger a save
case !empty($config['watch']):
$trigger = !empty($entity->extractOriginalChanged($config['watch']));
break;
# if `ignore` is set AND at least one other non-ignored field was changed, trigger a save
# if `ignore` is set AND at least one other non-ignored field was changed, trigger a save
case !empty($config['ignore']):
$trigger = !empty(array_diff($entity->extractOriginalChanged($this->_table->schema()->columns()), $entity->extractOriginalChanged($config['ignore'])));
break;
# if SOMETHING changed, and we're not explicity watching or ignoring anything, trigger anyway
# if SOMETHING changed, and we're not explicity watching or ignoring anything, trigger anyway
default:
$trigger = $entity->dirty();
break;
}
# if we don't need to trigger a save, stop
if (!$trigger) {
return;
}
# rebuild the original entity
$before = $this->_table->patchEntity($before = clone $entity, $before->extractOriginal($this->_table->schema()->columns()));
# load the Revisions Model
$this->Revisions = TableRegistry::get('Revisions.Revisions');
# build the Revision record
$r = $this->Revisions->newEntity(['model' => $this->_table->table(), 'modelPrimaryKey' => $entity->get($this->_table->primaryKey()), 'before_edit' => json_encode($before), 'after_edit' => json_encode($entity)]);
# and save it
$this->Revisions->save($r);
}
示例6: beforeSave
/**
* beforeSave callback
*
* @param \Cake\Event\Event $event Event.
* @param \Cake\ORM\Entity $entity The Entity.
* @param array $options Options.
* @return void
*/
public function beforeSave($event, $entity, $options)
{
$uploads = [];
$fields = $this->getFieldList();
foreach ($fields as $field => $data) {
if (!is_string($entity->get($field))) {
$uploads[$field] = $entity->get($field);
$entity->set($field, null);
}
if (!$entity->isNew()) {
$dirtyField = $entity->dirty($field);
$originalField = $entity->getOriginal($field);
if ($dirtyField && !is_null($originalField) && !is_array($originalField)) {
$fieldConfig = $this->config($field);
if ($fieldConfig['removeFileOnUpdate']) {
$this->_removeFile($entity->getOriginal($field));
}
}
}
}
$this->_uploads = $uploads;
}
示例7: beforeSave
/**
* Callback for Model.beforeSave event.
*
* @param \Cake\Event\Event $event The afterSave event that was fired.
* @param \Cake\ORM\Entity $entity The entity that was saved.
* @param \ArrayObject $options Options.
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$config = $this->_config;
if (!$entity->isNew() && !$config['onUpdate']) {
return;
}
if ($entity->dirty($config['field']) && (!$entity->isNew() || !empty($entity->{$config['field']}))) {
return;
}
$fields = (array) $config['displayField'];
$parts = [];
foreach ($fields as $field) {
$value = Hash::get($entity, $field);
if ($value === null && !$entity->isNew()) {
return;
}
if (!empty($value) || is_numeric($value)) {
$parts[] = $value;
}
}
$slug = $this->slug($entity, implode($config['separator'], $parts), $config['separator']);
$entity->set($config['field'], $slug);
}
示例8: _ensureFields
/**
* Ensures that the provided entity contains non-empty values for the left and
* right fields
*
* @param \Cake\ORM\Entity $entity The entity to ensure fields for
* @return void
*/
protected function _ensureFields($entity)
{
$config = $this->config();
$fields = [$config['left'], $config['right']];
$values = array_filter($entity->extract($fields));
if (count($values) === count($fields)) {
return;
}
$fresh = $this->_table->get($entity->get($this->_getPrimaryKey()), $fields);
$entity->set($fresh->extract($fields), ['guard' => false]);
foreach ($fields as $field) {
$entity->dirty($field, false);
}
}
示例9: beforeSave
/**
* Implementation of the beforesave event, handles uploading / saving and overwriting of image records
* @param \Cake\Event\Event $event [description]
* @param \Cake\ORM\Entity $entity [description]
* @param ArrayObject $options [description]
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$fields = $this->config('fields');
$alias = $this->_table->registryAlias();
$newOptions = [$this->_imagesTable->alias() => ['validate' => false]];
$options['associated'] = $newOptions + $options['associated'];
$entities = [];
foreach ($fields as $_fieldName => $fieldType) {
$uploadedImages = [];
$field = $entity->{$_fieldName};
$field = $fieldType == 'one' ? [$field] : $field;
if (!$field) {
continue;
}
foreach ($field as $index => $image) {
$uploadeImage = null;
if (!empty($image['tmp_name'])) {
// server based file uploads
$uploadeImage = $this->_upload($image['name'], $image['tmp_name'], false);
} elseif (is_string($image)) {
// any other 'path' based uploads
$uploadeImage = $this->_upload($image, $image, true);
}
if (!empty($uploadeImage)) {
$uploadedImages[$index] = $uploadeImage + ['field_index' => $index, 'model' => $alias, 'field' => $_fieldName];
}
}
if (!empty($uploadedImages)) {
if (!$entity->isNew()) {
$imagesTableAlias = $this->_imagesTable->alias();
$preexisting = $this->_imagesTable->find()->where(['model' => $alias, 'field' => $_fieldName, 'foreign_key' => $entity->{$this->_table->primaryKey()}])->order(['field_index' => 'ASC']);
foreach ($preexisting as $image) {
if (isset($uploadedImages[$image->field_index])) {
$entities[$image->field_index] = $this->_imagesTable->patchEntity($image, $uploadedImages[$image->field_index]);
} elseif ($fieldType == 'one') {
$this->_imagesTable->delete($image);
}
}
}
$new = array_diff_key($uploadedImages, $entities);
foreach ($new as $image) {
$entities[] = $this->_imagesTable->newEntity($image);
}
}
$entity->dirty($_fieldName, false);
}
$entity->set('_images', $entities);
}
示例10: _getOldValues
/**
* Get old order and scope values.
*
* @param \Cake\ORM\Entity $entity Entity.
*
* @return array
*/
protected function _getOldValues(Entity $entity)
{
$config = $this->config();
$fields = array_merge($config['scope'], [$config['order']]);
$values = [];
foreach ($fields as $field) {
if ($entity->dirty($field)) {
$values[$field] = $entity->getOriginal($field);
} elseif ($entity->has($field)) {
$values[$field] = $entity->get($field);
}
}
if (count($fields) != count($values)) {
$primaryKey = $entity->get($this->_table->primaryKey());
$values = $this->_table->get($primaryKey, ['fields' => $fields, 'limit' => 1])->toArray();
}
$order = $values[$config['order']];
unset($values[$config['order']]);
return [$order, $values];
}
示例11: _save
/**
* Helper method for saving an association's data.
*
* @param Association $association The association object to save with.
* @param Entity $entity The entity to save
* @param array $nested Options for deeper associations
* @param array $options Original options
* @return bool Success
*/
protected function _save($association, $entity, $nested, $options)
{
if (!$entity->dirty($association->property())) {
return true;
}
if (!empty($nested)) {
$options = (array) $nested + $options;
}
return (bool) $association->save($entity, $options);
}
示例12: beforeSave
/**
* Vlastní implementace beforeSave z pluginu
*
* @param Event $event
* @param Entity $entity
* @param ArrayObject $options
*
* @return bool
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$fields = $this->config('fields');
$alias = $this->_table->registryAlias();
$newOptions = [$this->_imagesTable->alias() => ['validate' => false]];
$options['associated'] = $newOptions + $options['associated'];
$entities = [];
foreach ($fields as $_fieldName => $fieldType) {
$uploadedImages = [];
$field = $entity->{$_fieldName};
$field = $fieldType == 'one' ? [$field] : $field;
if (isset($field['id'])) {
//Úprava existujícího obrázku
$field = array_filter($field, 'strlen');
$image = $this->_imagesTable->get($field['id']);
$image->modified = Time::now();
$entities[] = $this->_imagesTable->patchEntity($image, $field);
} else {
if ($field !== null) {
//Nativní chování, podle toho jestli existuje field index
foreach ($field as $index => $image) {
$uploadedImage = null;
if (!empty($image['tmp_name'])) {
// server based file uploads
$uploadedImage = $this->_upload($image['name'], $image['tmp_name'], false);
} elseif (is_string($image)) {
// any other 'path' based uploads
$uploadedImage = $this->_upload($image, $image, true);
}
if (!empty($uploadedImage)) {
$uploadedImages[$index] = $uploadedImage + ['field_index' => $index, 'model' => $alias, 'field' => $_fieldName, 'modified' => Time::now()];
if (isset($field['extra_data']) && is_array($field['extra_data'])) {
$uploadedImages[$index] = array_merge($field['extra_data'], $uploadedImages[$index]);
}
}
}
if (!empty($uploadedImages)) {
if ($this->config('pile')) {
//Pokud se mají obrázky nakládat místo přepisování TODO saveStrategy v modelu?
$query = $this->_imagesTable->find();
$maxFieldIndex = $query->select(['field_index' => $query->func()->max('field_index')])->first()->field_index;
foreach ($uploadedImages as $image) {
$image['field_index'] = ++$maxFieldIndex;
$image['created'] = Time::now();
$entities[] = $this->_imagesTable->newEntity($image);
}
} else {
if (!$entity->isNew()) {
$preexisting = $this->_imagesTable->find()->where(['model' => $alias, 'field' => $_fieldName, 'foreign_key' => $entity->{$this->_table->primaryKey()}])->order(['field_index' => 'ASC']);
foreach ($preexisting as $image) {
if (isset($uploadedImages[$image->field_index])) {
$entities[$image->field_index] = $this->_imagesTable->patchEntity($image, $uploadedImages[$image->field_index]);
} elseif ($fieldType == 'one') {
$this->_imagesTable->delete($image);
}
}
}
$new = array_diff_key($uploadedImages, $entities);
foreach ($new as $image) {
$image['created'] = Time::now();
$entities[] = $this->_imagesTable->newEntity($image);
}
}
}
$entity->dirty($_fieldName, false);
}
}
}
if (count($entities)) {
$entity->set('_images', $entities);
return true;
} else {
if (count($_FILES)) {
//nejsou entity ale jsou data
return false;
} else {
return true;
}
}
}
示例13: needsSlugUpdate
/**
* Method to find out if the current slug needs updating.
*
* The deep option is useful if you cannot rely on dirty() because
* of maybe some not in sync slugs anymore (saving the same title again,
* but the slug is completely different, for example).
*
* @param \Cake\ORM\Entity $entity
* @param bool $deep If true it will generate a new slug and compare it to the currently stored one.
* @return bool
*/
public function needsSlugUpdate($entity, $deep = false)
{
foreach ((array) $this->_config['label'] as $label) {
if ($entity->dirty($label)) {
return true;
}
}
if ($deep) {
$copy = clone $entity;
$this->slug($copy, ['overwrite' => true]);
return $copy->get($this->_config['field']) !== $entity->get($this->_config['field']);
}
return false;
}
示例14: updateField
/**
* Update a field, if it hasn't been updated already
*
* @param \Cake\ORM\Entity $entity Entity instance.
* @param string $field Field name
* @return void
*/
protected function updateField(Entity $entity, $field)
{
if ($entity->dirty($field)) {
return;
}
$entity->set($field, $this->getUserId());
}
示例15: _updateField
/**
* Update a field, if it hasn't been updated already
*
* @param \Cake\ORM\Entity $entity Entity instance.
* @param string $field Field name
* @param bool $refreshTimestamp Whether to refresh timestamp.
* @return void
*/
protected function _updateField(Entity $entity, $field, $refreshTimestamp)
{
if ($entity->dirty($field)) {
return;
}
if (substr($field, -3) == '_by') {
$user_id = $this->Auth->User('id');
$entity->set($field, $user_id);
} else {
$entity->set($field, $this->timestamp(null, $refreshTimestamp));
}
}