I am trying to update a document with script as mentioned below:
$script = new \Elastica\Script( 'ctx._source.fuzzy = value', array( 'value' => 'y' ), 'groovy' );
$script->setId( 1 );
$this->getType()->updateDocument( $script );
I am not able to recognize what is wrong as there is no error message. Am I missing any step in this process?
As you don't get an error, I assume the operation worked as intended. As the updated is by default not immediately available on all shards and I assume you directly check afterwards if it worked, I recommend you to pass the 'refresh' option as following:
$type->updateDocument($script, array('refresh' => true));
A working example can be found here: https://github.com/ruflin/Elastica/blob/master/test/lib/Elastica/Test/TypeTest.php#L609
In the case that this is does not solve the issue, please post also the part where you add the document and query for it to check if it exists for further analysis.
Related
I am wanting to take data from mysql, display it, have users edit this data and then save it to the database. I have all currently working except the saving to the database part. I have been lead to believe that the UPDATE query in mysql is how you get this to work. I put an UPDATE query in place and had no luck. Has anyone here experienced this issue before? I have read several posts here and on perl monks about this issue and can't seem to find an answer that solves my problem. I will put some of my code below. Thanks!
my $dbh=DBI->connect("dbi:mysql:survey_one", "user", "password", { PrintError =>0, RaiseError => 1, AutoCommit => 1}) or die $DBI::errstr;
my $edit_sql = q{UPDATE new_survey SET question = ? WHERE title= ?};
my $sthe = $dbh->prepare($edit_sql);
$sthe->execute($questionedit, $marathon);
$sthe->finish();
I'd like to note that if I were to SET the question column to a string like 'does this work?' I would have success. It's when I try to use user input $questionedit, which is defined as
$questionedit = param('editquestion'); This is where users can edit the question field.
Thanks!
The following are the four possible outcomes:
Nothing happens because the code isn't executed.
An exception is thrown because an error occurred (and RaiseError => 1 was used). The exception will end up being printed to STDERR unless caught.
->execute returns the string 0E0 (which is true, but numifies to zero) because no rows were updated because the WHERE clause didn't match any rows.
->execute returns a positive number indicating the number of rows modified.
Determine which case is applicable, and you'll know how to move forward.
Using Yii2 2.0.12, php 5.6.24 and codeception 2.3.3
On a recent upgrade a set of acceptance tests that have always worked up to now, failed. The upgrade was a standard composer update to the latest versions. The test scenario is as follows:
Test adding a record via a form
Grab the new record to verify
Test adding a second record via a form
Grab the new record to verify
The second grab fails in that it returns an empty record. I've traced this back to the 'queryInternal' method of the 'Command' object and there is a correctly formed PDO statement which performs the correct query but returns an empty result. I've tried breaking before the PDO->execute() and performing the query in MySQL. The query works but the PDO statement fails.
the code of the Cept test script is:
$I->wantTo('Do successive grabs');
$I->amOnPage('index.php?r=portfolio%2Fcreate');
$I->see('Create Portfolio');
$I->fillField('Name','Test Portfolio 1');
$I->click('Create');
$r = $I->grabRecord('app\models\Portfolio' , ['name' => 'Test Portfolio 1']);
$I->amOnPage('index.php?r=portfolio%2Fcreate');
$I->see('Create Portfolio');
$I->fillField('Name','Test Portfolio 2');
$I->click('Create');
$r = $I->grabRecord('app\models\Portfolio' , ['name' => 'Test Portfolio 2']);
The second "grabRecord" produces an empty record. Anyone else have a similar problem?
This seems to have been caused by the change in the 'cleanup' option in 2.2.6. Changing this value to false in the .yml file seems to clear the problem, although I'm not sure why.
I have added a new_column to a MyISAM table on a MySQLproduction server.
I have deleted the appropriate model cache file from the /app/tmp/models folder.
Upon reloading the page and inspection, the newly generated cache file for the model INCLUDES the new column.
The new column doesn't appear in SQL queries or results when read with fields => null.
debug($this->read(null, $id));
//Query generates fields: SELECT Model.column_a, Model.column_b, Model.id WHERE ...
//Results do not include the new column
debug($this->read(array('new_column'), $id));
//Query generates fields: SELECT Model.new_column, Model.id WHERE ...
//Results include the new column and id
Possible caveats and additional info:
The table has 39 columns (why would this be a problem?)
The model is an EXTENDED class from a plugin
The new column definition is TEXT DEFAULT null
Previous updates to the table gave expected results
Local development replica of the app doesn't have this issue
The test controller action enables Configure::write(debug => 2)
CakePHP 2.3.6, PHP 5.4.19, MySQL 5.1.70
How to make the Model recognize the new table definition? Are there any other cache systems I should investigate?
Edit #1:
debug($this->ModelName->getDataSource()->describe('table_name'));
// outputs all columns
// ...
'new_column' => array(
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
'collate' => 'utf8_general_ci',
'charset' => 'utf8'
)
Edit #2:
I have tried disabling the cache completely. Although the model picks up the new column, it is not really a solution.
Configure::write('Cache.disable', true);
// new_column appears in both query and results
Edit #3:
Further debugging shows that there is a discrepancy between these two results:
$this->ModelName->getDataSource()->fields($this->Model, 'ModelName');
//returns fields without new_colum
array_keys($this->ModelName->schema());
//returns fields with new_column
And this temporarily solves the problem:
$ds = $this->ModelName->getDataSource();
$ds->cacheMethods = false;
...
Method caching of the DboSource is the source of this issue. Slowly getting to the bottom of things.. :D
You have to clear all the cache files (but not directories) in app/tmp/cache whenever you update an application that is in production (debug is set to 0).
The actual cache causing problems in this issue was the app/tmp/cache/persistent/_cake_core_method_cache. Don't let the persistent part fool you, as it did me.
See this great answer for more details.
It is probably something related to the Database cache engine: in this case you can solve your issue just changing the query (for example, removing a field using the 'fields' statement). You can even try deleting the CakePHP's model cache: it's located in app/tmp/cache/models.
Happy Coding!
In fact what you can add a bootstrap file if you use a Plugin.
In my global bootstrap file I put:
CakePlugin::load('Myplugin', array('routes' => true, 'bootstrap' => true));
and in Plugin/Myplugin/Config/bootstrap.php I have
<?php
Cache::config('_cake_core_', array(
'engine' => 'File',
'prefix' => 'cake_core_myplugin_',
'path' => CACHE . 'persistent' . DS,
'serialize' => true,
'duration' => '+7 days',
));
?>
I added two columns in one of my models and I faced the same issue has you. Then I deleted all the models and persistent cache files and I change the duration inside my bootstrap file to 1 min.
Finally, my model was ok and reloaded. Later I go back to the initial duration.
It is durty, but it also looks like CakePHP messed up a little bit with cache.
Hope that it can help
Using CI for the first time and i'm smashing my head with this seemingly simple issue. My query wont insert the record.
In an attempt to debug a possible problem, the insert code has been simplified but i'm still getting no joy.
Essentially, i'm using;
$data = array('post_post' => $this->input->post('ask_question'));
$this->db->insert('posts', $data);
I'm getting no errors (although that possibly due to disabling them in config/database.php due to another CI related trauma :-$ )
Ive used
echo print $this->db->last_query();
to get the generated query, shown as below:
INSERT INTO `posts` (`post_post`) VALUES ('some text')
I have pasted this query into phpMyAdmin, it inserts no problem. Ive even tried using $this->db->query() to run the outputted query above 'manually' but again, the record will not insert.
The scheme of the DB table 'posts' is simply two columns, post_id & post_post.
Please, any pointers on whats going on here would be greatly appreciated...thanks
OK..Solved, after much a messing with CI.
Got it to work by setting persistant connection to false.
$db['default']['pconnect'] = FALSE;
sigh
Things generally look ok, everything you have said suggests that it should work. My first instinct would be to check that what you're inserting is compatible with your SQL field.
Just a cool CI feature; I'd suggest you take a look at the CI Database Transaction class. Transactions allow you to wrap your query/queries inside a transaction, which can be rolled back on failure, and can also make error handling easier:
$this->db->trans_start();
$this->db->query('INSERT INTO posts ...etc ');
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
// generate an error... or use the log_message() function to log your error
}
Alternatively, one thing you can do is put your Insert SQL statement into $this->db->query(your_query_here), instead of calling insert. There is a CI Query feature called Query Binding which will also auto-escape your passed data array.
Let me know how it goes, and hope this helps!
I have added a new column to my table Attributes which already has (id , form_id(foreign key),type,label,size,sequence no,instr)
where instr is the new column i have added.
My application is in CakePHP and MySQL.
I have used the following code to insert into the table Attributes But the field instr alone not inserted.
function saveFieldname($data)//from untitledfieldname
{
$this->data['Attribute']['form_id'] = $this->find( 'all', array(
'fields' => array('Form.id'),
'order' => 'Form.id DESC'
));
$this->data['Attribute']['form_id'] = $this->data['Attribute']['form_id'][0]['Form']['id'];
$this->data['Attribute']['label'] = 'Label';
$this->data['Attribute']['size'] ='50';
$this->data['Attribute']['instr'] ='Fill';
$this->data['Attribute']['type'] = $data['Attribute']['type'];
$this->data['Attribute']['sequence_no'] = $data['Attribute']['sequence_no'];
$this->Attribute->save($this->data);
}
Please suggest me..
The information about the structure of your table is probably cached. Remove the content of "app/tmp/cache/models" and try it again.
Note that in development the debug level in app/config/core.php is usually set to > 1. This means you should never run into the issue in development because Cake will not cache. However, in production, debug is set to 0 in core.php, thus causing cake to start caching.
To add to that, I had removed the cache files in app/tmp/cache/models as dhofstet specified on my production CakePHP app and the find queries were still not grabbing my new column.
--
As well as clearing the model cache files, I set the debug level to 2 on my production site and did page refresh then set it back to 0 and it got it working again. I know this is an ugly approach, but it fixed it for me when no other methods were working.