本文整理匯總了PHP中Cake\Datasource\EntityInterface::isNew方法的典型用法代碼示例。如果您正苦於以下問題:PHP EntityInterface::isNew方法的具體用法?PHP EntityInterface::isNew怎麽用?PHP EntityInterface::isNew使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Cake\Datasource\EntityInterface
的用法示例。
在下文中一共展示了EntityInterface::isNew方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: _processDelete
/**
* Perform the delete operation.
*
* Will soft delete the entity provided. Will remove rows from any
* dependent associations, and clear out join tables for BelongsToMany associations.
*
* @param \Cake\DataSource\EntityInterface $entity The entity to soft delete.
* @param \ArrayObject $options The options for the delete.
* @throws \InvalidArgumentException if there are no primary key values of the
* passed entity
* @return bool success
*/
protected function _processDelete($entity, $options)
{
if ($entity->isNew()) {
return false;
}
$primaryKey = (array) $this->primaryKey();
if (!$entity->has($primaryKey)) {
$msg = 'Deleting requires all primary key values.';
throw new \InvalidArgumentException($msg);
}
if ($options['checkRules'] && !$this->checkRules($entity, RulesChecker::DELETE, $options)) {
return false;
}
$event = $this->dispatchEvent('Model.beforeDelete', ['entity' => $entity, 'options' => $options]);
if ($event->isStopped()) {
return $event->result;
}
$this->_associations->cascadeDelete($entity, ['_primary' => false] + $options->getArrayCopy());
$query = $this->query();
$conditions = (array) $entity->extract($primaryKey);
$statement = $query->update()->set([$this->getSoftDeleteField() => 0])->where($conditions)->execute();
$success = $statement->rowCount() > 0;
if (!$success) {
return $success;
}
$this->dispatchEvent('Model.afterDelete', ['entity' => $entity, 'options' => $options]);
return $success;
}
示例2: beforeSave
public function beforeSave(Event $event, EntityInterface $entity)
{
if ($entity === null) {
return true;
}
$isNew = $entity->isNew();
$fields = $this->config('fields');
$ip = self::$_request->clientIp();
foreach ($fields as $field => $when) {
$when = strtolower($when);
if (!in_array($when, ['always', 'new'])) {
throw new UnexpectedValueException(sprintf('"When" should be one of "always", "new". The passed value "%s" is invalid', $when));
}
switch ($when) {
case 'always':
$entity->set($field, $ip);
continue;
break;
case 'new':
if ($isNew) {
$entity->set($field, $ip);
continue;
}
break;
}
}
return true;
}
示例3: beforeSave
public function beforeSave(Event $event, EntityInterface $entity)
{
$config = $this->config();
$new = $entity->isNew();
if ($config['when'] === 'always' || $config['when'] === 'new' && $new || $config['when'] === 'existing' && !$new) {
$this->slug($entity);
}
}
示例4: afterSave
/**
* afterSave callback
*
* Does not call the parent to avoid that the regular file storage event listener saves the image already
*
* @param \Cake\Event\Event $event
* @param \Cake\Datasource\EntityInterface $entity
* @param array $options
* @return boolean
*/
public function afterSave(Event $event, EntityInterface $entity, $options)
{
if ($entity->isNew()) {
$this->dispatchEvent('ImageStorage.afterSave', ['record' => $entity, 'storage' => $this->storageAdapter($entity->get('adapter'))]);
$this->deleteOldFileOnSave($entity);
}
return true;
}
示例5: beforeSave
/**
* Generates IDs for an entity before it is saved to the database.
*
* @param Event $event Instance of save event
* @param EntityInterface $entity Entity being saved
*/
public function beforeSave(Event $event, EntityInterface $entity)
{
// Check if entity is being created in database
// If so, update appropriate ID fields if present
if ($entity->isNew()) {
$entity->set($this->config('base64.field'), $this->generateBase64Id());
$entity->set($this->config('uuid.field'), $this->generateUuid());
}
}
示例6: afterSave
/**
* afterSave Callback
*
* @param Event $event CakePHP Event
* @param EntityInterface $entity Entity that was saved
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity)
{
$action = $entity->isNew() ? ModelHistory::ACTION_CREATE : ModelHistory::ACTION_UPDATE;
$dirtyFields = null;
if ($action === ModelHistory::ACTION_UPDATE && isset($this->_dirtyFields[$entity->id])) {
$dirtyFields = $this->_dirtyFields[$entity->id];
unset($this->_dirtyFields[$entity->id]);
}
$this->ModelHistory->add($entity, $action, $this->_getUserId(), ['dirtyFields' => $dirtyFields]);
}
示例7: afterSave
/**
* Save the file to the storage backend after the record was created.
*
* @param \Cake\Event\Event $event
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity)
{
if ($this->_checkEvent($event) && $entity->isNew()) {
$fileField = $this->config('fileField');
$entity['hash'] = $this->getFileHash($entity, $fileField);
$entity['path'] = $this->pathBuilder()->path($entity);
if (!$this->_storeFile($event)) {
return;
}
$event->stopPropagation();
}
}
示例8: __invoke
/**
* Performs the uniqueness check
*
* @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields
* @param array $options Options passed to the check,
* where the `repository` key is required.
* @return bool
*/
public function __invoke(EntityInterface $entity, array $options)
{
if (!$entity->extract($this->_fields, true)) {
return true;
}
$conditions = $entity->extract($this->_fields);
if ($entity->isNew() === false) {
$keys = (array) $options['repository']->primaryKey();
$keys = $entity->extract($keys);
if (array_filter($keys, 'strlen')) {
$conditions['NOT'] = $keys;
}
}
return !$options['repository']->exists($conditions);
}
示例9: handleEvent
/**
* There is only one event handler, it can be configured to be called for any event
*
* @param \Cake\Event\Event $event Event instance.
* @param \Cake\Datasource\EntityInterface $entity Entity instance.
* @throws \UnexpectedValueException if a field's when value is misdefined
* @return true (irrespective of the behavior logic, the save will not be prevented)
* @throws \UnexpectedValueException When the value for an event is not 'always', 'new' or 'existing'
*/
public function handleEvent(Event $event, EntityInterface $entity)
{
$eventName = $event->name();
$events = $this->_config['events'];
$new = $entity->isNew() !== false;
$refresh = $this->_config['refreshTimestamp'];
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 ($when === 'always' || $when === 'new' && $new || $when === 'existing' && !$new) {
$this->_updateField($entity, $field, $refresh);
}
}
return true;
}
示例10: afterSave
/**
* Save the file to the storage backend after the record was created.
*
* @param \Cake\Event\Event $event
* @param \Cake\Datasource\EntityInterface $entity
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity)
{
if ($this->_checkEvent($event) && $entity->isNew()) {
$fileField = $this->config('fileField');
$entity['hash'] = $this->getFileHash($entity, $fileField);
$entity['path'] = $this->pathBuilder()->fullPath($entity);
if (!$this->_storeFile($event)) {
return;
}
if ($this->_config['imageProcessing'] === true) {
$this->autoProcessImageVersions($entity, 'create');
}
$event->result = true;
$event->stopPropagation();
}
}
示例11: _modifyEntity
/**
* Modify entity
*
* @param \Cake\Datasource\EntityInterface entity
* @param \Cake\ORM\Association table
* @param string path prefix
* @return void
*/
protected function _modifyEntity(EntityInterface $entity, Association $table = null, $pathPrefix = '')
{
if (is_null($table)) {
$table = $this->_table;
}
// unset primary key
unset($entity->{$table->primaryKey()});
// unset foreign key
if ($table instanceof Association) {
unset($entity->{$table->foreignKey()});
}
// unset configured
foreach ($this->config('remove') as $field) {
$field = $this->_fieldByPath($field, $pathPrefix);
if ($field) {
unset($entity->{$field});
}
}
// set / prepend / append
foreach (['set', 'prepend', 'append'] as $action) {
foreach ($this->config($action) as $field => $value) {
$field = $this->_fieldByPath($field, $pathPrefix);
if ($field) {
if ($action == 'prepend') {
$value .= $entity->{$field};
}
if ($action == 'append') {
$value = $entity->{$field} . $value;
}
$entity->{$field} = $value;
}
}
}
// set as new
$entity->isNew(true);
// modify related entities
foreach ($this->config('contain') as $contain) {
if (preg_match('/^' . preg_quote($pathPrefix, '/') . '([^.]+)/', $contain, $matches)) {
foreach ($entity->{Inflector::tableize($matches[1])} as $related) {
if ($related->isNew()) {
continue;
}
$this->_modifyEntity($related, $table->{$matches[1]}, $pathPrefix . $matches[1] . '.');
}
}
}
}
示例12: afterSave
/**
* Regenerates snapshot after new content type is created.
*
* @param \Cake\Event\Event $event The event that was triggered
* @param \Cake\Datasource\EntityInterface $entity The entity that was saved
* @param \ArrayObject $options Array of options
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options = null)
{
if ($entity->isNew()) {
snapshot();
}
}
示例13: save
/**
* Persists an resource based on the fields that are marked as dirty and
* returns the same resource after a successful save or false in case
* of any error.
*
* @param \Cake\Datasource\EntityInterface $resource the resource to be saved
* @param array|\ArrayAccess $options The options to use when saving.
*
* @return \Cake\Datasource\EntityInterface|bool
*/
public function save(EntityInterface $resource, $options = [])
{
$options = new ArrayObject($options + ['checkRules' => true]);
$mode = $resource->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
if ($options['checkRules'] && !$this->checkRules($resource, $mode, $options)) {
return false;
}
$data = $resource->extract($this->schema()->columns(), true);
if ($resource->isNew()) {
$query = $this->query()->create();
} else {
$query = $this->query()->update()->where([$this->primaryKey() => $resource->get($this->primaryKey())]);
}
$query->set($data);
$result = $query->execute();
if (!$result) {
return false;
}
if ($resource->isNew() && $result instanceof EntityInterface) {
return $result;
}
$className = get_class($resource);
return new $className($resource->toArray(), ['markNew' => false, 'markClean' => true]);
}
示例14: _checkPersistenceStatus
/**
* Throws an exception should any of the passed entities is not persisted.
*
* @param \Cake\Datasource\EntityInterface $sourceEntity the row belonging to the `source` side
* of this association
* @param array $targetEntities list of entities belonging to the `target` side
* of this association
* @return bool
* @throws \InvalidArgumentException
*/
protected function _checkPersistenceStatus($sourceEntity, array $targetEntities)
{
if ($sourceEntity->isNew()) {
$error = 'Source entity needs to be persisted before proceeding';
throw new InvalidArgumentException($error);
}
foreach ($targetEntities as $entity) {
if ($entity->isNew()) {
$error = 'Cannot link not persisted entities';
throw new InvalidArgumentException($error);
}
}
return true;
}
示例15: save
/**
* Persists an entity based on the fields that are marked as dirty and
* returns the same entity after a successful save or false in case
* of any error.
*
* Triggers the `Model.beforeSave` and `Model.afterSave` events.
*
* ## Options
*
* - `checkRules` Defaults to true. Check deletion rules before deleting the record.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to be saved
* @param array $options An array of options to be used for the event
* @return \Cake\Datasource\EntityInterface|bool
*/
public function save(EntityInterface $entity, $options = [])
{
$options += ['checkRules' => true];
$options = new ArrayObject($options);
$event = $this->dispatchEvent('Model.beforeSave', ['entity' => $entity, 'options' => $options]);
if ($event->isStopped()) {
return $event->result;
}
if ($entity->errors()) {
return false;
}
$mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
if ($options['checkRules'] && !$this->checkRules($entity, $mode, $options)) {
return false;
}
$type = $this->connection()->getIndex()->getType($this->name());
$id = $entity->id ?: null;
$data = $entity->toArray();
unset($data[$id]);
$doc = new ElasticaDocument($id, $data);
$doc->setAutoPopulate(true);
$result = $type->addDocument($doc);
$entity->id = $doc->getId();
$entity->_version = $doc->getVersion();
$entity->isNew(false);
$entity->source($this->name());
$entity->clean();
$this->dispatchEvent('Model.afterSave', ['entity' => $entity, 'options' => $options]);
return $entity;
}