removing backslashes in the query using codeigniter mysql - codeigniter

Post value is multi select, means values are coming in the form of array
if(!empty($_POST['form_type']))
{
$test = implode("','",$_POST['form_type']);
$this->db->where_in('enquiry.type_of_enquiry',"'".$test."'");
}
my query is like this
SELECT `sobha_enquiry`.`name`, `sobha_enquiry`.`date_created`, `sobha_enquiry`.`company`, `sobha_enquiry`.`form_of`, `sobha_enquiry`.`projectname`, `sobha_enquiry`.`city`, `sobha_enquiry`.`country`, `sobha_enquiry`.`phone`, `sobha_enquiry`.`type_of_enquiry`, `sobha_enquiryzone`.`enquiry_id`, `sobha_enquiry`.`hearaboutus`, `sobha_enquiry`.`email`, `sobha_enquiry`.`comments`, `sobha_enquiry`.`address`, `sobha_admin`.`id`, `sobha_admin`.`city_id` FROM (`sobha_senquiry`) LEFT JOIN `sobha_enquiryzone` ON `sobha_enquiryzone`.`enquiry_id` =`sobha_enquiry`.`id` LEFT JOIN `sobha_admin` ON `sobha_admin`.`city_id`=`sobha_enquiryzone`.`city_id` WHERE `sobha_enquiry`.`type_of_enquiry` IN ('\'register form\',\'feedback form\'') GROUP BY `sobha_enquiry`.`id` ORDER BY `sobha_enquiry`.`id` desc LIMIT 15
since i used implode function it is coming like this IN ('\'register form\',\'feedback form\'') i want to remove those backslashes . Please help me

$this->db->where_in() will accept array as second argument.
So there is no need for imploding the data.
if(!empty($_POST['form_type']))
{
$this->db->where_in('enquiry.type_of_enquiry',$_POST['form_type']);
}
This will result as,
WHERE `enquiry`.`type_of_enquiry` IN ('register form','feedback form')
You can refer this link for more info, Codeigniter Active Record Documentation

Related

Partial select of left joined table with doctrine query builder

When querying one table using the doctrine query builder a partial select can be written like this:
$queryBuilder = $this->createQueryBuilder('person');
$queryBuilder->addSelect('partial person.{id, name}');
How can one write a partial select be written for a left joined table? I tried something like this, but can't figure out the correct syntax:
$queryBuilder = $this->createQueryBuilder('person');
$queryBuilder->join('person.address');
$queryBuilder->addSelect('partial person.{id, name} person.address.city'); // ???
My goal would be to select only parts of the Person and the Address object when executing the query to be more memory efficient.
Your syntax is off for your join operation. You have to give an alias when using join. From there, you can just use the same syntax to query your partial Address object:
// In a method of PersonRepository
$qb = $this->createQueryBuilder('person')
->select(['partial person.{id, name}', 'partial address.{id, city}'])
->join('person.address', 'address');
Notice that I added id to the fields retrieved for Address. If you don't, Doctrine will give you the following error:
Error: The partial field selection of class Path\To\Entity\Address must contain the identifier
As a side note, you said you wanted to write this select for a left joined table. If you want to perform a LEFT JOIN, you need to use leftJoin instead of join (the signature of both methods is the same).

Laravel query builder - Select elements unique or null on specific column

I have a model Form for table forms. There is a column called guid which can be null, or contain some sort of grouping random hash.
I need to select all forms that have column guid either null or unique in current search. In other words, for repeating guid values in current search I select only first occurence of every guid hash.
I tried:
$results = App\Form::where(... some where clauses .. ).groupBy('guid')
and it's almost ok, but for all rows, where guid == NULL it groups them and selects only one (and I need all of them).
How can I get the unique or null rows either by building proper SQL query or filtering the results in PHP?
Note: I need my $results to be an Illuminate\Database\Eloquent\Builder instance
EDIT:
I fount out that SQL version of query I need is:
SELECT * FROM `forms` WHERE .... GROUP BY IFNULL(guid, id)
What would be equivallent query for Laravel's database query builder?
UPDATE: Using DB::raw
App\Form::where(... conditions ...)
->groupBy(DB::raw("IFNULL('guid', 'id')"));
Or the another way could be:
You can also use whereNotNull, whereNull & at last merge both the collections using merge() like this:
First get the results where guid is grouped by (excluding null guid's here):
$unique_guid_without_null = App\Form::whereNotNull('guid')->groupBy('guid')->get();
Now, get the results where guid is null:
$all_guid_with_null = App\Form::whereNull('guid')->get();
and at last merge both the collections using merge() method:
$filtered_collection = $unique_guid_without_null->merge($all_guid_with_null);
Hope this helps!
For your edited question, you can use raw() as;
->groupBy(DB::raw("IFNULL('guid', 'id')"))
So your final query will be as:
$results = App\Form::where(... some where clauses .. )
->groupBy(DB::raw("IFNULL('guid', 'id')"));
By above query, your $results will be an instance of Illuminate\Database\Eloquent\Builder.

Select one column with where clause Eloquent

Im using Eloquent. But I'm having trouble understanding Eloquent syntax. I have been searching, and trying this cheat sheet: http://cheats.jesse-obrien.ca, but no luck.
How do i perform this SQL query?
SELECT user_id FROM notes WHERE note_id = 1
Thanks!
If you want a single record then use
Note::where('note_id','1')->first(['user_id']);
and for more than one record use
Note::where('note_id','1')->get(['user_id']);
If 'note_id' is the primary key on your model, you can simply use:
Note::find(1)->user_id
Otherwise, you can use any number of syntaxes:
Note::where('note_id', 1)->first()->user_id;
Note::select('user_id')->where('note_id', 1)->first();
Note::whereNoteId(1)->first();
// or get() will give you multiple results if there are multiple
Also note, in any of these examples, you can also just assign the entire object to a variable and just grab the user_id attribute when needed later.
$note = Note::find(1);
// $user_id = $note->user_id;

Laravel get all the values of a column beginning with a number

I have a model called "Image" and a table called "images". The table has a column for the "id" and another for the "name". I need to fetch only the rows with the name beginning with a number.
I need to fetch are called something like
16783521_facebook.png
While the others are something like...
twiter.png
Try this:
Image::whereRaw("name REGEXP '^[0-9]'") -> get();
If it's something you're going to use in more than 1 place, consider moving it to a scope.
In your image model define something like:
public function scopeNumeric($query)
{
return $query -> whereRaw("name REGEXP '^[0-9]'");
}
Then you can just use:
Image::numeric() -> get();
I dont know much about laravel, but this plain query will help -
SELECT * FROM mytable WHERE mycolumn REGEXP '^[0-9]+$' or
SELECT * FROM myTable WHERE col1 REGEXP '[0-9]+';
Laravel doesn't have that built-in, so you'll have to make do with raw queries. In its base form:
$results = SomeModel::whereRaw("some_column REGEXP '^[0-9]'")->get();
You can modify this as usual with selects, other limitations, etc. as you require.
Filter the images after the query using one of the collection methods. Like below solved me.
$onlyNumeric = $photos->filter(function ($value, $key) {
return is_numeric(substr($value, 0, 1));
});

Can you do a join using an embedded array in a document with rethinkdb?

Say I have a user table with a property called favoriteUsers which is an embedded array. i.e.
users
{
name:'bob'
favoriteUsers:['jim', 'tim'] //can you have an index on an embedded array?
}
user_presence
{
name:'jim', //index on name
online_since:14440000
}
Can I do an inner or eqJoin against say a 2nd table using the embedded property, or would I have to pull favoriteUsers out of the users table and into a join table like in traditional sql?
r.table('users')
.getAll('bob', {index:'name'})
// inner join user_presence on user_presence.name in users.highlights
.eqJoin("name", r.table('user_presence'), {index:'name'})
Eventually, I'd like to call changes() on the query so that I can get a realtime update of the users favorite users presence changes
eqJoin can works on embedded document, but it works by compare a value which we transform/pick from the embedded document to mark secondary index on right table.
In any other complicated join, I would rather use concatMap together with getAll.
Let's say we can fetch user and user_presence of their favoriteUsers
r.table('users')
.getAll('bob', {index: 'name'})
.concatMap(function(user) {
return r.table('user_presence').filter(function(presence) {
return user("favoriteUsers").contains(presence("name"))
})
)
So ideally, now you get the data and do the join yourself by querying extra data that you need. My query may have some syntax/error but I hope it gives you the idea

Resources