本文整理汇总了PHP中Cake\ORM\Entity::set方法的典型用法代码示例。如果您正苦于以下问题:PHP Entity::set方法的具体用法?PHP Entity::set怎么用?PHP Entity::set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Entity
的用法示例。
在下文中一共展示了Entity::set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: uploadFile
/**
* get appropriate fields with files
* move uploaded file and set path to entity field, but only if a file was selected
*
* @param Entity $entity
*/
public function uploadFile(Entity $entity)
{
$config = $this->config();
if (!is_array($config['field'])) {
$field = $entity->get($config['field']);
if (empty($field['tmp_name'])) {
$entity->unsetProperty($config['field']);
} else {
if ($entity->get($config['field']) != $entity->getOriginal($config['field'])) {
$originalFilePath = $entity->getOriginal($config['field']);
$this->_delete($originalFilePath);
}
$filePath = $this->_moveFile($field);
$entity->set($config['field'], $filePath);
}
} else {
foreach ($config['field'] as $value) {
$field = $entity->get($value);
if (empty($field['tmp_name'])) {
$entity->unsetProperty($config['field']);
} else {
if ($entity->get($config['field']) != $entity->getOriginal($config['field'])) {
$originalFilePath = $entity->getOriginal($config['field']);
$this->_delete($originalFilePath);
}
$filePath = $this->_moveFile($field);
$entity->set($config['field'], $filePath);
}
}
}
}
示例2: beforeSave
/**
* {@inheritDoc}
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
if (empty($options['loggedInUser'])) {
return;
}
if ($entity->isNew()) {
$entity->set('created_by', $options['loggedInUser']);
}
$entity->set('modified_by', $options['loggedInUser']);
}
示例3: 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);
}
示例4: 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']);
}
}
示例5: slug
public function slug(Entity $entity)
{
$config = $this->config();
if (!($value = $entity->get($config['slug']))) {
$value = $entity->get($config['field']);
}
$entity->set($config['slug'], Inflector::slug($value, $config['replacement']));
}
示例6: beforeSave
public function beforeSave(Event $event, Entity $entity)
{
$config = $this->config();
$value = $entity->get($config['field']);
$id = isset($entity->id) ? $entity->id : 0;
$slug = $this->createSLug(strtolower(Inflector::slug($value, $config['replacement'])), 0, $id);
$entity->set($config['slug'], $slug);
}
示例7: beforeSave
/**
* beforeSave event
*
* @param \Cake\Event\Event $event Event.
* @param \Cake\ORM\Entity $entity Entity.
* @param array $options Options.
* @return void
*/
public function beforeSave($event, $entity, $options)
{
$entity->set('cakeadmin', true);
$newPassword = $entity->get('new_password');
if (!empty($newPassword)) {
$entity->set('password', $entity->new_password);
// set for password-changes
}
}
示例8: beforeSave
/**
* Every save event triggers a new personal_key. If the entity is new then
* check the config if there needs to be an email verification.
*/
public function beforeSave(Event $event, Entity $entity)
{
$entity->set('personal_key');
if ($entity->isNew()) {
if (Configure::read('Users.send_email_verification')) {
$entity->emailVerification();
}
}
}
示例9: transformState
public function transformState(Entity $entity)
{
$states = $this->getStates();
$field = $this->config('field');
$state = $this->config('defaultState');
if (array_key_exists($entity->{$field}, $states)) {
$state = $entity->{$field};
}
$state = $states[$state];
$entity->set($this->config('field'), $state);
}
示例10: beforeSave
public function beforeSave(Event $event, Entity $entity)
{
// create short link if one does not yet exist
if (empty($entity->id)) {
$link = $this->makeShortLink();
$Jobs = TableRegistry::get('Jobs');
while ($Jobs->find('all')->where(['short_link' => $link])->count() > 0) {
$link = $this->makeShortLink();
}
$entity->set('short_link', $link);
}
}
示例11: afterSave
/**
* Before Saving the entity, slug it.
* @param Event $event
* @param Entity $entity
* @return void
*/
public function afterSave(Event $event, Entity $entity, $options)
{
$config = $this->config();
# load the config built by the instantiated behavior
$original = $entity->get($config['field']);
# manually store $original - wasn't working for some reason otherwise
$entity->set($config['field'], $this->_generateSlug($entity));
# set the slug
# if the slug is actually different than before - save it
if ($entity->dirty() && $original != $entity->get($config['field'])) {
$this->_table->save($entity);
}
}
示例12: beforeSave
/**
* Before save
* Transforma o valor de BRL para o formato SQL antes de salvar a entidade
* no banco de dados
*
* @param Event $event Instância do evento
* @param Entity $entity Instância da entidade a ser salva pelo ORM
* @return bool
* @access public
*/
public function beforeSave(Event $event, Entity $entity)
{
foreach ($this->_table->schema()->columns() as $campo) {
$valor = $entity->get($campo);
if (!empty($valor) && $this->_table->schema()->columnType($campo) === "float") {
if (!is_string($valor) || preg_match('/^[0-9]+(\\.[0-9]+)?$/', $valor)) {
continue;
}
$entity->set($campo, str_replace(['.', ','], ['', '.'], $valor));
}
}
return true;
}
示例13: beforeSave
/**
* Save also related model data
*
* @param \Cake\Event\Event
* @param \Cake\ORM\Entity;
* @return void
*/
public function beforeSave(Event $event, Entity $entity)
{
foreach ($this->config('fields') as $field => $mapped) {
list($mappedTable, $mappedField) = explode('.', $mapped);
if (!isset($this->_table->{$mappedTable}) || $this->_table->{$mappedTable}->isOwningSide($this->_table)) {
throw new Exception(sprintf('Incorrect definition of related data to persist for %s', $mapped));
}
// get related entity
$related = $this->_table->{$mappedTable}->get($entity->get($this->_table->{$mappedTable}->foreignKey()));
// set field value
$entity->set($field, $related->get($mappedField));
}
}
示例14: beforeSave
/**
* beforeSave method
*
* @param Event $event The event
* @param Entity $entity The entity
* @param ArrayObject $options Array of options
* @param ProfferPathInterface $path Inject an instance of ProfferPath
* @return true
* @throws Exception
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options, ProfferPathInterface $path = null)
{
foreach ($this->config() as $field => $settings) {
if ($entity->has($field) && is_array($entity->get($field)) && $entity->get($field)['error'] === UPLOAD_ERR_OK) {
// Allow path to be injected or set in config
if (!empty($settings['pathClass'])) {
$path = new $settings['pathClass']($this->_table, $entity, $field, $settings);
} elseif (!isset($path)) {
$path = new ProfferPath($this->_table, $entity, $field, $settings);
}
$event = new Event('Proffer.afterPath', $entity, ['path' => $path]);
$this->_table->eventManager()->dispatch($event);
if (!empty($event->result)) {
$path = $event->result;
}
$path->createPathFolder();
if ($this->moveUploadedFile($entity->get($field)['tmp_name'], $path->fullPath())) {
$entity->set($field, $path->getFilename());
$entity->set($settings['dir'], $path->getSeed());
// Only generate thumbnails for image uploads
if (getimagesize($path->fullPath()) !== false && isset($settings['thumbnailSizes'])) {
// Allow the transformation class to be injected
if (!empty($settings['transformClass'])) {
$imageTransform = new $settings['transformClass']($this->_table, $path);
} else {
$imageTransform = new ImageTransform($this->_table, $path);
}
$imageTransform->processThumbnails($settings);
}
} else {
throw new Exception('Cannot upload file');
}
}
unset($path);
}
return true;
}
示例15: matchUser
public function matchUser(Entity $entity)
{
if (!empty($entity->user_id)) {
return true;
}
$netId = $entity->net_id;
if (empty($netId)) {
return false;
}
$userId = TableRegistry::get('Users')->findOrCreateByNetId($netId);
if (empty($userId)) {
return false;
}
$entity->set('user_id', $userId);
return true;
}