ZF2 - Record not existing - database

I try to use the zf2 db validator to check if a record does not exist.
I set the follow code in my controller:
$validator = new NoRecordExists(
array(
'table' => 'topics',
'field' => 'topic',
)
);
after this configuration I try to validate but I got 'an error occurred', can't find any other error output who can tell me more about this error.
var_dump($validator->isValid('test'));
If someone can give me some tips, would be great :)
thx

Have you set the DB Adapter?
You can pass it in the contructor or called setAdapter();
validator->setAdapter(
$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
);
You could set this up in your service config to automatically inject the adapter for you.

Related

MissingRouteException in test of Table Object

I have a TableObject that sends an email in the afterSave-Event to notify the admin of a change. After the switch from cakephp-3 to cakephp-4 the test for this fails with this error message:
Cake\Routing\Exception\MissingRouteException: A route matching ... could not be found.
The exception occurs on a line in the email template where I build a link like this:
$this->Url->build([
'prefix' => 'Partner',
'controller' => 'orders',
'action' => 'view',
$order->id,
]);
I believe that the routes are not set up in the context of a test for a Tableobject and therefore the reverse routing is not working. (I only get the error when running the test, not when the email is sent in the app).
Is there a way I can load all routes in the test?
Since 4.x the routes are not auto loaded anymore.
You need to add
$this->loadRoutes();
into your setUp() or before the test run to actively load them now.

How to connect to a database without the config file in Yii2?

I am using, for the same problem, the code which is given in this question Yii2 Create Database Connection.
I realise the $config variable is no longer the one in the web.php file, from the "config" folder, and that he is changing $configin his Configuration::setConfig() function.
My question for those more experienced than me in Yii is what should I write
in the web.php file in the db field (or in the db.php file) to "create a database connection programmatically without using the config file" ?
in the function Configuration::setConfig() to properly configure the application?
I'm sorry if my question is not clear enough. Please ask for details in the comments if needed. Thank you!
You can define a new connection this way
$db = new yii\db\Connection([
'dsn' => 'mysql:host=localhost;dbname=example',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]); and
$db->open();
After the DB connection is established, one can execute SQL statements
like the following eg: :
$command = $db->createCommand('SELECT * FROM post');
$posts = $command->queryAll(); or
$command = $connection->createCommand('UPDATE post SET status=1');
$command->execute();
you can look at this for doc and guide
http://www.yiiframework.com/doc-2.0/guide-db-dao.html
http://www.yiiframework.com/doc-2.0/yii-db-connection.html

Cakelog is writing to multiple files, but I want it to write to only the one I specify

I want to log payment gateway errors in payment.log. So add this to bootstrap:
CakeLog::config('payment', array(
'engine' => 'FileLog',
'file' => 'payment',
));
and when a problem occurred:
CakeLog::write('payment', 'The is a problem!');
but, the above command will log This is a problem! in both payment.log and error.log, while log into first file is enough.
Also, if any other problems occurred in other controllers (like users), it will log into both files. while it should just log in error.log
(I mean payment errors should log into payment.log and any other problems should write into error.log)
Where's the mistake?
Thanks.
It looks like you are missing the scopes option in the config maybe?
CakeLog::config('payments', array(
'engine' => 'File',
'scopes' => array('payment', 'order')
));
Then you need to specify the scope where you want to write to:
CakeLog::write('warning', 'Stuff is broken here', 'payment');
It may take some fiddling to get it just right, but this should help.

CakeDC User Plugin - Is there Documentation Anywhere?

Browsing through GitHub and I found a pretty powerful CakePHP plugin called CakeDC Users that has a lot of features (Account verification, password reset, etc) for a creating a login/authentication system. I like it because it seems to be written by some of the actual CakePHP developers and it gets updated a lot but there seems to be absolutely zero documentation anywhere on it. I've just come across this plugin recently, since I was trying to see if there's a better way than "rolling" with my own solution. So I was wondering if anybody here has had experience with it and if so could point to some decent documentation online.
Edit There is some stuff at the bottom of the readme, but it hasn't been too intuitive for me.
Alternate question, if you don't use this plugin, is there a login/authentication plugin you use in CakePHP that you use for login/authentication?
I have ran into the same problem with using the CakeDC plugins, a lot of them have little/no documentation.
However, there is not "Zero" documentation for it, you can see how to set it up for the most part at the bottom of the github page in the read me. Also you need to put this inside your AppController::beforeFilter() method.
$this->Auth->authorize = 'controller';
$this->Auth->fields = array('username' => 'email', 'password' => 'passwd');
$this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false);
$this->Auth->loginRedirect = '/';
$this->Auth->logoutRedirect = '/';
$this->Auth->authError = __('Sorry, but you need to login to access this location.', true);
$this->Auth->loginError = __('Invalid e-mail / password
combination. Please try again', true);
$this->Auth->autoRedirect = true;
$this->Auth->userModel = 'User';
$this->Auth->userScope = array('User.active' => 1);
if ($this->Auth->user()) {
$this->set('userData', $this->Auth->user());
$this->set('isAuthorized', ($this->Auth->user('id') != ''));
}
Also, you need an isAuthorized() function, something as simple as this will do:
public function isAuthorized() {
return true;
}
Additionally, you will need to allow the 'login' action (this will involve editing the plugin files). Just add 'login' to the $this->Auth->allow() in users_controller.php.
This question is pretty old now, but as it's not marked as resolved and we've been doing a lot on the documentation since then I think it's worth to update:
Documentation can be found here:
For the version 3+ of the framework
https://github.com/CakeDC/users/blob/master/Docs/Home.md
Tutorial > http://www.cakedc.com/jorge_gonzalez/2016/02/21/cakedc_users_plugin_for_cakephp_3_-_update
CakePHP Facebook login tutorial >
http://www.cakedc.com/jorge_gonzalez/2016/02/21/cakephp_facebook_login_using_cakedc_users_plugin_-_update_3_1_5
For the (old) version 2
https://github.com/CakeDC/users/blob/2.x/Docs/Home.md
After exhaustive search I found a tutorial on how to use CakeDC!
Here it is

Saving data from another model (my own log system)

I´m trying to write a log system for my CakePHP intranet. What i need is to store de username/datetime whene someone tryes to login.
My login code is on the clients_controller and i need to store the data on a model called log (i have the model, controller, view... but they are not related)
how can i achive that?
thank you
Add this line in your Client Controller:
$var uses = array('Client', 'Log'); // all the model that is used
// by the client controller.
in cakephp 2.3
public $uses = array('Client', 'Log');
To create a log when someone logs in:
$this->Log->create();
$log = array('Log' => array(
'username' => $username,
'datetime' => date('Y-m-d H:i:s')
));
$this->Log->save($log);

Resources