本文整理汇总了PHP中Cake\ORM\Entity::toArray方法的典型用法代码示例。如果您正苦于以下问题:PHP Entity::toArray方法的具体用法?PHP Entity::toArray怎么用?PHP Entity::toArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Entity
的用法示例。
在下文中一共展示了Entity::toArray方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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);
}
}
示例3: 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();
}
示例4: getForm
/**
* @param Entity $page
*
* @return \Symfony\Component\Form\Form
* @throws \Rad\DependencyInjection\Exception\ServiceNotFoundException
*/
public static function getForm($page = null)
{
$data = null;
if ($page) {
$data = $page->toArray();
}
$action = $page ? Container::get('router')->generateUrl(['pages', $data['slug']]) : Container::get('router')->generateUrl(['pages']);
$formFactory = Forms::createFormFactory();
$options = ['action' => $action, 'method' => $page ? 'PUT' : 'POST'];
return $formFactory->createBuilder('form', $data, $options)->add('title', 'text', ['required' => true, 'attr' => ['class' => 'form-control']])->add('slug', 'text', ['required' => true, 'attr' => ['class' => 'form-control']])->add('body', 'textarea', ['required' => true, 'attr' => ['class' => 'form-control wysiwyg']])->add('submit', 'submit')->getForm();
}
示例5: testToArrayHiddenProperties
/**
* Test that toArray respects hidden properties.
*
* @return void
*/
public function testToArrayHiddenProperties()
{
$data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
$entity = new Entity($data);
$entity->hiddenProperties(['secret']);
$this->assertEquals(['name' => 'mark', 'id' => 1], $entity->toArray());
}
示例6: __construct
/**
* Email constructor.
*
* @param Entity $entity
*/
public function __construct(Entity $entity)
{
$config = PluginConfig::getInstance()->getGroup('union_core');
$this->_entity = $entity;
$this->config = $config->params();
$this->_entityId = $entity->get('id');
$this->_arrayData = $entity->toArray();
$this->_serverName = Union::serverName();
$this->mailer = $this->config->get('mailer', 'mail');
$this->fromName = $this->config->get('mailfrom', 'Union CMS');
$this->fromEmail = $this->config->get('mailname', 'noreply@union.cms');
}
示例7: bind
/**
* Bind data.
*
* @param Entity $data
*/
public function bind(Entity $data)
{
$this->_data[$this->name] = $data->toArray();
}
示例8: prepareEntityData
/**
* converts a Entity into a flat Array
* properties are inserted in first row
*
* @param Entity $entity
* @return array
*/
public function prepareEntityData(Entity $entity = null)
{
$entityArray = $entity->toArray();
$data = [array_keys($entityArray)];
$data[] = array_values($entityArray);
return $data;
}
示例9: beforeSave
/**
* Check if there is some files to upload and modify the entity before
* it is saved.
*
* 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 \ErrorException
*/
public function beforeSave(Event $event, Entity $entity)
{
$config = $this->_config;
/**
* Foreach file set in the configuration, do the upload.
*/
foreach ($config['fields'] as $field => $fieldOption) {
$data = $entity->toArray();
$virtualField = $field . $config['suffix'];
/**
* Check if the "virtual" field is in the data array and if this
* field have a name param. (This is only for check if its a file type.)
*/
if (isset($data[$virtualField]) && !empty($data[$virtualField]['name'])) {
//Get the array (name, tmp_name etc) of the virtual field.
$file = $entity->get($virtualField);
/**
* If the tmp_name is empty, that mean there was an error while uploading
* the file in the temporary directory.
*/
if (empty($file['tmp_name'])) {
continue;
}
if (!isset($fieldOption['path'])) {
throw new \ErrorException(__('Error to get the path for the {0} field.', $field));
}
/**
* Check if the user has set an option for the prefix, else use the default prefix config.
*/
if (isset($fieldOption['prefix']) && (is_bool($fieldOption['prefix']) || is_string($fieldOption['prefix']))) {
$this->_prefix = $fieldOption['prefix'];
}
/**
* Get the upload path with identifiers replaced by their corresponding folders.
*/
$uploadPath = $this->getUploadPath($entity, $fieldOption['path'], (new File($file['name'], false))->ext());
if (!$uploadPath) {
throw new \ErrorException(__('Error to get the uploadPath.'));
}
/**
* Create the folders if it doesn't exist.
*/
$this->_folder->create(dirname($uploadPath), 0755);
/**
* Try to move the temporary file to the correct upload path.
*/
if ($this->moveFile($entity, $file['tmp_name'], $uploadPath, $fieldOption, $field)) {
if (!$this->_prefix) {
$this->_prefix = '';
}
/**
* Set the new value for the current field.
*/
$entity->set($field, $this->_prefix . $uploadPath);
}
}
/**
* Unset the virtual field from the Entity.
*/
$entity->unsetProperty($virtualField);
}
}
示例10: _resourceToString
/**
* Convert Entity resource values to strings.
* Temporary fix for bug with resources and json_encode() (see link).
*
* @param \Cake\ORM\Entity $entity Entity
* @return void
* @link https://github.com/cakephp/cakephp/issues/9658
*/
protected function _resourceToString(Entity $entity)
{
$fields = array_keys($entity->toArray());
foreach ($fields as $field) {
// handle belongsTo associated data
if ($entity->{$field} instanceof Entity) {
$this->_resourceToString($entity->{$field});
}
// handle hasMany associated data
if (is_array($entity->{$field})) {
if (empty($entity->{$field})) {
continue;
}
foreach ($entity->{$field} as $associatedEntity) {
if (!$associatedEntity instanceof Entity) {
continue;
}
$this->_resourceToString($associatedEntity);
}
}
if (is_resource($entity->{$field})) {
$entity->{$field} = stream_get_contents($entity->{$field});
}
}
}
示例11: validateResource
/**
* Validates a resource and returns the validation errors.
*
* @param Cake\ORM\Entity $resource
*
* @return array
*/
public function validateResource(Entity &$resource)
{
$errors = $this->getResourceValidator()->errors($resource->toArray());
$resource->errors($errors, null, true);
return $errors;
}