Doctrine get result issue - mysql

I got DQL like this:
$sql =$qb->select('c')
->from('Cusomter','c')
->where('c.login = :login')
->setParameter('login',$login);
$rs = $sql->getQuery()->getResult(Query::HYDRATE_OBJECT);
But I got the following error
Fatal error: Class 'customer\Query' not found in
MyNameSpace\customer.php
Do i need to use any namespace?
Thanks.

you dont need to actually tell getResult to hydrate_object, its default behaviour, and therefore called from the method itself.
try:
$rs = $sql->getQuery()->getResult();
if you want to know the right namespace for the constant though its:
Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT

Try:
use Doctrine\ORM\Query;
or
\Doctrine\ORM\Query::HYDRATE_ARRAY

Related

Trouble with AND operator on Eloquent Raw query MySQL JSON

I am trying to figure out the correct syntax to add an AND operator to the following Eloquent Raw query in which I am querying a MySQL (5.7.9) table's JSON field. In doing so, I would like to be able to have case insensitivity capabilities.
After doing initial research as to how to achieve this, I have my code working in a basic way like this:
$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?)', ["%{$user_name}%"])
But my goal is to add an AND operator to narrow down my results further with a non-JSON varchar column.
I have tried this (and other variations without success):
$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?)', 'and user_type = admin', ["%{$user_name}%"])
Which gives me an error:
"Array to string conversion"
I have also tried:
$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?) and user_type = admin', ["%{$user_name}%"])
Which give me the following error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_type' in 'where clause'"
Other similar variations that have not resulted in errors have yielded too many or too few results.
How can I successfully adjust my syntax to query an additional non-JSON field with the correct results? Also, is doing a raw query like this the most effective way to do so with Eloquent, MySQL (5.79) and Laravel 5.6?
Thank you for any and all help/direction offered! :)
I think you should do something like
$users = User::where(DB::raw('lower(info_json->"$.full_name")', 'like', DB::raw('lower(%{$user_name}%)'))
->where('user_type', '=', 'admin')
Probably I have some sintax error but main idea is to use DB::raw to help you with your query.
I figured it out. My syntax should have looked like this:
$users = User::whereRaw('lower(info_json->"$.full_name") like lower(?)', ["%{$user_name}%"])->where('user_type', '=', 'admin')->get();
Thanks to those who assisted with this! Like a lot of syntax/coding problems, taking a little break from it helped me to figure it out quite quickly when I returned to it.

error warning message when i try to log on to a page using php and mysql

$result = mysql_query($db, $query);
if (mysqli_num_rows($result) ==1)
Error message: "mysql_query() expects parameter 1 to be string, object
given in C:\xampp\htdocs\registration\server.php on line 65"
"mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
in C:\xampp\htdocs\registration\server.php on line 66"
Can anyone please help me not getting those two errors, I'm trying to log on to a page but I'm really not familiar with the errors above
mysql pass connection object second
write mysql_query($query,$db); instead of mysql_query($db, $query);
and you write mysqli_query for count row and use mysql_query for get result
so change mysqli_fetch_rows to mysql_fetch_rows
so its work
code like below
$result = mysql_query($query,$db);
if (mysql_num_rows($result) ==1)
or using mysqli
$result = mysqli_query($db,$query);
if (mysqli_num_rows($result) ==1)
Change it to
syntax mysql_query(query, connection)
$result = mysql_query( $query,$db);
The mysql_query($query, [$link]); documentation can be found here.
So you would need mysql_query($query, $db);
Part of your issue is that you're also using mysql_query with mysqli_num_rows, and you need to be consistent.
That being said, mysql_* functions are deprecated and should not be used in new code (and replaced in old). If you want a simple transition, mysqli_* is an acceptable replacement, which supports parameter binding and other more secure and conventional methods. That documentation can be found here.
A tutorial for mysqli_* or PDO (preferred) can be found all of the place, but a decent one is here.

how to get Even/Odd id numbers from a database table in codeigniter

I am new in codeigniter and just stacked in a query to solve a report for an emergency project. Please help me Codeigniter's Experts.
I have a large database table and wants to show only Odd/Even Data rows from that table which will filtered by a table Field named is "sale_id". I tried it in PHPMyadmin in raw coding and it's worked for me. But can not apply in Codeigniter.
SELECT * FROM ospos_pak_sub_cat WHERE id %2 =0;
Worked for me in raw PHP Coding. How can I use it in Codeigniter. I used a Where Condition already on that query and now want to add the new query.
Existing Where condition is given below, which is working fine.
$this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
It is working and I tried the code below to get the solution which is not working and getting error.
$this->db->where('sale_id %2'=> 0);
Getting error with this line. says--
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
Please help me to get the solutions. Thanks in Advance.
In your Model, just write the query like that:
$this->db->select('*');
$this->db->from('ospos_pak_sub_cat');
$this->db->where('sale_id %2=', 0);
$query_result = $this->db->get();
$result = $query_result->result();
You missed the '=' in your code. Hope, it will work.
in SQL the % character is a wildcard rather than a modulo which would explain your error. you can use the MOD function instead http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod
so the resulting code would be :
$this->db->where('MOD(sale_id ,2) => 0');
I am not sure but i think it will work try this line
$this->db->where('sale_id %2',0);
As you mentioned, you can add the new condition in existing where condition as like below
$this->db->where('(sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].')" and ((sale_id % 2) = 0)');

get sql query from collection model

I know that we can get sql query from magento model collection using this,
getSelect();
But I can get query from only the model collections, not worked in others or May be I dont know how to use it.
Here I want to know what query is running behind this,
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("28");
}
If I use this,
echo $productModel->getSelect(); exit;
I'm just get the one part the query only, like,
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Update:
This is my full code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
$color_label = $attr->getSource()->getOptionText("28");
}
$productModel->printlogquery(true);exit;
Please help me guys,
Your order condition is not visible in the query. The reason your order isn't showing is because the orders are added to the query during the load() method.
See Varien_Data_Collection_Db::load()
Try calling load(true) to see the complete SQL containing the order by clause.
$productModel->load(true);
$productModel->printLogQuery(true);
Hope it helps.
If you want to see what is the exact query, then you can get this by using:
$productModel->printlogquery(true);exit;
Use this code after you have loaded your model's object and applied all conditions.
I hope this will help you.
Magento collecting data with lot of internal queries - models and lot of checks, and may be more than 1 table. So it is not possible to get the query like what I'm looking for.

Class 'Silex\Provider\PDO' not found. How to solve?

I'm using Silex as well as Doctrine. It's worked great for me for everything until I needed to bindValue for a LIMIT value. PDO's default behavior is to insert quotes around the number, which is obviously not workable. So, the solution is to set the data_type parameter. Unfortunately, it throws an error.
My Code
$start_num = 3;
$stmt = $app['db']->prepare('SELECT * FROM myTable LIMIT ?,10');
$stmt->bindValue(1, $start_num, PDO::PARAM_INT);
The Error
Fatal error: Class 'Silex\Provider\PDO' not found in ...
Most answers I've found regarding this issue say that it's a telltale sign of PDO not being compiled/enabled, however I've been using Doctrine (which relies on PDO?) successfully for a while with no problem.
Is this an issue with Doctrine? Is there something I'm doing wrong with my code?
This is a namespacing issue, if this code is in a class under the Silex\Provider namespace
Try
\PDO::PARAM_INT