本文整理汇总了PHP中Cake\ORM\Entity类的典型用法代码示例。如果您正苦于以下问题:PHP Entity类的具体用法?PHP Entity怎么用?PHP Entity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Entity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _prettify
/**
* Method that renders Entity values through Field Handler Factory.
*
* @param Cake\ORM\Entity $entity Entity instance
* @param Cake\ORM\Table|string $table Table instance
* @param array $fields Fields to prettify
* @return void
*/
protected function _prettify(Entity $entity, $table, array $fields = [])
{
if (!$this->__fhf instanceof FieldHandlerFactory) {
$this->__fhf = new FieldHandlerFactory();
}
if (empty($fields)) {
$fields = array_keys($entity->toArray());
}
foreach ($fields as $field) {
// handle belongsTo associated data
if ($entity->{$field} instanceof Entity) {
$tableName = $table->association($entity->{$field}->source())->className();
$this->_prettify($entity->{$field}, $tableName);
}
// handle hasMany associated data
if (is_array($entity->{$field})) {
if (empty($entity->{$field})) {
continue;
}
foreach ($entity->{$field} as $associatedEntity) {
if (!$associatedEntity instanceof Entity) {
continue;
}
$tableName = $table->association($associatedEntity->source())->className();
$this->_prettify($associatedEntity, $tableName);
}
}
$renderOptions = ['entity' => $entity];
$entity->{$field} = $this->__fhf->renderValue($table instanceof Table ? $table->registryAlias() : $table, $field, $entity->{$field}, $renderOptions);
}
}
示例2: __construct
public function __construct(Table $table, Entity $entity, $field, array $settings)
{
$this->setRoot(TMP . 'ProfferTests');
$this->setTable($table->alias());
$this->setField($field);
$this->setSeed('proffer_test');
if (isset($settings['thumbnailSizes'])) {
$this->setPrefixes($settings['thumbnailSizes']);
}
$this->setFilename($entity->get($field));
}
示例3: _generateUniqueSlug
/**
* Generate unique slug by field.
*
* @param Entity $entity
* @return mixed|string
*/
protected function _generateUniqueSlug(Entity $entity)
{
$conditions = [];
$slug = $entity->get($this->_config['slug'], '');
if ($this->_config['unique']) {
$conditions[$this->_config['slug'] . ' LIKE'] = $slug . '%';
}
if ($id = $entity->get('id', false)) {
$conditions[$this->_table->primaryKey() . ' !='] = $id;
}
if (empty($slug)) {
$slug = $entity->get($this->_config['field'], 'Title');
}
if ($this->_config['translate']) {
$Slug = new Slug();
$slug = $Slug->create($slug);
}
$duplicates = $this->_table->find()->where($conditions)->toArray();
if (!empty($duplicates)) {
$duplicates = $this->_extractSlug($duplicates);
if (!in_array($slug, $duplicates)) {
return $slug;
}
$index = 1;
$startSlug = $slug;
while ($index > 0) {
if (!in_array($startSlug . $this->_config['separator'] . $index, $duplicates)) {
$slug = $startSlug . $this->_config['separator'] . $index;
$index = -1;
}
$index++;
}
}
return $slug;
}
示例4: beforeSave
/**
* Check if there is some files to upload and modify the entity before
* it is saved.
*
* At the end, for each files to upload, unset their "virtual" property.
*
* @param Event $event The beforeSave event that was fired.
* @param Entity $entity The entity that is going to be saved.
*
* @throws \LogicException When the path configuration is not set.
* @throws \ErrorException When the function to get the upload path failed.
*
* @return void
*/
public function beforeSave(Event $event, Entity $entity)
{
$config = $this->_config;
foreach ($config['fields'] as $field => $fieldOption) {
$data = $entity->toArray();
$virtualField = $field . $config['suffix'];
if (!isset($data[$virtualField]) || !is_array($data[$virtualField])) {
continue;
}
$file = $entity->get($virtualField);
if ((int) $file['error'] === UPLOAD_ERR_NO_FILE) {
continue;
}
if (!isset($fieldOption['path'])) {
throw new \LogicException(__('The path for the {0} field is required.', $field));
}
if (isset($fieldOption['prefix']) && (is_bool($fieldOption['prefix']) || is_string($fieldOption['prefix']))) {
$this->_prefix = $fieldOption['prefix'];
}
$extension = (new File($file['name'], false))->ext();
$uploadPath = $this->_getUploadPath($entity, $fieldOption['path'], $extension);
if (!$uploadPath) {
throw new \ErrorException(__('Error to get the uploadPath.'));
}
$folder = new Folder($this->_config['root']);
$folder->create($this->_config['root'] . dirname($uploadPath));
if ($this->_moveFile($entity, $file['tmp_name'], $uploadPath, $field, $fieldOption)) {
if (!$this->_prefix) {
$this->_prefix = '';
}
$entity->set($field, $this->_prefix . $uploadPath);
}
$entity->unsetProperty($virtualField);
}
}
示例5: afterSave
/**
* After Save Callback
*
* @param Cake/Event/Event $event The afterSave event that was fired.
* @param Cake/ORM/Entity $entity The entity
* @param ArrayObject $options Options
* @return void
*/
public function afterSave(Event $event, Entity $entity, ArrayObject $options)
{
if ($entity->isNew()) {
$this->saveDefaultUri($entity);
} else {
// Change route.
if ($entity->dirty()) {
$SeoUris = TableRegistry::get('Seo.SeoUris');
$SeoCanonicals = TableRegistry::get('Seo.SeoCanonicals');
$urlsConfig = $this->config('urls');
foreach ($urlsConfig as $key => $url) {
$uri = $this->_getUri($entity, $url);
$seoUri = $SeoUris->find()->contain(['SeoCanonicals'])->where(['model' => $this->_table->alias(), 'foreign_key' => $entity->id, 'locale' => isset($url['locale']) ? $url['locale'] : null])->first();
if ($seoUri) {
$seoUri = $SeoUris->patchEntity($seoUri, ['uri' => $uri], ['associated' => ['SeoCanonicals']]);
$SeoUris->save($seoUri);
$seoUriCanonical = Router::fullBaseUrl() . $uri;
$seoUri->seo_canonical->set('canonical', $seoUriCanonical);
$SeoCanonicals->save($seoUri->seo_canonical);
}
}
}
}
\Cake\Cache\Cache::clear(false, 'seo');
}
示例6: one
/**
* Validates a single entity by getting the correct validator object from
* the table and traverses associations passed in $options to validate them
* as well.
*
* @param \Cake\ORM\Entity $entity The entity to be validated
* @param array|\ArrayObject $options options for validation, including an optional key of
* associations to also be validated.
* @return bool true if all validations passed, false otherwise
*/
public function one(Entity $entity, $options = [])
{
$valid = true;
$types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE];
$propertyMap = $this->_buildPropertyMap($options);
$options = new ArrayObject($options);
foreach ($propertyMap as $key => $assoc) {
$value = $entity->get($key);
$association = $assoc['association'];
if (!$value) {
continue;
}
$isOne = in_array($association->type(), $types);
if ($isOne && !$value instanceof Entity) {
$valid = false;
continue;
}
$validator = $association->target()->entityValidator();
if ($isOne) {
$valid = $validator->one($value, $assoc['options']) && $valid;
} else {
$valid = $validator->many($value, $assoc['options']) && $valid;
}
}
if (!isset($options['validate'])) {
$options['validate'] = true;
}
return $this->_processValidation($entity, $options) && $valid;
}
示例7: save
/**
* Takes an entity from the source table and looks if there is a field
* matching the property name for this association. The found entity will be
* saved on the target table for this association by passing supplied
* `$options`
*
* @param \Cake\ORM\Entity $entity an entity from the source table
* @param array|\ArrayObject $options options to be passed to the save method in
* the target table
* @return bool|Entity false if $entity could not be saved, otherwise it returns
* the saved entity
* @see Table::save()
* @throws \InvalidArgumentException when the association data cannot be traversed.
*/
public function save(Entity $entity, array $options = [])
{
$targetEntities = $entity->get($this->property());
if (empty($targetEntities)) {
return $entity;
}
if (!is_array($targetEntities) && !$targetEntities instanceof \Traversable) {
$name = $this->property();
$message = sprintf('Could not save %s, it cannot be traversed', $name);
throw new \InvalidArgumentException($message);
}
$properties = array_combine((array) $this->foreignKey(), $entity->extract((array) $this->source()->primaryKey()));
$target = $this->target();
$original = $targetEntities;
foreach ($targetEntities as $k => $targetEntity) {
if (!$targetEntity instanceof Entity) {
break;
}
if (!empty($options['atomic'])) {
$targetEntity = clone $targetEntity;
}
$targetEntity->set($properties, ['guard' => false]);
if ($target->save($targetEntity, $options)) {
$targetEntities[$k] = $targetEntity;
continue;
}
if (!empty($options['atomic'])) {
$original[$k]->errors($targetEntity->errors());
$entity->set($this->property(), $original);
return false;
}
}
$entity->set($this->property(), $targetEntities);
return $entity;
}
示例8: getFilePath
public function getFilePath(Entity $entity, $path, $extension)
{
$id = $entity->get('id');
$path = trim($path, '/');
$replace = ['%id1000' => ceil($id / 1000), '%id100' => ceil($id / 100), '%id' => $id, '%y' => date('y'), '%m' => date('m')];
$path = strtr($path, $replace) . '.' . $extension;
return $path;
}
示例9: getAttributes
/**
* Get resource attributes.
*
* @param \Cake\ORM\Entity $resource Entity resource
* @return array
*/
public function getAttributes($resource)
{
if ($resource->has($this->idField)) {
$hidden = array_merge($resource->hiddenProperties(), [$this->idField]);
$resource->hiddenProperties($hidden);
}
return $resource->toArray();
}
示例10: 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);
}
示例11: beforeSave
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
if ($entity->isNew() && empty($entity->user_id)) {
$entity->errors('user_id', ['not_found', 'User not found']);
$entity->errors('net_id', ['not_found', 'User not found']);
return false;
}
}
示例12: afterSave
/**
* AfterSave callback.
*
* @param \Cake\Event\Event $event The afterSave event that was fired.
* @param \Cake\ORM\Entity $entity The entity that was saved.
* @param \ArrayObject $options The options passed to the callback.
*
* @return bool
*/
public function afterSave(Event $event, Entity $entity, ArrayObject $options)
{
if ($entity->isNew()) {
$comment = new Event('Model.BlogArticlesComments.add', $this, ['comment' => $entity]);
$this->eventManager()->dispatch($comment);
}
return true;
}
示例13: __construct
/**
* Constructor
*
* @param \Cake\ORM\Entity $entity Entity
* @param int $code code to report to client
*/
public function __construct(Entity $entity, $code = 422)
{
$this->_validationErrors = array_filter((array) $entity->errors());
$flat = Hash::flatten($this->_validationErrors);
$errorCount = $this->_validationErrorCount = count($flat);
$this->message = __dn('crud', 'A validation error occurred', '{0} validation errors occurred', $errorCount, [$errorCount]);
parent::__construct($this->message, $code);
}
示例14: toggle
/**
* Render ajax toggle element.
*
* @param array|string $url
* @param array $data
* @param Entity|\Cake\ORM\Entity $entity
* @return string
*/
public function toggle($entity, $url = [], array $data = [])
{
if (empty($url)) {
$url = ['action' => 'toggle', 'prefix' => $this->request->param('prefix'), 'plugin' => $this->request->param('plugin'), 'controller' => $this->request->param('controller'), (int) $entity->get('id'), (int) $entity->get('status')];
}
$data = Hash::merge(['url' => $url, 'entity' => $entity], $data);
return $this->_View->element(__FUNCTION__, $data);
}
示例15: afterSave
/**
* Called after an entity is saved
* @param \Cake\Event\Event $event Event object
* @param \Cake\ORM\Entity $entity Entity object
* @param \ArrayObject $options Options
* @return void
* @uses MeCms\Model\Table\AppTable::afterSave()
*/
public function afterSave(\Cake\Event\Event $event, \Cake\ORM\Entity $entity, \ArrayObject $options)
{
//Creates the folder
if ($entity->isNew()) {
(new Folder())->create(PHOTOS . DS . $entity->id, 0777);
}
parent::afterSave($event, $entity, $options);
}