Yii: Using CDbCriteria with custom variable in model - yii

I'm trying to search based on full_name, which is a concatenation of first_name and last_name, but I keep getting an error.
This is what my controller looks like:
$criteria = new CDbCriteria;
$criteria->addSearchCondition('full_name',$customer_search);
$customers = Customer::model()->findAll($criteria);
In my Customer model, I have a method that should return the full_name:
public function getFull_name() {
return $this->first_name.' '.$this->last_name;
}
But, I get this error:
CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'where clause'. The SQL statement executed was: SELECT * FROM `customer` `t` WHERE full_name LIKE :ycp0
I need to be able to search both the first_name and last_name, what do I need to change to make this work?

You cannot use "calculated" columns in your query. You will simply have to rewrite the criteria so that they only check columns that physically exist in the database.
Since you want to find people where part of either their first or last name matches you will have to enclose the search term in percent signs (see MySql documentation for LIKE) and specify the second condition with OR:
$criteria = new CDbCriteria;
$criteria->addSearchCondition('first_name', $customer_search);
$criteria->addSearchCondition('last_name', $customer_search, true, "OR");
$customers = Customer::model()->findAll($criteria);

Related

Fuzzy matching a List in SQL

Have a set of data that is riddled with duplicates. The company names are either written as their Workplace name, e.g. Amazon, or the legal name, e.g Amazon.com Inc. Both entries have information I need.
Issue with the name is I am running a subquery to generate the correct list of companies to search for, however the LIKE function only seems to work for a set list.
FROM CRM.organizations
WHERE name LIKE (SELECT org_name FROM CRM.deals WHERE UUID IS NOT NULL AND status = 'won')```
The code above returns the following error: 'Error: Scalar subquery produced more than one element'
Trying to understand if there is a function that can help, or I will need to create a list manually with: 'companyAinc';'companyBllc';....
Well, the LIKE operator doesn't support directly passing a list a values to match with, you can use the CROSS APPLY to map each value to fuzzy match in your statement.
You can refer to this example for the same to use multiple clauses with LIKE operator.
On the other hand you can also try using User-defined functions/routines, in which you can map all your the returned values with the LIKE and OR operators and return your required query as a string.
FROM CRM.organizations
WHERE name in (SELECT org_name FROM CRM.deals WHERE UUID IS NOT NULL AND status = 'won');
FROM CRM.organizations
WHERE exists (SELECT 1 FROM CRM.deals WHERE UUID IS NOT NULL AND status = 'won' and organizations.name like deals.org_name );

ERR:Unknown column in where clause when query with struct

db.Table("test").Where(&Query{Name:"test"}).Find(&infos)
When I execute this statement, the SQL statement looks like this:
SELECT * FROM `test` WHERE (`queries`.`name` = 'test')
And the error:
Error 1054: Unknown column 'queries.name' in 'where clause'
It uses the name of my struct instead of the table name
Are you running the latest version of Gorm v2? I cannot reproduce what you see: the code you wrote produces
SELECT * FROM `test` WHERE `test`.`name` = "test"
In any case, if you don't use the Query struct, but instead write out the condition it should work:
db.Table("test").Where("name = ?", "test").Find(&infos)
Or you can use a map:
db.Table("test").Where(map[string]interface{}{"Name": "test"}).Find(&infos)

Laravel Query builder: left join, select, and where not working

Unfortunately the Laravel query builder seems not in a good mood!
Here is my table structure:
Table user structure
ID, USERNAME, NAME, USER_ROLE_MODULE_ID
Table user_role_module structure
ID, NAME
And this is my query builder statement
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);
Goal:
I just want to get all data from user + user role name from user_role_module AND also applies in WHERE statement so I can use it for search feature.
I am getting result from WHERE statement without specifying table name. because it already stated which table and column to select from SELECT statement.
The problem:
if I search for name, it return error ambiguous column //Laravel is confusing whether taking name from user table or user_role_module table!
if I search for user_role, then the column doesn't exist
Why is it? What is the solution?
The problem:
user.NAME <-- both these columns are called NAME
user_role_module.NAME <--
You have multiple problems with your Laravel query. As #Viktor correctly pointed out, your join condition is wrong. You should be joining user.USER_ROLE_MODULE_ID to user_role_module.id. But the immediate cause of your error I believe is that both tables have a NAME column.
Just specify the columns you want using both table and column name. This way, no column name would be ambiguous.
DB::table('user')
->leftJoin('user_role_module', 'user.USER_ROLE_MODULE_ID', '=', 'user_role_module.id')
->select('user.ID', 'user.USERNAME', 'user.NAME AS userName',
'user_role_module.name as user_role')
->where("name LIKE '%John%'")
->paginate(10);
Change this:
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);
with this:
DB::table('user')
->leftJoin('user_role_module', 'user.user_role_module_id', '=', 'user_role_module.id')
->where('user.name', 'like', '%Jhon%')
->select('user.*', 'user_role_module.name as user_role')
->get();
and make a custom pagination because select is not working with paginate.

select column in hibernate does not work but * does

I have this:
Query q = em.createNativeQuery("select * from TRANSAKTION",Transaktion.class);
List<Transaktion> trans = q.getResultList();
While this works, using a column name like id doesn't although it exists in database and generated entity class.
Other tables work fine it's just that one and with all columns. When I use * and .getId() it returns the correct id.
What could possibly be the problem?
The error message is:
SQL Exception: invalid column name

name matching with escape sequence in commons dbutils

I am using dbutils QueryRunner on a product table to get (ID, Name) from the table where the query is as follows:
select ID, NAME
from PRODUCT
where NAME LIKE ?
LIMIT 5;
I am getting
ERROR 203 (22005): Type mismatch. BOOLEAN for null Query
while if i run the same query with NAME = ? , it works fine.
Can you please let me know what is wrong with the syntax and also how can i use the escape sequence for e.g
WHERE NAME LIKE 'XYZ%'