本文整理汇总了PHP中Elastica\Document::setAutoPopulate方法的典型用法代码示例。如果您正苦于以下问题:PHP Document::setAutoPopulate方法的具体用法?PHP Document::setAutoPopulate怎么用?PHP Document::setAutoPopulate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elastica\Document
的用法示例。
在下文中一共展示了Document::setAutoPopulate方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: testAddDocumentAutoGeneratedId
/**
* @group functional
*/
public function testAddDocumentAutoGeneratedId()
{
$index = $this->_createIndex();
$type = $index->getType('elastica_type');
$document = new Document();
$document->setAutoPopulate();
$document->set('name', 'ruflin');
$this->assertEquals('', $document->getId());
$this->assertFalse($document->hasId());
$type->addDocument($document);
$this->assertNotEquals('', $document->getId());
$this->assertTrue($document->hasId());
$foundDoc = $type->getDocument($document->getId());
$this->assertInstanceOf('Elastica\\Document', $foundDoc);
$this->assertEquals($document->getId(), $foundDoc->getId());
$data = $foundDoc->getData();
$this->assertArrayHasKey('name', $data);
$this->assertEquals('ruflin', $data['name']);
}
示例3: testUpdateDocumentPopulateFields
/**
* @group functional
*/
public function testUpdateDocumentPopulateFields()
{
$this->_checkScriptInlineSetting();
$index = $this->_createIndex();
$type = $index->getType('test');
$client = $index->getClient();
$newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4'));
$newDocument->setAutoPopulate();
$type->addDocument($newDocument);
$script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"');
$script->setParam('count', 5);
$script->setUpsert($newDocument);
$client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => '_source'));
$data = $type->getDocument(1)->getData();
$this->assertArrayHasKey('field1', $data);
$this->assertEquals('value1', $data['field1']);
$this->assertArrayHasKey('field2', $data);
$this->assertEquals(15, $data['field2']);
$this->assertArrayHasKey('field4', $data);
$this->assertEquals('changed', $data['field4']);
$this->assertArrayNotHasKey('field3', $data);
$script = new Script('ctx._source.field2 += count; ctx._source.remove("field4"); ctx._source.field1 = field1;');
$script->setParam('count', 5);
$script->setParam('field1', 'updated');
$script->setUpsert($newDocument);
$client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => 'field2,field4'));
$document = $type->getDocument(1);
$data = $document->getData();
$this->assertArrayHasKey('field1', $data);
$this->assertEquals('updated', $data['field1']);
$this->assertArrayHasKey('field2', $data);
$this->assertEquals(20, $data['field2']);
$this->assertArrayNotHasKey('field3', $data);
$this->assertArrayNotHasKey('field4', $data);
}
示例4: testUpdateDocumentPopulateFields
public function testUpdateDocumentPopulateFields()
{
$index = $this->_createIndex();
$type = $index->getType('test');
$client = $index->getClient();
$newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4'));
$newDocument->setAutoPopulate();
$type->addDocument($newDocument);
$script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"');
$script->setParam('count', 5);
$newDocument->setScript($script);
$client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => '_source'));
$data = $newDocument->getData();
$this->assertArrayHasKey('field1', $data);
$this->assertEquals('value1', $data['field1']);
$this->assertArrayHasKey('field2', $data);
$this->assertEquals(15, $data['field2']);
$this->assertArrayHasKey('field4', $data);
$this->assertEquals('changed', $data['field4']);
$this->assertArrayNotHasKey('field3', $data);
$script = new Script('ctx._source.field2 += count; ctx._source.remove("field4"); ctx._source.field1 = field1;');
$script->setParam('count', 5);
$script->setParam('field1', 'updated');
$newDocument->setScript($script);
$client->updateDocument(1, $newDocument, $index->getName(), $type->getName(), array('fields' => 'field2,field4'));
$data = $newDocument->getData();
$this->assertArrayHasKey('field1', $data);
$this->assertEquals('value1', $data['field1'], 'Field1 should not be updated, because it is not in fields list');
$this->assertArrayHasKey('field2', $data);
$this->assertEquals(20, $data['field2'], 'Field2 should be 20 after incrementing by 5');
$this->assertArrayNotHasKey('field3', $data, 'Field3 should be removed already');
$this->assertArrayNotHasKey('field4', $data, 'Field3 should be removed');
$document = $type->getDocument(1);
$data = $document->getData();
$this->assertArrayHasKey('field1', $data);
$this->assertEquals('updated', $data['field1']);
$this->assertArrayHasKey('field2', $data);
$this->assertEquals(20, $data['field2']);
$this->assertArrayNotHasKey('field3', $data);
$this->assertArrayNotHasKey('field4', $data);
}