Magento query __toString() like Zend - magento

i can return this like a string?
Similar Zend Framework ->__toString()
here i select my categories (just name) when is active...
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('is_active', 1);
i want see that like string sql query "SELECT * ..."

(string)$categories->getSelect();
You have to use the getSelect() function and cast the results of that to a string. It will make use of the aforementioned __toString() function and return the results of the query.

Related

I would like to check a SQL query of Laravel builder

Method Illuminate\Database\Eloquent\Collection::toSql does not exist.
occured when I tried this
My laravel version 6.20
$query = Car::where($criteria)
->get(['id'])->shuffle();
$sql = $query->toSql();
Log::info($sql);
When you execute the query using get() it will return an instance of Illuminate\Database\Eloquent\Collection class. And there's no method named toSql() on the Collection class.
If you want to inspect the sql you should
$query = Car::where($criteria)->select('id');
//Here $query is an instance of Illuminate\Database\Eloquent\Builder
//And it has a method named toSql
$sql = $query->toSql();
Log::info($sql);
$query->get()->shuffle();
You could use dd or dump methods for debugging purposes.
Source: https://laravel.com/docs/6.x/queries#debugging

Laravel DB::select returns General error: 2031

Laravel DB::select returns
General error: 2031
It looks like quotes are not added to the statement and it tries to execute the statement like this:
$sql = 'SELECT id FROM users where email=email#email.com';
The code looks like this:
$sql = 'SELECT id FROM users where email=?';
$results = DB::select($sql, ['email' => $email]);
I thought this is handled automatically by PDO but do I need to add anything to the code above so the statement looks like?
$sql = 'SELECT id FROM users where email="email#email.com"';
I recommend you to read the select docs from Laravel Query Builder.
The Query Builder provides a complete API to make it easier and safer. Your query would look like:
$results = DB::table("users")->select("id")->where("email",$email);
EDIT
The problem is that you're trying to pass a named parameter, but inside the string you didn't named it.
As the doc example shows:
$results = DB::select('select * from users where id = :id', ['id' => 1]);
Your query should look like:
$sql = 'SELECT id FROM users where email=:email';
$results = DB::select($sql, ['email' => $email]);
#mthrsj has correctly identified a better solution in your particular query.
Your underlying issue with the DB::select call is you're using non-named placeholders ? while passing it named data.
This should work:
$sql = 'SELECT id FROM users where email=?';
$results = DB::select($sql, [$email]);
as should this:
$sql = 'SELECT id FROM users where email=:email';
$results = DB::select($sql, ['email' => $email]);
If you have models setup, you can also call the DB using your User model:
$user = User::select('id')->where('email', $email)->get();
This uses Eloquent, which uses PDO.

Pass specific id to model and take data from database using codeigniter?

My controller method like below
public function index($book_id) {
print_r($book_id);
}
I will get the id from view. View like below
I need to get specific row according id on model how can I do that if I use query in controller its not working
If I use like below in controller it gives something else as result
public function index($book_id) {
print_r($book_id);
$this->db->select('*');
$this->db->from('books')->where('book_id', $book_id);
$query = $this->db->get();
print_r($query);
}
but if I use same query in model with hard coded id for testing it gives the expected out put
Please help me with this
You don't need to include "/index" in anchor tag href as controller default method will be index if found. And "print_r($book_id)" should be "echo $book_id" as $book_id is variable not array.
Please try this code (either in model/controller):
$query = $this->db->get_where('books', array('book_id' => $book_id));
$result = $query->row_array();
print_r($result);

Laravel Eloquent ORM eager loading. Relation incorrectly returned as null

I have an Eloquent ORM relationship defined as follows:
ProductConfiguration:
public function product()
{
return $this->belongsTo('Excel\Products\Product');
}
public function currency()
{
return $this->belongsTo('Excel\Currencies\Currency');
}
Product
public function productConfigurations()
{
return $this->hasMany('Excel\Products\ProductConfiguration');
}
public function productType()
{
return $this->belongsTo('Excel\Products\ProductType');
}
I expect that if I do the following that I will load all product configurations of a specified product type, with the related products, nested product type details and the product configuration currency
$results = ProductConfiguration::with(
array(
'product' => function($query) use ($product_type) {
$query->where('product_type_id' , $product_type);
},
'product.productType',
'currency'
)
)
->get();
however the returned collection has 'product' set to NULL. the Currency Relationship is there, but the product relationship is not. I can see the outputted SQL queries and the query that selects the products retrieves the correct products if I paste it directly into my sql editor
select * from `products`
where `products`.`id` in ('12', '13')
and `product_type_id` = '1'
Am I correct to think that the results from this query should be included in my collection, or is there some obvious flaw in my thinking?
I think you don't want to achieve that. Now what you get is getting all ProductConfiguration with products that are only of certain_type.
So in case you have some configuration that has other type for product you will get null because you limited results from product to only the one that has certain product type.
I might be wrong, but you probably wanted to get those ProductConfiguration that belongs to Product that is type of certain_type. In this case you should use whereHas:
$results = ProductConfiguration::
with('product', 'product.productType', 'currency')->
whereHas('product', function($q) use ($product_type)
{
$q->where('product_type_id', '=', $product_type);
})->get();
I hate to post this as an answer but since i don't have enough rep to comment so try this first:
$results = ProductConfiguration::with('product')->get();
dd($results->toArray());
See what you get, if you get some data, try this
$results = ProductConfiguartion::with(array('products' => function($query){
$query->where('product_type_id' , $product_type);
})->get();
dd($results);
See what you get, if you get null: your $product_type variable may be something you didnt expect, so try dd($product_type) to make sure its what your expecting.

Codeigniter: does $this->db->last_query(); execute a query?

Does query execution happen at the get_where() clause of the following codeigniter active record statement?
$this->db->select('*');
$q = $this->db->get_where('Contacts', array('id' => $contact_id));
$sql = $this->db->last_query();
Or does it happens once you call the result_array()?
And is $this->db->last_query(); a reliable way in getting the query string.
The query execution happens on all get methods like
$this->db->get('table_name');
$this->db->get_where('table_name',$array);
While last_query contains the last query which was run
$this->db->last_query();
If you want to get query string without execution you will have to do this.
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now you can write query and get it in a variable
$this->db->select('trans_id');
$this->db->from('myTable');
$this->db->where('code','B');
$subQuery = $this->db->_compile_select();
Now reset query so if you want to write another query the object will be cleared.
$this->db->_reset_select();
And the thing is done. Cheers!!!
Note : While using this way you must use
$this->db->from('myTable')
instead of
$this->db->get('myTable')
which runs the query.
Take a look at this example
For me save_queries option was turned off so,
$this->db->save_queries = TRUE; //Turn ON save_queries for temporary use.
$str = $this->db->last_query();
echo $str;
Ref: Can't get result from $this->db->last_query(); codeigniter
For Me It Works Perfectly: Source Disclosure : This Source website Belongs to me , i am also sharing solutions on my website ...
public function index()
{
$db = \Config\Database::connect();
$heroesCount = $db->table('products')->countAll();
echo $db->getLastQuery();
exit;
}

Resources