Or sql statement using MY_MODEL by avenirer - codeigniter

Can anybody please help me achieving the result and solve my problem. I'm working in codeigniter where i want to fetch records from a mysql database using OR statement. I'm using custom model extension by Avenirer. I want to know how to use or operator in where function of this MY_MODEL:
https://github.com/avenirer/CodeIgniter-MY_Model/blob/version-3.0/core/MY_Model.php

You can do it in two ways.
You use or_where
$this->db->where('column1', 'like');
$this->db->or_where('column2', 'like');
or you can write both in a where
$this->db->where("(column1='like' OR column2='like')");
Hope it helps you out.
I can't give you an exact answer for your code, because I don't know exactly where the error is. If you could write the line to it, that would be useful.

I figured out the answer by studying the code in MY_MODEL file.
The right way to write where with OR clause was to use their special syntax.
As per the code in the file MY_MODEL, it states the supported arguments for where function, which are:
where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE)
Setting the 4th argument true will change the final syntax to OR statement.
Right statement in my case will be:
$this->Table_model->where('field1','value1')->where('field2','value2',null,true)->get_all();
This statement will be processed to following SQL statement.
SELECT * FROM Table WHERE 'field1'='value1' or 'field2'='value2'
Thanks all for their cooperation.

Related

Laravel relationship gives me "?" instead of email

I'm setting this relationship
public function nonAcceptedContracts()
{
return $this->belongsToMany(UserContract::class, 'invite', 'email', 'usercontract')->where('status', 'pending');
}
And calling it like that.
$user->loadMissing('nonAcceptedContracts');
dd($user->nonAcceptedContracts()->toSql());
What I get is
"select * from `usercontract` inner join `invite` on `usercontract`.`id` = `invite`.`usercontract` where `invite`.`email` = ? and `status` = ? and `usercontract`.`deleted_at` is null"
The query seems to be the one I'm looking for but why do I get this question mark there? Shouldn't be there the email of my user model? Also the "status" value, should be "pending" and there is another question mark.
That's just how toSql() works. What you are seeing is a parameterized query. These parameterized queries allow type-specific value when replacing the ? with their respective value. They are frequently used when executing dynamic SQL from a program.
You can read more on this topic here: https://scotch.io/tutorials/debugging-queries-in-laravel
If you really want to see the actual queries, try enabling the query log. One thing to note here is that this logfile can grow very large on a busy server.

laravel return '?' instead of '1' in WHERE clause

$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->toSql();
dd($datas);
I wrote query in "laravel 5.2". I displayed it as sql query with function toSql()
and the result is:
"select * from `els` where `status` = ? order by `created_at` desc limit 4"
you can see that there is ? mark. this is why my query doesn't work. why does it return me ? instead of one. I also tried it like this '1' same result.
You may want to read about bindings if you want to understand what does ? mean.
https://laravel.com/docs/5.3/database#running-queries
If you want to debug and watch queries, you can install this debugbar: https://github.com/barryvdh/laravel-debugbar
? is used for Parameter binding which prevent against SQL injection.
If you want to see the raw SQL query which is executed with parameters values, try the below code:
DB::enableQueryLog();
$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->get();
dd(DB::getQueryLog());
Hope this helps!

Short table name for SQL Query with Codeigniter database library

I'm using CodeIgniter, I want to using short table name for my SQL Query like that:
select pr.name from product pr where pr.id=12;
by using db class, I supposed to do:
$this->db->select('pr.name')->from('product pr')->where('pr.id', 12)->get();
This works perfect on CI 2.1.3. Don't forget to use result().
Example works for me:
function test(){
$this->load->database();
$sql = $this->db->select('pr.order_id')->from('items_table pr')->where('pr.order_id', 2)->get();
foreach($sql->result() as $item){
echo $item->order_id;
}
}
You can do it in this form:
$this->db->select('pr.name');
$this->db->from('product as pr');
$this->db->where('pr.id', 4);
return $this->db->get()->row_array();
with row_array() you will get one row, with result_array() you will get result of array.
There are so many solutions.. I prefer HMVC, but for demo purpose to explain "how it works", solutions inside controller (terrible,sucks!!!!)
Answer for using shorts table as alias, pls read rtfm or use simple query and generating results
For join methods,you can use too. Happy coding

Doctrine query not executing

trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....

EntityFramework Linq query

I cannot understand what is wrong with following query:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;
Should this be same as:
select * from mytable
where Barcode like '%11%' and Description like '%EW%';
If I run this in sql server i get four rows as it should be, but not when I run the linq query
i get 0 rows.
Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much
You forget fetch data, do this:
var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
Also do not call AsEnumerable soon, use it as below:
var tmp = ctx.mytable.Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();
dbset.AsEnumerable<mytable>()...
Don't do that!
You are causing all your data to get pulled from the database before checking the Where condition.
Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.
Run a query with only one condition? " c.Barcode.Contains("11") ".
That code should run fine.

Resources