cake migrations seed [Error] Object of class DateTimeImmutable could not be converted to string - cakephp

I use command cake bake seed --data Users to created Seeds\UsersSeep.php with the following data type:
$data = [
[
'id' => 1,
'username' => 'pitocmsssss',
'email' => 'demo123#admin.com',
'amount' => 0,
'password' => '$2y$10$kcprj5VbJlJgcJXx3U5SJuFLmnlk5kNJKrpScZ8HQO6H7O9WgEHpi',
'created' =>
Cake\I18n\FrozenTime::__set_state(array(
'date' => '2020-04-01 07:14:22.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
)),
'modified' =>
Cake\I18n\FrozenTime::__set_state(array(
'date' => '2022-08-22 04:03:58.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
)),
],
...
];
When using the command cake migrations seed to insert data, the following error occurs:
using migration paths
/var/www/html/cake_myapp/config/Migrations using seed paths
/var/www/html/cake_myapp/config/Seeds using migration paths
/var/www/html/cake_myapp/config/Migrations using seed paths
/var/www/html/cake_myapp/config/Seeds using environment default using adapter mysql using database cake_tutorial
== UsersSeed: seeding 2022-08-23 07:51:53 error: [Error] Object of
class DateTimeImmutable could not be converted to string in
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php
on line 335 Stack Trace:
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:335
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php:180
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/TimedOutputAdapter.php:102
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php:180
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:652
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:624
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Db/Table.php:682
/var/www/html/cake_myapp/config/Seeds/UsersSeed.php:216
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php:146
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php:416
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php:557
/var/www/html/cake_myapp/vendor/robmorgan/phinx/src/Phinx/Console/Command/SeedRun.php:102
/var/www/html/cake_myapp/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php:37
/var/www/html/cake_myapp/vendor/cakephp/migrations/src/Command/Phinx/Seed.php:76
/var/www/html/cake_myapp/vendor/symfony/console/Command/Command.php:298
/var/www/html/cake_myapp/vendor/symfony/console/Application.php:1024
/var/www/html/cake_myapp/vendor/symfony/console/Application.php:299
/var/www/html/cake_myapp/vendor/symfony/console/Application.php:171
how to solve this problem? Thanks?

Related

SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS on cakePHP

I get the following error on my cakePHP2.7 project when I move the code to my new server. The code works fine on all my existing servers.
we are using aws SES as mail service.
We created a sample file as instructed by https://docs.aws.amazon.com/ses/latest/dg/send-using-smtp-programmatically.html and that also works fine.
Any help is appriciated
code for your reference
public $mailarr = array(
'host'=>'email-smtp.us-west-2.amazonaws.com',
'port' => 587,
'username' => 'XXXXXXXXXXXXXXX',
'password' => 'XXXXXXXXXXX',
'tls'=>true,
'returnPath'=>'xxx#xxx.com',
'transport' => 'Smtp',
'from' => array('xxx#xxx.com' => 'Alert!'),
'emailFormat' => 'html',
'timeout' => 300,
);
I have tried to add the below as part of my mailarr but the error still persists
'context' => array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
)

CakePHP migration re-organising column order

I have added new column using migrations.
This is my new added field
$table->addColumn('lastname', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
]);
This column added after modified field. I want to change column order and want to put it after column name. How can I do it using migrations ?
You can use after in option, In adding time
$table->addColumn('firstname', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
'after' => 'name'
]);

Error: SQLSTATE[42P01]: Undefined table: "Missing FROM-claouse entry for table

Hy I create a join query using cakephp find as shown below :
$test = $this->DiagnosisBind->find(
'all',
[
'table' => 'diagnosis_binds',
'alias' => 'dbinds',
'order' => 'dbinds.set_num ASC',
//'recursive' => 0,
'conditions' => [
'dbinds.deleted IS NULL',
'dbinds.diagnosis_id ='.$diagnosisData[0][0]['id']
],
'joins' => [
[
'type' => 'INNER',
'table' => 'diagnosis_treats',
'alias' => 'dtreats',
'conditions' => [
'dtreats.diagnosis_bind_id = dbinds.id'
]
],
[
'type' => 'INNER',
'table' => 'medical_masters',
'alias' => 'medmas',
'conditions' => [
'medmas.deleted IS NULL'
]
]
],
'fields' => [
'dtreats.id',
'dtreats.medical_name',
'dtreats.amount',
'dbinds.days'
]
]
);
When executing the query, I got this error :
I have read this post missing FROM-clause entry for table "Grupo" cakephp And solution is create a recursive (I have tried but failed) and use containable (I'm not suppose to create containable). So what's wrong in my find above. Thank you
Note:
I'm using cakephp 2.0
Postgresql
There are no table and alias options for query builder finders, they are only available for joins, so your main table will use the default alias as you can see in the query in the error message, and that alias is DiagnosisBind, hence using dbinds will cause an error.
Long story short, use DiagnosisBind instead of dbinds.
Side note, never ever inject data into single value conditions (or in the key of a key => value condition for that matter):
'dbinds.diagnosis_id ='.$diagnosisData[0][0]['id']
That's a possible SQL injection vulnerability!, always use the key => value syntax, or bindings:
'DiagnosisBind.diagnosis_id' => $diagnosisData[0][0]['id']

CakePHP - Add custom values to User Object

I have added 4 more columns to my CakePHP Users table and I am trying to figure out how I can include these columns in the $this->Auth->user() object.
I've added the column information to my Users Model and Entity but still no joy. Currently my user object looks like this;
[
'id' => (int) 1,
'username' => 'admin',
'name' => 'Web Admin',
'email' => 'webteam#',
'role' => 'admin',
'created' => object(Cake\I18n\Time) {
'time' => '2016-02-09T16:04:46+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2016-02-12T08:53:16+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
]
Where is this object created and is there a way I can add my custom values to it, without editing core CakePHP files?
Thanks for your help .
By default the built-in authenticators will fetch all fields in the tables schema.
You most probably just forgot to clear your cache (tmp/cache/models), which you should do whenever you make changes to your schemas.
In case one would want to specify what fields are being fetched, a custom finder would be needed.
See Cookbook > Controllers > Components > Authentication > Customizing Find Query
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'finder' => 'auth'
]
],
]);
In your UsersTable class
public function findAuth(\Cake\ORM\Query $query, array $options)
{
return $query
->select(['id', 'username', 'password', 'column_x', 'column_y']);
}
It should be noted that the fields required for authentication must always be included, ie like username and password!

How Can I Read the DB Configuration Settings From a Cake Shell?

I would like to write a cake shell to do a nightly backup of my database using mysqldump. I could do this as a shell script, but if I can cram it into a CakePHP shell, then I will get the added benefit of it working across both the development and live server, if I can get it to automagically read my database config settings. I will cron the cake shell and have some peace-of-mind knowing that I have frequent backups of my DB.
In my shell I'm trying to build up a string which starts with "mysqldump --user=" and I'd like to get the username from app/config/database.php. How can I get at the data in database.php?
In cake 2.1 the format has changed to:
App::uses('ConnectionManager', 'Model');
$dataSource = ConnectionManager::getDataSource('default');
$username = $dataSource->config['login'];
The following snippet should do the trick:
App::import('Core', 'ConnectionManager');
$dataSource = ConnectionManager::getDataSource('default');
$username = $dataSource->config['login'];
In CakePHP 3.x the format has changed to -
use Cake\Datasource\ConnectionManager;
$source = ConnectionManager::get('default');
debug($source); #Debugging the result
Result :
object(Cake\Database\Connection) {
'config' => [
'password' => '*****',
'username' => '*****',
'host' => '*****',
'database' => '*****',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
'url' => null,
'name' => 'remote'
],
'driver' => object(Cake\Database\Driver\Mysql) {
'connected' => false
},
'transactionLevel' => (int) 0,
'transactionStarted' => false,
'useSavePoints' => false,
'logQueries' => false,
'logger' => null
}
Get the Result :
debug($source->config()); #Accessing the result
Result :
[
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'database',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
'url' => null,
'name' => 'remote'
]
Just for sharing.
For any cakephp project, if using php as cronjob or command line to do large data processing I would build a standalone php script without cakephp, the reason for doing this because cakephp is slow (e.g. read & process 100K records).
To make my life simple I put all my standalone scripts under app/Vendor/myscripts/ (e.g: app/Vendor/myscripts/process.php)
below also the basic code to make sure you use the same database settings in standalone script with cakephp (tested with MySQL)
require_once '/XYZ/app/Config/database.php';
$database = new DATABASE_CONFIG;
try{
$dblink = new PDO('mysql:host='.$database->default['host'].';dbname='.$database->default['database'], $database->default['login'], $database->default['password'], array(PDO::ATTR_PERSISTENT => false));
$dblink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dblink->exec('SET CHARACTER SET '.$database->default['encoding']);
} catch (Exception $e) {
die('DB Error'. $e->getMessage());
}
Example in controller, Change multi DB for DataSources in CakePHP 2.5.x
App::uses('AppController', 'Controller');
class DemoController extends AppController {
public $uses = array('AppModel', 'GVA21', 'GVA01', 'GVA14', 'GVA24' );
public function test_dbs(){
$this->autoRender=false;
// Load ConnectManager
App::uses('ConnectionManager', 'Model');
// DataSource ['default']
$MDM = $this->GVA14->find('count');
echo "MDM.GVA14\n<br>";
debug($MDM);
// Get DataSource Config DB ['default'] and ['SRL']
$confDeafult = ConnectionManager::$config->default;
$confSrl = ConnectionManager::$config->SRL;
// Change DataSource ['SRL']
ConnectionManager::drop('default');
ConnectionManager::create('default',$confSrl); //<== Is permanet change Find All models Down
// $this->GVA01->setDataSource('SRL'); //<== Is temp change Find model
echo "SRL.GVA14\n<br>";
$SRL = $this->GVA14->find('count');
debug($SRL);
$SRL = $this->GVA01->find('count');
echo "SRL.GVA01\n<br>";
debug($SRL);
$SRL = $this->GVA21->find('count');
echo "SRL.GVA21\n<br>";
debug($SRL);
// Change to DataSource ['default']
debug(ConnectionManager::drop('default'));
ConnectionManager::create('default',$confDeafult); //<== Is permanet change Find All models Down
//$this->GVA01->setDataSource('default'); //<== Is temp change Find model
$MDM = $this->GVA01->find('count');
echo "MDM.GVA01\n<br>";
debug($MDM);
$MDM = $this->GVA21->find('count');
echo "MDM.GVA21\n<br>";
debug($MDM);
////FIN
exit('FIN');
}

Resources