Not getting right alias name with Cakephp - mysql
I am new on CakePHP and facing a problem with SQL Alias.
I want to make an Excel file to generate a report from the MySQL database.
/**
* Generate transaction report
*
* #param none
*
* #return void
*/
public function generateReport()
{
//Getting data from the database to generate the report in Excel file
$data = $this->AchTransaction->find('all', array(
//'conditions' => $searchCondition,
'fields' => array(
"payee_name AS PAYEENAME",
'reference AS REFERENCE',
'reference2 AS REFERENCE2',
'currency AS CURRENCY',
'amount AS PAYOUTAMOUNT',
'bank_name AS BANKNAME',
'bank_branch_name AS BANKBRANCHNAME',
'bank_address AS BANKADDRESS',
'bank_country AS BANKCOUNTRY',
'account_name AS ACCOUNTNAME',
'account_class AS ACCOUNTCLASS',
'account_type AS ACCOUNTTYPE',
'bank_routing_number AS BANKROUTING',
'account_number AS ACCOUNT',
'beneficiary_address AS BENEFICIARYADDRESS',
'beneficiary_city AS BENEFICIARYCITY',
'beneficiary_state AS BENEFICIARYSTATE',
'beneficiary_postcode AS BENEFICIARYZIPCODE',
'beneficiary_country AS BENEFICIARYCOUNTRY',
'note as NOTE'
),
'order' => array('AchTransaction.id DESC')
));
// Generating Excel file
$this->Common->generateReport($data);
}
I am able to generate the Excel but having only one problem in alias, As you can see
"payee_name AS PAYEENAME",
Here the payee_name is the name of column or field in MySQL table, and I want this as PAYEE NAME, but when I write:
"payee_name AS PAYEE NAME",
I am getting this MySQL error:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'payee_name AS PAYEE NAME' in 'field list'
SQL Query: SELECT `payee_name AS PAYEE NAME`, `reference` AS `REFERENCE`, `reference2` AS `REFERENCE2`, `currency` AS `CURRENCY`, `amount` AS `PAYOUTAMOUNT`, `bank_name` AS `BANKNAME`, `bank_branch_name` AS `BANKBRANCHNAME`, `bank_address` AS `BANKADDRESS`, `bank_country` AS `BANKCOUNTRY`, `account_name` AS `ACCOUNTNAME`, `account_class` AS `ACCOUNTCLASS`, `account_type` AS `ACCOUNTTYPE`, `bank_routing_number` AS `BANKROUTING`, `account_number` AS `ACCOUNT`, `beneficiary_address` AS `BENEFICIARYADDRESS`, `beneficiary_city` AS `BENEFICIARYCITY`, `beneficiary_state` AS `BENEFICIARYSTATE`, `beneficiary_postcode` AS `BENEFICIARYZIPCODE`, `beneficiary_country` AS `BENEFICIARYCOUNTRY`, `note` AS `NOTE` FROM `aramor_payments`.`ach_transactions` AS `AchTransaction` WHERE 1 = 1 ORDER BY `AchTransaction`.`id` DESC
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
Stack Trace
CORE\Cake\Model\Datasource\DboSource.php line 461 → PDOStatement->execute(array)
CORE\Cake\Model\Datasource\DboSource.php line 427 → DboSource->_execute(string, array)
CORE\Cake\Model\Datasource\DboSource.php line 671 → DboSource->execute(string, array, array)
CORE\Cake\Model\Datasource\DboSource.php line 1082 → DboSource->fetchAll(string, boolean)
CORE\Cake\Model\Model.php line 2631 → DboSource->read(AchTransaction, array)
APP\Controller\AchtransactionsController.php line 353 → Model->find(string, array)
[internal function] → AchtransactionsController->generateReport()
CORE\Cake\Controller\Controller.php line 485 → ReflectionMethod->invokeArgs(AchtransactionsController, array)
CORE\Cake\Routing\Dispatcher.php line 186 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 161 → Dispatcher->_invoke(AchtransactionsController, CakeRequest, CakeResponse)
APP\webroot\index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Can you please help me to get the name of payee_name field as PAYEE NAME?
Change
"payee_name AS PAYEE NAME",
to
"`payee_name` AS `PAYEE NAME`"
Another option is using virtualFields
$this->AchTransaction->virtualFields['PAYEE NAME'] = 'payee_name';
array('fields' => array('PAYEE NAME', 'etc'))
Related
Unknown error from inserting data into database (MySQL)
I have not seen this error before, I tried inserting these to my database table INSERT courses SET startTime= '30-Dec-2020 09:00:00', exam_catID = 2, courseTitle = 'Test course', courseCode = 'Tcs299', duration = 3 courses Atributes startTime (Varchar) exam_catID (Int) courseTitle (Varchar) courseCode (Varchar) duration (int) Outpur Error Fatal error: Uncaught TypeError: Argument 1 passed to PhpMyAdmin\DatabaseInterface::escapeString() must be of the type string, null given, called in C:\xampp\phpMyAdmin\libraries\classes\Tracker.php on line 149 and defined in C:\xampp\phpMyAdmin\libraries\classes\DatabaseInterface.php:2991 Stack trace: #0 C:\xampp\phpMyAdmin\libraries\classes\Tracker.php(149): PhpMyAdmin\DatabaseInterface->escapeString(NULL) #1 C:\xampp\phpMyAdmin\libraries\classes\Tracker.php(841): PhpMyAdmin\Tracker::isTracked('time_table_syst...', NULL) #2 C:\xampp\phpMyAdmin\libraries\classes\DatabaseInterface.php(345): PhpMyAdmin\Tracker::handleQuery('INSERT courses ...') #3 C:\xampp\phpMyAdmin\libraries\classes\Sql.php(958): PhpMyAdmin\DatabaseInterface->tryQuery('INSERT courses ...', 256, 1) #4 C:\xampp\phpMyAdmin\libraries\classes\Sql.php(1177): PhpMyAdmin\Sql->executeQueryAndMeasureTime('INSERT courses ...') #5 C:\xampp\phpMyAdmin\libraries\classes\Sql.php(2225): PhpMyAdmin\Sql->executeTheQuery(Array, 'INSERT courses ...', false, 'time_table_syst in C:\xampp\phpMyAdmin\libraries\classes\DatabaseInterface.php on line 2991
You should use proper insert statement. You are missing INTO clause: INSERT into courses SET startTime= '30-Dec-2020 09:00:00', exam_catID = 2, courseTitle = 'Test course', courseCode = 'Tcs299', duration = 3 Or you can use the standard sql as follows: INSERT into courses( startTime, exam_catID , courseTitle, courseCode, duration) values ( '30-Dec-2020 09:00:00', 2, 'Test course', 'Tcs299', 3)
Seed script raise Invalid datetime format error
I moved my app from laravel 5 to 7 and running initial migration seed suffer error: Migrating: 2018_08_09_113432_create_vote_item_users_results_table Illuminate\Database\QueryException SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2020-03-29 03:03:20' for column 'created_at' at row 1 (SQL: insert into `vt2_vote_item_users_result` (`user_id`, `vote_item_id`, `is_correct`, `created_at`) values (5, 27, 0, 2020-03-29 03:03:20)) Which I did not have priorly 1-2 years working with this app. My table is created with migration public function up() { Schema::create('vote_item_users_result', function (Blueprint $table) { $table->increments('id'); $table->integer('vote_item_id')->unsigned(); $table->foreign('vote_item_id')->references('id')->on($this->vote_items_tb)->onDelete('CASCADE'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on($this->users_tb)->onDelete('CASCADE'); $table->boolean('is_correct')->default(false); $table->timestamp('created_at')->useCurrent(); $table->unique(['vote_item_id', 'user_id'], 'vote_item_users_result_vote_item_id_user_id_index'); $table->index(['vote_item_id', 'is_correct', 'user_id'], 'vote_item_users_result_vote_item_id_is_correct_user_id_index'); }); Artisan::call('db:seed', array('--class' => 'VoteItemUsersResultsInitData')); } after this migration I see next fields in vt2_vote_item_users_result table of the db : id int(10) unsigned Auto Increment vote_item_id int(10) unsigned user_id int(10) unsigned is_correct tinyint(1) [0] created_at timestamp [CURRENT_TIMESTAMP] and running initial seed I fill field created_at manually as I need this date to be withing some range, not only at date of creation: $usersList = User::all(); $votesList = Vote::all(); $faker = \Faker\Factory::create(); foreach ($usersList as $nextUser) { foreach ($votesList as $nextVote) { $voteItemsList = VoteItem::getByVote($nextVote->id)->orderBy('ordering', 'asc')->get(); if ( count($voteItemsList) > 0 ) { $random_index = mt_rand(0, count($voteItemsList) - 1); if ( ! empty($voteItemsList[$random_index]->id)) { if ( $voteItemsList[$random_index]->is_correct and in_array($nextVote->id,[1,2]) ) continue; if ( !$voteItemsList[$random_index]->is_correct and in_array($nextVote->id,[3,4]) ) continue; DB::table('vote_item_users_result')->insert([ 'user_id' => $nextUser->id, 'vote_item_id' => $voteItemsList[$random_index]->id, 'is_correct' => $voteItemsList[$random_index]->is_correct, 'created_at' => $faker->dateTimeThisMonth()->format('Y-m-d h:m:s'), // I fill created_at manually ]); } } } // foreach ($votesList as $nextVote) { } // foreach ($usersList as $nextUser) { Running sql from error as : insert into `vt2_vote_item_users_result` (`user_id`, `vote_item_id`, `is_correct`, `created_at`) values (5, 27, 0, '2020-03-29 03:03:20') I got error : Error in query (1292): Incorrect datetime value: '2020-03-29 03:03:20' for column 'created_at' at row 1 I do not see why error ? The sql-statement seems valid... Mysql 5.7.29-0ubuntu0.18.04.1 Laravel Framework 7.6.2 PHP 7.2.24-0ubuntu0.18.04.4 Thanks!
Argument 2 passed to App\Http\Controllers\
Why am I getting the following error? ErrorException in AdsController.php line 69: Argument 2 passed to App\Http\Controllers\AdsController::pick_campaign() must be an instance of App\Websites, null given, called in /home/getad/dashboard.getad.ir/app/Http/Controllers/AdsController.php on line 256 and defined line 69: 'protected function pick\_campaign(IlluminateHttpRequest $request, AppWebsites $website, $campaign\_tbl, $params = array( ))' line 256: ' $eligible = $this->pick\_campaign($request, NULL, $campaign\_tbl, $p);' Error 2 'QueryException in Connection.php line 631: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'details' at row 1 (SQL: insert into transactions (user, type, amount, details, date) values (1, click, 8, a:1:{s:5:"click";O:10:"AppClicks":22:{s:8:"_table";s:6:"clicks";s:10:"timestamps";b:0;s:11:"_fillable";a:9:{i:0;s:5:"token";i:1;s:8:"campaign";i:2;s:6:"banner";i:3;s:7:"website";i:4;s:2:"ip";i:5;s:4:"date";i:6;s:4:"fake";i:7;s:2:"os";i:8;s:7:"browser";}s:13:"_connection";N;s:13:"_primaryKey";s:2:"id";s:10:"_perPage";i:15;s:12:"incrementing";b:1;s:13:"_attributes";a:10:{s:5:"token";i:814927090;s:6:"banner";N;s:8:"campaign";s:2:"13";s:7:"website";s:2:"20";s:2:"ip";s:15:"109.125.175.129";s:4:"date";O:8:"DateTime":3:{s:4:"date";s:26:"2019-06-02 15:12:32.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:11:"Asia/Tehran";}s:4:"fake";i:0;s:2:"os";s:7:"Windows";s:7:"browser";s:6:"Safari";s:2:"id";i:2464;}s:11:"_original";a:10:{s:5:"token";i:814927090;s:6:"banner";N;s:8:"campaign";s:2:"13";s:7:"website";s:2:"20";s:2:"ip";s:15:"109.125.175.129";s:4:"date";r:25;s:4:"fake";i:0;s:2:"os";s:7:"Windows";s:7:"browser";s:6:"Safari";s:2:"id";i:2464;}s:12:"_relations";a:0:{}s:9:"_hidden";a:0:{}s:10:"_visible";a:0:{}s:10:"_appends";a:0:{}s:10:"guarded";a:1:{i:0;s:1:"";}s:8:"_dates";a:0:{}s:13:"_dateFormat";N;s:8:"_casts";a:0:{}s:10:"_touches";a:0:{}s:14:"_observables";a:0:{}s:7:"_with";a:0:{}s:13:"_morphClass";N;s:6:"exists";b:1;}}, 2019-06-02 15:12:32))'
How to code a complex array of hashes in Perl?
Here is a task that's puzzling me for quite a while now: Regularly, we get exam results of our students as a .csv file. The header has some meta data such as ID, gender, date of birth, status, exam room, seat number, exam version, and the score for 60 questions (0.0 0.5 1.0 points if the answer is wrong, half-correct, or correct). There are 6 versions (A - F) of the exam differing only by the order of the 60 questions. The information is stored for statistical evaluation which requires the correct alignment according to the exam master (a .txt file with 7 columns for version A-F and the correct answer in the 7th column). I tried to accommodate the .csv file as an array of hashes to generate a different .csv or tabbed .txt file in which all exam results appear in a unified order for later statistical evaluation. Example: header -- ID,gender,birthdate,order,room,seat,version,points,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 277710814533,f,01/02/1993,m,sr_3,A11,A, 1,1,1,1,0,1,1,1,.5,1,1,1,0,1,.5,1,1,1,0,1,.5,1,1,0,1,1,1,1,1,1,1,0,0,1,0,1,.5,1,1,1,1,.5,0,1,1,1,0,1,1,1,1,1,0,1,1,1,.5,1,1,1 755310765962,f,31/07/1992 00:00,v,aula,C11,C,1,.5,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,.5,1,0,.5,1,0,1,.5,0,.5,0,1,0,0,.5,1,1,0,.5,1,1,.5,.5,1,.5,.5,1,1,1,.5,.5 394610513538,m,20/10/1992 00:00,m,sr_3,E13,E,1,1,0,.5,1,1,1,1,1,1,1,.5,1,1,.5,.5,1,1,1,.5,.5,1,1,1,1,0,0,.5,1,1,.5,.5,.5,.5,0,1,0,.5,0,0,1,0,1,.5,0,1,0,0,.5,1,0,1,1,0,.5,.5,.5,.5,.5,.5 The code generates hash keys according to the following scheme: while ( <FH> ) { chomp ; if ( /^\d\d\d/) { ( $id , $gender , $birthday , $status , $room , $seat , $version , #points ) = split ( /,/ , $_ ) ; $student = { 'id' => $id , 'gender' => $gender , 'birthday' => $birthday , 'position' => $position , 'room' => $room , 'seat' => $seat , 'version' => $version , 'points' => #points } ; push ( #candidates , $student ) ; } } ; close FH ; print "Number of candidates processed: " . ( $#candidates + 1 ) . "\n" ; The compiler throws a warning for each record, e.g. "Odd number of elements in anonymous hash at /Documents//testAoH.pl line 38, line 16." but the script is executed. The script prints the correct number of processed records, but when I try to retrieve a specific record I only get the scalar values and the #points array yields only one (the first?) result as if it were destroyed. A data dumper output further shows that something must be internally wrong with this code. Data Dumper e.g. 755310765962 $VAR1 = \{ '0' => '0', 'gender' => 'f', 'id' => '755310765962', 'points' => '1', 'room' => 'aula', '.5' => undef, '1' => '.5', 'birthday' => '31/07/1992', 'seat' => 'A11', 'version' => 'A', 'status' => 'v' }; Any clues?
Use \#points. #points expands in the hash constructor to generate: 'Points' => $points[0], $points[1] => $points[2], ...
Getting Error when using POST Orders on Shopify
I'm trying to post a new order which comes from another marketplace. I have all the correct information, but for some reason the POST is not accepting or recognizing the variant_id Im sending. It always returns an error saying I'm missing name, price, and title. I'm not sure why this comes. Here is the array Im sending: Array ( [order] => Array ( [line_items] => Array ( [0] => Array ( [id] => 2147483647 [quantity] => 1 [price] => 609 ) ) [customer] => Array ( [first_name] => Jurgen [last_name] => Feuchter Garcia [email] => mail#gmail.com ) [note] => /// ***** ORDEN MERCADOLIBRE ***** /// ID DE ORDEN ML: order_number [financial_status] => pending [tags] => Array ( [0] => Orden MercadoLibre ) ) ) And here is the response: Array ( [errors] => Array ( [order] => Array ( [0] => Line items is invalid ) [line_items] => Array ( [0] => Name can't be blank [1] => Title can't be blank ) ) ) I've tried using the value name variant_id instead of id, and it asked me for the same information. Any ideas why this might be happening?
I actually found out what was wrong, the variant_id was wrong. Apparently if the variant_id is wrong, it tells you that name and title is missing, and if you have it right, it mentions that price line is missing. This is what happened to me. Not totally sure if it works like that, but I got it to work using the correct variant_id and adding pricing for the product.
You can't add a price like that and mind that the id pertains to a variant id and not the product id. Your variant with id 214748364 should already be priced before hand in the Shopify admin dashboard (backend).
Error 1 is displayed because Price is not part of line_items. Error 2 is however strange because, Name and Title are not compulsory parts of line_items. For e.g. below example would create an order successfully. POST /admin/orders.json { "order": { "email": "foo#example.com", "fulfillment_status": "fulfilled", "send_receipt": true, "send_fulfillment_receipt": true, "line_items": [ { "variant_id": 447654529, "quantity": 1 } ] } }