本文整理匯總了PHP中Cake\Datasource\EntityInterface類的典型用法代碼示例。如果您正苦於以下問題:PHP EntityInterface類的具體用法?PHP EntityInterface怎麽用?PHP EntityInterface使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了EntityInterface類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: isActive
/**
* Checks if the given menu link should be marked as active.
*
* If `$item->activation` is a callable function it will be used to determinate
* if the link should be active or not, returning true from callable indicates
* link should be active, false indicates it should not be marked as active.
* Callable receives current request object as first argument and $item as second.
*
* `$item->url` property MUST exists if "activation" is not a callable, and can
* be either:
*
* - A string representing an external or internal URL (all internal links must
* starts with "/"). e.g. `/user/login`
*
* - An array compatible with \Cake\Routing\Router::url(). e.g. `['controller'
* => 'users', 'action' => 'login']`
*
* Both examples are equivalent.
*
* @param \Cake\Datasource\EntityInterface $item A menu's item
* @return bool
*/
public function isActive(EntityInterface $item)
{
if ($item->has('activation') && is_callable($item->get('activation'))) {
$callable = $item->get('activation');
return $callable($this->_View->request, $item);
}
$itemUrl = $this->sanitize($item->get('url'));
if (!str_starts_with($itemUrl, '/')) {
return false;
}
switch ($item->get('activation')) {
case 'any':
return $this->_requestMatches($item->get('active'));
case 'none':
return !$this->_requestMatches($item->get('active'));
case 'php':
return php_eval($item->get('active'), ['view', &$this->_View, 'item', &$item]) === true;
case 'auto':
default:
static $requestUri = null;
static $requestUrl = null;
if ($requestUri === null) {
$requestUri = urldecode(env('REQUEST_URI'));
$requestUrl = str_replace('//', '/', '/' . urldecode($this->_View->request->url) . '/');
}
$isInternal = $itemUrl !== '/' && str_ends_with($itemUrl, str_replace_once($this->baseUrl(), '', $requestUri));
$isIndex = $itemUrl === '/' && $this->_View->request->isHome();
$isExact = str_replace('//', '/', "{$itemUrl}/") === $requestUrl || $itemUrl == $requestUri;
if ($this->config('breadcrumbGuessing')) {
return $isInternal || $isIndex || $isExact || in_array($itemUrl, $this->_crumbs());
}
return $isInternal || $isIndex || $isExact;
}
}
示例2: _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;
}
示例3: __invoke
/**
* Performs the existence 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.
* @throws \RuntimeException When the rule refers to an undefined association.
* @return bool
*/
public function __invoke(EntityInterface $entity, array $options)
{
if (is_string($this->_repository)) {
$alias = $this->_repository;
$this->_repository = $options['repository']->association($alias);
if (empty($this->_repository)) {
throw new RuntimeException(sprintf("ExistsIn rule for '%s' is invalid. The '%s' association is not defined.", implode(', ', $this->_fields), $alias));
}
}
$source = $target = $this->_repository;
if (!empty($options['repository'])) {
$source = $options['repository'];
}
if ($source instanceof Association) {
$source = $source->source();
}
if ($target instanceof Association) {
$bindingKey = (array) $target->bindingKey();
$target = $target->target();
} else {
$bindingKey = (array) $target->primaryKey();
}
if (!empty($options['_sourceTable']) && $target === $options['_sourceTable']) {
return true;
}
if (!$entity->extract($this->_fields, true)) {
return true;
}
if ($this->_fieldsAreNull($entity, $source)) {
return true;
}
$primary = array_map([$target, 'aliasField'], $bindingKey);
$conditions = array_combine($primary, $entity->extract($this->_fields));
return $target->exists($conditions);
}
示例4: __invoke
/**
* Performs the existence 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 (is_string($this->_repository)) {
$this->_repository = $options['repository']->association($this->_repository);
}
$source = !empty($options['repository']) ? $options['repository'] : $this->_repository;
$source = $source instanceof Association ? $source->source() : $source;
$target = $this->_repository;
if ($target instanceof Association) {
$bindingKey = (array) $target->bindingKey();
$target = $this->_repository->target();
} else {
$bindingKey = (array) $target->primaryKey();
}
if (!empty($options['_sourceTable']) && $target === $options['_sourceTable']) {
return true;
}
if (!$entity->extract($this->_fields, true)) {
return true;
}
$nulls = 0;
$schema = $source->schema();
foreach ($this->_fields as $field) {
if ($schema->column($field) && $schema->isNullable($field) && $entity->get($field) === null) {
$nulls++;
}
}
if ($nulls === count($this->_fields)) {
return true;
}
$primary = array_map([$this->_repository, 'aliasField'], $bindingKey);
$conditions = array_combine($primary, $entity->extract($this->_fields));
return $this->_repository->exists($conditions);
}
示例5: __invoke
/**
* Performs the existence 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 (is_string($this->_repository)) {
$this->_repository = $options['repository']->association($this->_repository);
}
if (!empty($options['_sourceTable'])) {
$source = $this->_repository instanceof Association ? $this->_repository->target() : $this->_repository;
if ($source === $options['_sourceTable']) {
return true;
}
}
if (!$entity->extract($this->_fields, true)) {
return true;
}
$nulls = 0;
$schema = $this->_repository->schema();
foreach ($this->_fields as $field) {
if ($schema->isNullable($field) && $entity->get($field) === null) {
$nulls++;
}
}
if ($nulls === count($this->_fields)) {
return true;
}
$alias = $this->_repository->alias();
$primary = array_map(function ($key) use($alias) {
return "{$alias}.{$key}";
}, (array) $this->_repository->primaryKey());
$conditions = array_combine($primary, $entity->extract($this->_fields));
return $this->_repository->exists($conditions);
}
示例6: afterSave
/**
* afterSave Event
*
* @param Event $event Event
* @param EntityInterface $entity Entity to be saved
* @return void
*/
public function afterSave(Event $event, EntityInterface $entity)
{
$uploads = $entity->get($this->config('formFieldName'));
if (!empty($uploads)) {
$this->Attachments->addUploads($entity, $uploads);
}
}
示例7: resetPassword
/**
* Send the reset password email to the user
*
* @param EntityInterface $user User entity
* @param string $template string, note the first_name of the user will be prepended if exists
*
* @return array email send result
*/
protected function resetPassword(EntityInterface $user, $template = 'CakeDC/Users.reset_password')
{
$firstName = isset($user['first_name']) ? $user['first_name'] . ', ' : '';
$subject = __d('Users', '{0}Your reset password link', $firstName);
$user->hiddenProperties(['password', 'token_expires', 'api_token']);
$this->to($user['email'])->subject($subject)->viewVars($user->toArray())->template($template);
}
示例8: 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;
}
示例9: 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);
}
}
示例10: beforeSave
/**
* Callback before save entity.
*
* @param Event $event
* @param EntityInterface $entity
* @param \ArrayObject $options
* @return bool
*/
public function beforeSave(Event $event, EntityInterface $entity, \ArrayObject $options)
{
// Hack for test fieldToggle.
if ($entity->get('id') == 4) {
return false;
}
return true;
}
示例11: tagsChooser
/**
* Render a multi select with all available tags of entity and the tags of attachment preselected
*
* @param EntityInterface $entity the entity to get all allowed tags from
* @param Attachment\Model\Entity\Attachment $attachment the attachment entity to add the tag to
* @return string
*/
public function tagsChooser(EntityInterface $entity, $attachment)
{
if (!TableRegistry::exists($entity->source())) {
throw new Cake\Network\Exception\MissingTableException('Could not find Table ' . $entity->source());
}
$Table = TableRegistry::get($entity->source());
return $this->Form->select('tags', $Table->getAttachmentsTags(), ['type' => 'select', 'class' => 'tag-chooser', 'style' => 'display: block; width: 100%', 'label' => false, 'multiple' => true, 'value' => $attachment->tags]);
}
示例12: afterSaveCommit
public function afterSaveCommit(Event $event, EntityInterface $entity, \ArrayObject $options)
{
$rev = $this->_table->Revisions->newEntity();
unset($entity->revisions);
$rev->data = $entity->toArray();
$rev->ref = $this->_table->alias();
$rev->ref_id = $entity->id;
$this->_table->Revisions->save($rev);
}
示例13: 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());
}
}
示例14: _repository
/**
* Gets the repository for this entity
*
* @param EntityInterface $entity
* @return Table
*/
protected function _repository($entity)
{
$source = $entity->source();
if ($source === null) {
list(, $class) = namespaceSplit(get_class($entity));
$source = Inflector::pluralize($class);
}
return TableRegistry::get($source);
}
示例15: 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]);
}