Doctrine Raw SQL not calling custom function - php

I'm having an issue calling an custom defined function in my database from Doctrine when using the Raw SQL.
Here is an example SQL Query that is being run
SELECT unicodeDecoder(answer) from answers;
unicodeDecoder is a custom defined function in my database and runs perfectly fine when I run the SQL statement directly on the database.
However when I run the query using raw sql, as follows:
$sql = "SELECT unicodeDecoder(answer) from answers";
$stmt = $this->getEntitityManager()->getConnection()->prepare($sql);
$stmt->execute();
I get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION unicodeDecoder does not exist
Do I need to create a custom ORM mapping when using custom functions or could this be some form of caching issue?
Any help would be massively appreciated.
Thanks in advance

Try to prefix the function with database name:
$sql = "SELECT my_db.unicodeDecoder(answer) from answers";

If found the issue. Thank you to #alex-blex for giving me a point of somewhere to start looking.
My database has a . in the name: cms.app.com
The issue was caused by the .
When Doctrine tries to access the method it automatically appends the database name to the beginning.
When I attempted to add the cms.app.com to the beginning (as suggested by #alex-blex) it created the following method call (which is the wrong syntax)
cms.app.com.unicodeDecoder()
I attempted the following
`cms.app.com`.unicodeDecoder but this still resolved to
cms.app.com.unicodeDecoder()
I removed the . from the database name and it now works as expected.
Important Note: Do not use . in your database name as Doctorine doesn't resolve the name correctly as the ` symbol get stripped out.

Related

Laravel - Where Condition on Multiple Database

I'm using laravel with multiple DB connection. Oracle and PostgreSQL. I can do query from both of instance.
But the problem is I have to look inside my Oracle DB WHERE NOT EXISTS on my postgreSQL.
This is my current Query :
DB::connection('ora')->table('ora_purch')
->whereExists(function($query)
{
$query->select(DB::connection('pgs')->raw(1))
->from('pg_purch')
->whereRaw('ora_purch.id = pg_purch.id');
})
->get();
From this query, I get error "database pg_purch is not exists". It's just like laravel reads pg_purch as an oracle instance.
I also do simulation with the same query and the same instance (multiple database on postgreSQL only), the query is fine and produces correct data.
Is it possible to makes the laravel reads pg_purch refer to the connection?
Or maybe i've missing something?
Please advise, thank you.
Your code generates query like this:
SELECT * FROM ora_purch WHERE EXISTS (SELECT 1 FROM pg_purch WHERE ora_purch.id = pg_purch.id);
And make it with "ora" connection, ignoring "pgs" connection.
You can't use multiple connections inside one query.
Try to split them:
$ids = DB::connection('pgs')->from('pg_purch')->pluck('id');
$result = DB::connection('ora')->from('ora_purch')->whereIn('id', $ids)->get();

Why suddenly I started getting No query results for model error in Laravel?

I don't know why I started getting the following error while working on Laravel application.
No query results for model [App\Hotspot].
Here is my Model Function to get user's hotspots.
public function hotspots()
{
return $this->hasManyThrough(Hotspot::class, Operator::class, 'id', 'operator_id');
}
and here is how I am executing the query to get data.
$hotspotId = $id;
$hotspot = Auth::user()->hotspots()->findOrFail($hotspotId);
I started getting this error suddenly. I don't know what went wrong! I tried to find solution on internet but they are totally different cases.
As shown in https://laravel.com/docs/5.4/eloquent-relationships, section Has Many Through.
return $this->hasManyThrough(Hotspot::class, Operator::class, 'user_id', 'operator_id', 'id');
This is necesary due to you are first connecting the Hotspots to he User, after that you are connecting he operations to the Hotspots. therefor his order is correct.
Reason for it working certain times, is because of id clashes, if you used Uuids this code would never work. You can also debug this solution with he following.
DB::enableQueryLog();
// General i feel like Auth::user()->hotspots->where('id', $hotspotId)->first() is more Laravel'sh.
// Like this you will query a lot, if used in wrong cases.
$hotspot = Auth::user()->hotspots()->findOrFail($hotspotId);
dd(DB::getQueryLog());
Or you can get the raw sql, sometimes provides you with a different view.
dd(Auth::user()->hotspots()->toSql());

Retrieve data from database in Laravel 5.0

I have a table in my database called modules so in my controller I write like this to retrieve datas from the table
$allModuleNames = DB::table('users')->get();
return view("BaseView.home")->with('$allModuleNames',$allModuleNames);
But for some weird reason I am getting syntax error, unexpected ';' error on the query like. This must be silly I guess,not able to pass through this. Can someone help?
I have added this to the header
use Illuminate\Support\Facades\DB;
And when I hover over table I get method table not found in \illuminate\Support\Faces\DB
Use modules intead of users :
$allModuleNames = DB::table('modules')->get();
and you dont need the $ on this line :
return view("BaseView.home")->with('allModuleNames',$allModuleNames);

Selecting MySQL views using $wpdb class

I am building a Wordpress site and it is connected to a MySQL database. I am using the wordpress class wpdb (https://codex.wordpress.org/Class_Reference/wpdb) to interact with the database. With that class, I am able to query TABLES of my database, but not Views.
I need to be able to select Views of my database. Is this something that is not allowed with wpdb, or is my code just wrong? Is there a way to query views the same way I can query tables using wpdb?
I have tried using the query function, as well as treating a view the same way I treat a table, but it does not work. It returns empty.
Query method:
$test = $mydb->query(
$mydb->prepare(
"
SELECT name FROM $mydb->$view_name
WHERE id = 1"
)
);
echo $test; //returns empty; should return a name
Table method:
$test = $mydb->get_var(
"select name from $view_name WHERE id = 1"
);
echo $test; //returns empty; should return a name
Any suggestions? Am I able to connect to my database using something other than $wpdb (does Wordpress allow that?).
name seems to be a reserved word in MySQL (reference). Try surrounding it with back-ticks (`) like this:
SELECT `name` FROM ...
You should be seeing some errors, do you keep an eye on the logs? Also, when you have doubts in your queries, you can simply copy the raw query and execute it into phpMyAdmin or whatever tool you are using to access your database manually

Weird alias [0_ added by doctrine while trying to run with SQL SERVER

I'm trying to implement APYDataGridBundle on Symfony, with SQL Server. Symfony throws in this exception:
SQLSTATE[HY000]: General error: 105 General SQL Server error: Check messages from the SQL Server [105] (severity 15) [SELECT TOP 50 [0_.PI_PK AS PI_PK0, [0_.ProductName AS ProductName1, [0_.ProductDetails AS ProductDetails2 FROM [TSOFT_LEARN].[dbo].[tblProductDemo] [0_]
I tried:
$repo = $em->getRepository("ProductOrderLookupBundle:Product");
$product = $repo->findAll();
and everything worked fine, but it breaks for over 1 million records. Someone suggested to me to use APYDatagridBundle like here. I have tried ThraceDataGrid Bundle before and it gave me this same problem.
If I remove the "[0_" and everything worked fine while running the query on SQL Server.
Can anybody tell me what might be the problem?
Doctrine always create alias for tables in the generated queries. I always had the same kind of queries, i don't know if it can be disabled or not but better not because i don't know how it will react for joins , what you can do is to add a function in your repository :
public function getAllProducts(){
$qb = $this->_em->createQueryBuilder()
->select('partial p.{ /*add all fields needed separated by coma optimized for big queries get only whats needed */ }')
->from('Product', 'p');
$results = $qb->getQuery()->getResult();
return $results;
}
**SOLVED:**The problem was with how I defined my table names in the doctrine entities. I gave the name
[TSOFT_LEARN].[dbo].[tblOrders]
and it should be
TSOFT_LEARN.dbo.tblOrders
Because of that my alias was becoming [0_ instead of t0_. I suppose its how doctrine creates its aliases by taking the first character of the table name. Since the table name was starting with "[" the alias got converted to [0_.

Categories