Skip to content

Commit

Permalink
Merge pull request #168 from cakephp/sync-1
Browse files Browse the repository at this point in the history
Re-adding missing changes
  • Loading branch information
lorenzo authored May 23, 2018
2 parents 3b7cea3 + d83f7a0 commit fbc4df5
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .stickler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters:
phpcs:
standard: CakePHP
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ install:
- composer install --dev

script:
- sh -c "if [ '$RUN_TESTS' = '1' ]; then phpunit --stderr; fi"
- sh -c "if [ '$RUN_TESTS' = '1' ]; then vendor/bin/phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"

notifications:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License

CakePHP(tm) : The Rapid Development PHP Framework (http://cakephp.org)
Copyright (c) 2005-2015, Cake Software Foundation, Inc.
Copyright (c) 2005-2018, Cake Software Foundation, Inc.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can install ElasticSearch into your project using
following to your `composer.json` file:

"require": {
"cakephp/elastic-search": "dev-master"
"cakephp/elastic-search": "^1.0"
}

And run `php composer.phar update`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ruflin/elastica": "~3.1"
},
"require-dev": {
"cakephp/cakephp-codesniffer": "dev-master",
"cakephp/cakephp-codesniffer": "~2.1",
"psr/log": "~1.0",
"phpunit/phpunit": "^5.7|^6.0",
"cakephp/cakephp": "~3.4.0"
Expand Down
2 changes: 1 addition & 1 deletion src/Datasource/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Connection extends Client implements ConnectionInterface
* is `_all`
*
* @param array $config config options
* @param callback $callback Callback function which can be used to be notified
* @param callable $callback Callback function which can be used to be notified
* about errors (for example connection down)
*/
public function __construct(array $config = [], $callback = null)
Expand Down
72 changes: 72 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,78 @@ public function exists($conditions)
return $query->count() > 0;
}

/**
* Persists a list of entities 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 array $entities An array of entities
* @param array $options An array of options to be used for the event
* @return bool
*/
public function saveMany($entities, $options = [])
{
$options += ['checkRules' => true];
$options = new ArrayObject($options);

$documents = [];

foreach ($entities as $key => $entity) {
if (!$entity instanceof EntityInterface) {
throw new RuntimeException(sprintf(
'Invalid items in the list. Found `%s` but expected `%s`',
is_object($entity) ? get_class($entity) : gettype($entity),
EntityInterface::class
));
}

$event = $this->dispatchEvent('Model.beforeSave', [
'entity' => $entity,
'options' => $options
]);

if ($event->isStopped() || $entity->errors()) {
return false;
}

$mode = $entity->isNew() ? RulesChecker::CREATE : RulesChecker::UPDATE;
if ($options['checkRules'] && !$this->checkRules($entity, $mode, $options)) {
return false;
}

$id = $entity->id ?: null;

$data = $entity->toArray();
unset($data['id'], $data['_version']);

$doc = new ElasticaDocument($id, $data);
$doc->setAutoPopulate(true);

$documents[$key] = $doc;
}

$type = $this->connection()->getIndex()->getType($this->name());
$type->addDocuments($documents);

foreach ($documents as $key => $document) {
$entities[$key]->id = $doc->getId();
$entities[$key]->_version = $doc->getVersion();
$entities[$key]->isNew(false);
$entities[$key]->source($this->name());
$entities[$key]->clean();

$this->dispatchEvent('Model.afterSave', [
'entity' => $entities[$key],
'options' => $options
]);
}

return true;
}

/**
* 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
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ public function testNewEntities()
$this->assertSame($data[1], $result[1]->toArray());
}

/**
* Test saving many entities
*
* @return void
*/
public function testSaveMany()
{
$entities = [
new Document([
'title' => 'First',
'body' => 'Some new content'
], [
'markNew' => true
]),
new Document([
'title' => 'Second',
'body' => 'Some new content'
], [
'markNew' => true
])
];

$result = $this->type->saveMany($entities);
$this->assertTrue($result);
}

/**
* Test saving a new document.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/
require dirname(__DIR__) . '/vendor/autoload.php';

define('CAKE', dirname(__DIR__) . '/vendor/cakephp/cakephp/src/');

require CAKE . 'basics.php';

define('APP', __DIR__);

use Cake\Cache\Cache;
Expand Down

0 comments on commit fbc4df5

Please sign in to comment.