Data Source missing - cakephp

I run my CakePHP project on localhost. When I fill form, after clicking submit button it shows the following error:
Missing Datasource Configuration
Error: The datasource configuration Array was not found in
database.php.
I actually configured a datasource it and it looks like:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'arwinder',
'password' => 'root',
'database' => 'sh_db',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'arwinder',
'password' => 'root',
'database' => 'sh_db',
'prefix' => '',
//'encoding' => 'utf8',
);
}
... but it's getting ignored.

I had a similar issue when trying to work with data sanitation (it's a weird use case, what can I say).
The documentation for sanitize::escape says,
"The name of the database to quote the string for, as named in your
app/Config/database.php file."
It does not, in fact, take the name of the database eg. 'mydatabase' nor does it accept the host eg. 'localhost', nor does it accept the 'datasource' eg. 'Database/Mysql'. When given any of these VALUES, you get the error mentioned in the question.
What you actually need is the name of the "Datasource" to use. Again, the documentation for 'Datasource' fails to answer this topic.
I was stumped. So I searched for that error and found this unanswered post.
I finally found a solution thorough shear dumb luck, and as this post is still the highest result for this error on Google, I came back to answer it.
What you actually need is the variable name (key not value) as listed in the above mentioned config file. eg. $default or $test or $what_have_you. You then provide 'default', 'test' or 'what_have_you' depending on hostname.
Here's what boggles my mind the most. If the Datasource changes from 'production' to 'testing' and also 'local_dev' knowing which one is in use at any given time is critical to making Sanitise:escape() work.
But hold on a second... if you're doing that, you basically set the datasource to $default using a constructor method as documented here on SO so in all actuality, this mysterious 'Datasource' it's almost always going to be 'default' unless it's set on the fly, as an override, in your model which would be known while you're Sanitizing for a manual query anyway.
In fact, the default for this parameter is indeed, 'default'. It's not a required parameter at all, but because the documentation doesn't list it as optional or provide a defined default parameter (on that particular page) one might assume that it is required.
Anyway, the answer is to not provide a $datasource unless you are using $useDBConfig to specify a different datasource in your Model already.

Related

cakePHP 2.x Custom Authentication

So I am working with cakePHP 2.3 and I try to use different Frameworks when possible a) to keep my 41 Year old mind aware b) To make sure I use every tool in the shed for myself and my customer.
I have a personal SaaS app Im building and need to know the best way to add "where site_id = 2" to the authentication calls basically based on how they are viewing the app i.e. subdomain or domain sets a particular site_id in AppController.
I have looked for custom authentication but I havent seen anything that stood out. I also have a roles column & table which is comma delim I need to join in the auth request
Any good how to's or pointers would be great
Thanks
I'm just taking a shot in the dark here with limited info but i think this is somewhere around the woods of what your looking for.
Locate your cake build and navigate to /lib/Cake/Controller/Component/Auth/BaseAuthenticate.php
Locate:
public $settings = array(
'fields' => array(
'username' => 'username',
'password' => 'password'
),
'userModel' => 'User',
'scope' => array(),
'recursive' => 0,
'contain' => null,
);
and make your mods there.

CakePHP database table, missing datasource default

I found this similar question but my problem is different.
I moved my CakePHP 2.2 application to another server. There exists no problem before migration. Most of the things works fine after migration. I can reach most of my database tables etc. But when I try to reach one of my table I get this error:
"Error 500: Table stats for model Stat was not found in datasource default."
To solve this, I checked this folder:
"/app/tmp/cache/models"
In that folder there is a file for each of my tables
myapp_cake_model_default_mydatabase_table1
myapp_cake_model_default_mydatabase_table2
myapp_cake_model_default_mydatabase_table3
etc..
But there is no file for stats table. Can it be the problem? Or how can I solve this?
(Permission for "/app/tmp/cache/models" folder is 755)
In Database.php I have this:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'myuser',
'password' => 'mypass',
'database' => 'mydatabase',
'prefix' => '',
'encoding' => 'utf8',
);
Edit:
As I noted in thaJeztah's answer's comments, after removing all files inside app/tmp/cache/persistent problem solved. CakePHP created new model cache files and it worked. After one year I found out the real problem. The problem was setting cake model files' clear duration. I set clearing cache to +999 days, so model files aren't regenerated. While making model changes you can set lower values for model cache clear:
Cache::config('_cake_model_', array(
'engine' => "File",
'prefix' => "myapp_". 'cake_model_',
'path' => CACHE . 'models' . DS,
'serialize' => ($engine === 'File'),
'duration' => "+999 days"
));
Have you checked your database, e.g. in phpMyAdmin or MySql workbench? Does the table exist in the database?
The error message indicates that the table could not be accessed using the default connection. It's possible that the table is really missing, or that the user you're using to connect to the database does not have the right permissions for that table.
If you migrated the database from another server, did you get error messages while importing? If you did not create a dump enclosed in a transaction, it's possible that the database dump was only partially imported.
[update] this suggestion solved the problem;
Remove all files from app/tmp/cache/persistent and /app/tmp/cache/models then enable debugging. Your SQL log/debug should show the queries that CakePhp is using to detect if the tables exist in the database. Also you'll be able to check if Cake writes to the tmp files without problems
If this is helpfull for anyone in 2020.
I had this problem even with full permissions to the model. I tried doing like #theJeztah suggested which was clearing cache/persistent but the problem persisted. What ended up working was switching Configure::write('debug', 0) to Configure::write('debug', 2) in the app/core file. Presumably on debug it clears the cache better.

CakePHP 2.x: Custom Logging

I've got a CakePHP application that receives instant payment notifications from PayPal. I'd like to log the data that gets posted by PayPal. I could easily do that using something like this:
file_put_contents(LOGS . 'ipns.log', date('Y-m-d H:i:s ') . print_r($_POST, true) . "\n", FILE_APPEND|LOCK_EX);
But I prefer to do things "the CakePHP way™" whenever possible. I've already looked through the "Core Libraries > Logging" section of CakePHP's cookbook and am having trouble understanding it. I know it's not correct to do this:
CakeLog::write('ipns', print_r($_POST, true));
Although the above does seem to work, it can also cause problems, as shown here.
So what is the CakePHP way to do this? Or should I just use the raw PHP shown at the top of this question?
What you want is explained here http://book.cakephp.org/2.0/en/core-libraries/logging.html#creating-and-configuring-log-streams
But I would suggest you to read the whole page and not just this section.
I would write the ipn to a database table field by field and not into a file log. I can tell you this based on my experience with the paypal API. The advantages are obviously, you can for example lookup the ipns for an order, search for errors and so on.
According to Writing to log paragraph of Logging section of the 2.x cookbook:
CakeLog does not auto-configure itself anymore. As a result log files
will not be auto-created anymore if no stream is listening. Make sure
you got at least one default stream set up, if you want to listen to
all types and levels. Usually, you can just set the core FileLog class
to output into app/tmp/logs/:
CakeLog::config('default', array(
'engine' => 'File'
));
So, in order to make CakeLog::write('ipns', print_r($_POST, true)); to write to custom file app/tmp/logs/ipns.log you need in app/Config/bootstrap.php instead of:
/**
* Configures default file logging options
*/
App::uses('CakeLog', 'Log');
CakeLog::config('debug', array(
'engine' => 'File',
'types' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
CakeLog::config('error', array(
'engine' => 'File',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'file' => 'error',
));
write:
/**
* Configures default file logging options
*/
App::uses('CakeLog', 'Log');
CakeLog::config('default', array(
'engine' => 'File'
));

CakePHP 2.2 find returns empty data even if query matches results

I have a fresh baked (with bake console) project in which I have 2 models behaving differently when I call model->find function.
UsersController
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
WordsController
public function index() {
$this->Word->recursive = 0;
$this->set('words', $this->paginate());
}
Query (DebugKit)
SELECT `Word`.`*` FROM `words` AS `Word` WHERE `Word`.`iniziale` = 'A' AND `Word`.`pubblicata` = '1' ORDER BY `Word`.`parola` ASC LIMIT 10
Affected 10
Num rows 10
In both cases the query inspected has affected rows, but the WordsController doesn't return any results if I debug the paginate() result, while the Users one gives correctly.
Word model has no relations, and I tried to change the model name to Term, obtaining the same result.
I tried also to downgrade CakePHP core to 2.1.4. Nothing.
Are there any possible causes to this problem? Is Word some kind of reserved keyword? How can this be debugged?
I figured it out.
I had in the words table (UTF8), some texts containing special characters like àòèéìù. Cake removed all the result containing these characters. I tried replacing "è" with "e" and magically the record was available in Cake!
Hope my 5 hours of headache will help someone else!
The correct answer follows that accepted. You should set the encoding in your DB configuration:
In my conf I had:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'giuseppe',
'password' => 'pass',
'database' => 'db'
'prefix' => '',
//'encoding' => 'utf8',
);
Do you see the encoding key? Try to uncomment or set it to the correct encoding, and voila!
Quoting from a comment in the question above..
Check your model and AppModel and see whether there is any afterFind(). The afterFind call must return the $results dataset at the end.
function afterFind($results, $primary = false) {
//some code here..
return $results; //I had this commented out for some reason
//and it generated the same issue.
}

Does CakePHP finderQuery work with SQL Server? Where would I debug that?

I'm new to cakePHP, so I may be missing the obvious.
The system is running the latest download using Microsoft SQL Server 2005 as database. I appreciate that is slightly unusual, but having fixed the URL rewrite I have seen no other issues.
I'd like use a custom finderQuery, but I cannot even seem to replace the default. Specifically if I use
var $hasMany = array(
'RecyclateTypeConversion' => array(
'className' => 'RecyclateTypeConversion',
'foreignKey' => 'recyclate_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => 'select RecyclateTypeConversion.* from recyclate_type_conversions AS RecyclateTypeConversion WHERE RecyclateTypeConversion.recyclate_type_id IN ({$__cakeID__$});',
'counterQuery' => ''
),
};
I see this error
Notice (8): Undefined index:
RecyclateTypeConversion
[CORE\cake\libs\model\datasources\dbo_source.php,
line 1099]
However the SQL debug output confirms that the query itself runs fine and returns 4 records, and the view runs perfectly when the finderQuery is not specified. I've tried for other hasMany tables too - with exactly the same issue.
I've attempted to replace the select all with specific field selects but I still see the same result. Certainly the query looks correct according to the manual - so what is the issue (and could it be related to using MSSQL?)
EDIT: Also, as this hasn't picked up any answers yet, what would be the best approach to debugging this? I've started hunting through the cake debugging class, but so far with no results that have enlightened me. Of course if there is a problem I'll be submitting the fix back to the project.
Have you checked that there is actually a model called RecyclateTypeConversion and that it exists with a filename according to the CakePHP conventions? I.e. is there a models/recyclate_type_conversion.php and in that file, is the name of the model defined as RecyclateTypeConversion.
The error that you're getting seems to hint that there's something wrong with that model name, as it cannot find the associated index.
Try removing the alias from the select - the casting in in the "AS RecyclateTypeConversion" part should handle that for you. I also like to wrap custom queries in double quotes. I might just be paranoid but string parsing errors have bit me in the ass before.
var $hasMany = array(
'RecyclateTypeConversion' => array(
'className' => 'RecyclateTypeConversion',
'foreignKey' => 'recyclate_type_id',
'dependent' => false,
'finderQuery' => "select * from recyclate_type_conversions AS RecyclateTypeConversion WHERE RecyclateTypeConversion.recyclate_type_id IN ({$__cakeID__$});",
);
Also, I highly suggest you use the DebugKit plugin and post back to us the query log and a debug output of the find results that cause the errors.
did you go through it step by step?
try to get everything (all data)
try conditions
try "containable" behavior to create your query, which makes things a lot easier in my opinion
Have you tried CakePHP Debug Kit

Resources