//i am using datables in CI but not running properly . please look at the join query and suggests how i can last join condition .
private function _get_datatables_query($term='')
{
$user_id = $this->session->userdata("user_id");
$user_type = $this->session->userdata("user_type");
$column = array('A.type','C.school_name','A.mobile','A.email','A.birthdate','A.anniversary','A.gender','A.firstname','A.lastname');
$this->db->select('A.type,C.school_name,A.mobile,A.email,A.birthdate,A.anniversary,A.gender,A.firstname,A.lastname,C.s2m_admin_id,A.status,A.id,A.pusername');
$this->db->from('users as A');
$this->db->join('school_user as B', 'B.user_id = A.id');
$this->db->join('school as C', 'C.id = B.school_id');
$this->db->join('school as D', 'D.s2m_admin_id = A.id');//<= i am having problem in this line else is working properly .if i add this line query return nothing.
$this->db->having('A.status','active');
$this->db->like('A.type', $term);
$this->db->or_like('A.mobile', $term);
$this->db->or_like('A.firstname', $term);
Try this
$user_id = $this->session->userdata("user_id");
$user_type = $this->session->userdata("user_type");
//$column = array('U.type','S.school_name','U.mobile','U.email','U.birthdate','U.anniversary','U.gender','U.firstname','U.lastname');
$this->db->select(*);
$this->db->from('users as U');
$this->db->join('school_user as SU', 'SU.user_id = U.id');
# added where clause
$this->db->where('school.id = SU.school_id OR school.s2m_admin_id = U.id');
$this->db->having('U.status','active');
$this->db->like('U.type', $term);
$this->db->or_like('U.mobile', $term);
$this->db->or_like('U.firstname', $term);
Related
SQL:
$datas = DB::select("SELECT a.id,a.parent_id, d.action_id, d.role_id,d.department_id, d.is_created,
d.is_edit,d.is_delete,d.is_view,d.is_audit,d.is_verify,d.is_approved
FROM application_security_actions a
LEFT JOIN application_default_permissions d on d.action_id = a.id where d.role_id = " . $id . " or d.role_id is null");
Query Builder:
$datas = DB::select('application_security_actions as A')
->leftJoin('application_default_permissions as B','A.id','=','B.action_id')
->where('B.role_id','=',$id)
->orWhereNull('B.role_id')
->select('A.id','A.parent_id','B.is_created','B.is_edit','B.is_delete','B.is_view','B.is_audit','B.is_verify','B.is_approved')
->get();
sql is working perfectly but when is convert in to query builder it is not working. can any on check where i am wrong
Change on the line 1
DB::select to DB::table
Full code:
$datas = DB::table('application_security_actions as A')
->leftJoin('application_default_permissions as B','A.id','=','B.action_id')
->where('B.role_id','=',$id)
->orWhereNull('B.role_id')
->select('A.id','A.parent_id','B.is_created','B.is_edit','B.is_delete','B.is_view','B.is_audit','B.is_verify','B.is_approved')
->get();
In the Laravel Query Builder I want to implement something like Scope in Eloquent.
Ref: Laravel Queries: Adding custom feature like Soft Deletes.
I have some complex queries (with joins and what not) but I want to be able to easily apply a WHERE condition that works as follows:
original:
Select * from t1 join t2 ... join t3 ... etc
Where t1.c1 = x OR t3.c4 like "%like"
wanted:
Select * from t1 join t2 ... join t3 ... etc
Where (t1.c1 = x OR t3.c4 like "%like") AND (t1.isTest = false AND t3.isTest = false)
I have written the following method:
public static function scopeNoTest($query, $tables=[false])
{
if (!is_array($tables)) $tables = [$tables];
foreach ($tables as $table)
{
$field = ($table) ? $table . '.isTest' : 'isTest';
$query = $query->where(function ($q) use ($query, $field)
{
$q->where($field, false)
->orWhereNull($field);
}
);
}
return $query;
}
It gets run like this:
$select = <parameter driven select statement>
$where[$role] = <array of different where condition based on passed in parameter?
$bindings = <query bindings based on passed in parameters>
$query = DB::table('Transactions AS trans')
->leftJoin('Buyers AS b', 'trans.ID', '=', 'b.Transactions_ID')
->leftJoin('Sellers AS s', 'trans.ID', '=', 's.Transactions_ID')
->leftJoin('Agents AS ba', 'trans.BuyersAgent_ID', '=', 'ba.ID')
->leftJoin('Agents AS sa', 'trans.SellersAgent_ID', '=', 'sa.ID')
->leftJoin('TransactionCoordinators AS btc', 'trans.BuyersTransactionCoordinators_ID', '=', 'btc.ID')
->leftJoin('TransactionCoordinators AS stc', 'trans.SellersTransactionCoordinators_ID', '=', 'stc.ID')
->leftJoin('lu_UserRoles AS lu_ur', 'trans.ClientRole', '=', 'lu_ur.Value')
->leftJoin('Properties AS p', 'trans.Properties_ID', '=', 'p.ID')
->selectRaw($select);
// ... Adds code to Only Select records with isTest NOT True
$query = Model_Parent::scopeNoTest($query, ['trans', 'ba', 'sa', ]);
$query->whereRaw($where[$role].$whereUser, $bindings)->distinct();
$transactions = $query->get();
The problem with this code is that it does not put the original [passed in] query in parentheses - so the query is wrong!.
The WHERE the code creates is:
where
(`trans`.`isTest` = 0 or `trans`.`isTest` is null)
and (`ba`.`isTest` = 0 or `ba`.`isTest` is null)
and (`sa`.`isTest` = 0 or `sa`.`isTest` is null)
and trans.BuyersTransactionCoordinators_ID = 1 OR trans.SellersTransactionCoordinators_ID = 1
OR trans.CreatedByUsers_ID = 1 OR trans.OwnedByUsers_ID = 1
And I want
where
(`trans`.`isTest` = 0 or `trans`.`isTest` is null)
and (`ba`.`isTest` = 0 or `ba`.`isTest` is null)
and (`sa`.`isTest` = 0 or `sa`.`isTest` is null)
and (trans.BuyersTransactionCoordinators_ID = 1 OR trans.SellersTransactionCoordinators_ID = 1
OR trans.CreatedByUsers_ID = 1 OR trans.OwnedByUsers_ID = 1)
Is there a way to do this ??
It looks like the following line is causing this:
$query->whereRaw($where[$role].$whereUser, $bindings)->distinct();
I think there are two ways to solve this:
// 1
->whereRaw('(' . $where[$role].$whereUser . ')', $bindings)->
// 2
->where(function ($query) use (...) {
$query->whereRaw(...);
})->
I have two tables: products and current_product_attribute_values
I have tried a join query to filter them as per attribute selected by the user but when I try this with an additional condition it gives me 2 results instead of one it is including the first one which is not matching as per query:
select * from `products` inner join `current_product_attribute_values` on `products`.`id` = `current_product_attribute_values`.`product_id` where `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `product_name` LIKE ?
here is my laravel Controller code :
$all = Input::all();
$q = Input::get('search_text');
$att_val = Input::get('attribute_value');
$subcat = Input::get('subcat_id');
$subcat_name = DB::table('subcategories')->where('id', $subcat)->value('subcategory_name');
$brandname = DB::table('brands')->where('subcat_id', $subcat)->value('brand_name');
$brand_id = DB::table('brands')->where('subcat_id', $subcat)->value('id');
$product_count = DB::table('products')->where('brand_id', $brand_id)->count();
if ($q != "") {
// getting multiple same name params
$query = DB::table('products');
$query->join('current_product_attribute_values', 'products.id', '=', 'current_product_attribute_values.product_id');
$j = 0;
foreach ($all as $key => $values) {
//echo 'my current get key is : ' . urldecode($key). '<br>';
if ($key == $name[$j]) {
$query->where('current_product_attribute_values.attribute_id', '=', $att_id_value[$j]);
echo'<br>';
print_r($query->toSql());
echo'<br>';
//echo '<br> key matched and have some value : <br>';
//echo count($values);
if (count($values) >= 1) {
//echo '<br> it has array inside <br>';
foreach ($values as $val) {
// or waali query in same attribute
echo'<br>';
$query->orwhere('current_product_attribute_values.attribute_value_id', '=', $val);
print_r($query->toSql());
echo'<br>';
}
}
$j++;
}
}
$records = $query->toSql();
$query->where('product_name', 'LIKE', '%' . $q . '%');
$records = $query->toSql();
print_r($records);
$products = $query->paginate(10)->setPath('');
$pagination = $products->appends(array(
'q' => Input::get('q')
));
if (count($products) > 0) {
$filters = DB::table('product_attributes')->where('subcategory_id', $subcat)->get(['attribute_title']);
} else {
$filters = array();
}
$categories = categories::where('add_to_menu', 1)->with('subcategories')->with('brands')->get();
$categoryhome = categories::where('add_to_menu', 1)->with('subcategories')->get();
return view('searchfilter')
->with('productsdata', $products)
->with('filtersdata', $filters)
->with('categories', $categories)
->with('categorieshome', $categoryhome)
->with('subcat_name', $subcat_name)
->with('subcat_id', $subcat)
->with('brandname', $brandname)
->with('product_count', $product_count)
->with('querytext', $q);
}
return 'No Details found. Try to search again !';
its easier if you use raw sql as calling db select function. ex:
$query=DB::select("select * from `products` inner join `current_product_attribute_values` on `products`.`id` = `current_product_attribute_values`.`product_id` where `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `product_name` LIKE ?
");
indeed you can concat vars in raw sql if you need to, ex:
$queryBrands = "select id from brands where subcat_id =".$subcat;
//echo $queryBrands
$queryBrands = DB::select($queryBrands);
By looking at your tables, product table with id value 17 has two records in table current_product_attribute_values in column product_id (I assume this column is used as foreign key to product table).
With select *, you select all of the columns from both tables. So it would most likely cause your query to return multiple records.
My suggestions:
Only select the columns you need. Avoid using select * in the long run, i.e. select product.id, product.description, current_product_attribute_values.attribute_values ......
Make use of GROUP BY
Hope these helps.
I'm building a custom module and i'm trying get all orders with specific status:
$canceledQuery = Mage::getSingleton('core/resource')->getConnection('core_read')->select()
->from('mage_sales_flat_order', 'entity_id')
->where('status = ?', 'canceled');
This works very well. But now i'm trying use OR in where, without success.
I have been try this:
$whereCanceled = array();
foreach ($_status as $statusCode => $value) {
$whereCanceled[] = sprintf('%s=:%s', 'status', $statusCode);
}
$ordersQuery = Mage::getSingleton('core/resource')->getConnection('core_read')->select()
->from('mage_sales_flat_order', 'entity_id')
->where(implode(' OR ', $whereCanceled));
So, I don't know how use OR right in this case. I found no use for this with OR. Any idea?
instead of implode use join. magento use join itself.
->where(join(' OR ', $orWhere));//$orWhere array of condition
you can see in below function magento use join for OR condition in where clause
public function getSystemConfigByPathsAndTemplateId($paths, $templateId)
{
$orWhere = array();
$pathesCounter = 1;
$bind = array();
foreach ($paths as $path) {
$pathAlias = 'path_' . $pathesCounter;
$orWhere[] = 'path = :' . $pathAlias;
$bind[$pathAlias] = $path;
$pathesCounter++;
}
$bind['template_id'] = $templateId;
$select = $this->_getReadAdapter()->select()
->from($this->getTable('core/config_data'), array('scope', 'scope_id', 'path'))
->where('value LIKE :template_id')
->where(join(' OR ', $orWhere));
return $this->_getReadAdapter()->fetchAll($select, $bind);
}
for more reference open file located at [magento]/app/code/core/Mage/Core/Model/Resource/Email/Template.php
Hope this help you
following is my query :
$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select('link_req')
->from('\Entities\link_requests','link_req')
->where( 'link_req.is_confirmed = 1' )
->andWhere('link_req.$link_requestsSenderUser='.$user1_id .'or'.' link_req.$link_requestsSenderUser='.$user2_id)
->andWhere('link_req.$link_requestsReceiverUser='.$user1_id .'or'.' link_req.$link_requestsReceiverUser='.$user2_id);
$result= $q_1->getDql();
echo $result;
and i want following query:
SELECT * FROM [fb_local].[dbo].[link_requests]
WHERE is_confirmed = 1 AND (request_user_id = 12 or request_user_id=19) AND (accept_user_id = 12 or accept_user_id = 19)
Your SQL is just 1 query, so not sure what you're trying to nest. But try the following:
$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select('link_req')
->from('\Entities\link_requests','link_req')
->where( 'link_req.is_confirmed = 1' )
->andWhere('link_req.link_requestsSenderUser=:user_id or link_req.link_requestsSenderUser=:user_id')
->andWhere('link_req.link_requestsReceiverUser=:user_id or link_req.link_requestsReceiverUser=:user_id');
$q_1->setParameter('user_id', $user1_id);
$query = $q_1->getQuery();
echo $query->getSql();
Notice how I removed the $ in front of link_requestsReceiverUser. I also made a parameter from $user1_id, this prevents SQL injections.