CakePHP added column doesn't appear in query - mysql

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

Related

How to use of cast in cakephp query for sorting?

I was facing a problem to sorting data. Sorting was not working perfectly then i do some R&D on it and there is mysql function "cast" that help to enable conversion of values from one data type to another. And it start working for me. But still the question was how i can use the cast in cakephp query builder.I spend lots of time over it, that`s why i post it here to help other to save there time. Below you can check the answer.
You can add the "+ 0" above the fields name in order array like below. It work for me :)
$unit_mix = $this->Unit->find('all', array('conditions' => array('property_id' => $property_id), 'order' => array('Unit.unit_no + 0' => 'ASC')));

drupal 'node' table regenerate

I have a drupal 7 installation in a shared hosting server and I am in a situation that the server provider removed some entries from node table when the database size crossed 1gb limit.
I lost a lot of entries, but some data exists in other tables with entity_id reference numbers.
I can query my data using the references from other tables but drupal interface could not show the data because of these missing entries.
So, how could I safely regenerate the entries into node table and its tables references ?
Where can I get the information about the usage of fields in drupal tables?
If possible, a step by step guide would be very helpful.
Jahangir is on the right track. The revision info will get you 90% of the way there.
My example makes the assumption that only the node table has been affected by these spurious deletions.
Start by getting any revisions that don't have a related node
$result = db_query('SELECT nid, MAX(vid) AS vid, uid, title, `timestamp`, `status`, `comment`, promote, sticky FROM {node_revision}
WHERE nid NOT IN (SELECT nid FROM {node}) GROUP BY nid');
You could do this next part using a node object, but given that we just need the node entries I would do it directly to the db:
foreach ($result as $record) {
//You might be able to get the created date by looking at the earliest revision
$min_result = db_query('SELECT nid, MIN(vid) as vid, `timestamp` FROM {node_revision} WHERE nid = :nid', array(':nid' => $record->nid));
$created = $min_result->fetchColumn(4);
$new_node = db_insert('node')
->fields(array(
'nid' => $record->nid,
'vid' => $record->vid,
'type' => 'YOU CANNOT GET THIS',
'language' => $record->language,
'title' => $record->title,
'uid' => $record->uid,
'created' => $created,
'updated' => $record->timestamp,
//you get the idea...
));
}
I wasn't able to find a way to retrieve the content type using the revisions, so that is the weakness of the solution. Assuming all of your field data is still in place, this should get your nodes back.
*disclaimer: I wasn't able to fake up an environment that resembled the problem presented here so this code is based on my experience and a little Googling. This hasn't been tested, but I hope it is still valuable. Downvotes humbly accepted.

Unable to update a document elastica

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.

Backing Up MySQL's Data

As while using Laravel, we have an option to seed our database or create tables anytime, like
class UsersTableSeeder extends Seeder
{
public function run()
{
User::truncate();
User::create([
'username' => 'Junaid',
'email' => 'darulehsan03#gmail.com',
'password' => '1234'
]);
User::create([
'username' => 'Junaid Farooq',
'email' => 'aba#bcd.com',
'password' => '4321'
]);
}
}
we can seed our database anytime, but what if we have a large no of rows in our table, which are not being seeded but added by the users , then how can we put it that way, like a Seeder file so, anytime at anyplace , we can load all those rows through our Seeder File?
not asking about to save .SQL file and then import or Export it, But a way to backup them in a Seeder file
I don't know of any method of backing up the data to a seed file. You can, like you've already said, export and import your data.
There are also a couple of packages available to backup and restore a database.
laravel-backup, which seems to be a Laravel-specific package that allows you to backup your database and restore it.
database-backup, which is framework agnostic but does come with a Laravel service provider for easier integration with Laravel.
Both seem to allow you to backup and restore from Amazon S3. Having used neither I can't say which is better or why. You'll have to try both out and make that decision for yourself.

Why doesn't a new table column show up in the query results in CakePHP?

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.