this is my query in model (in my sql this query is working fine but in code -igniter it gives me fatal error(Call to a member function result() on a non-object )
$this->db->select('j.*, c.name as Cname, d.name as Dname, e.name as
Ename,GROUP_CONCAT(s.qualification SEPARATOR ",") as
Qualification,GROUP_CONCAT(s.year SEPARATOR ",") as
Year,GROUP_CONCAT(s.institute SEPARATOR ",") as Institute' );
$this->db->from('jobseekers j');
$this->db->join('city c','c.id = j.location','left');
$this->db->join('vertical e','e.id = j.vertical','left');
$this->db->join('designation d','d.id = j.designation','left');
$this->db->join('jobseekers_qul s','s.jobseekers_id = j.id','left');
$this->db->group_by('j.id');
$query = $this->db->get();
$row = $query->result();
return $row;
Related
In symfony, I join table user(id, email) and table useCases(id, userId, caseType) (relations is OneToMany) like this:
public function getQueryBuilder()
{
$qb = $this->userRepository->createQueryBuilder('u');
$qb->select(
'
u.id,
u.email,
GROUP_CONCAT(DISTINCT(CONCAT(IFNULL(uc.caseType,"NULL")))) as dfCaseType,
'
)
->leftJoin('u.userCases', 'uc')
->orderBy("u.id", "DESC");
return $qb;
}
public function getById($id)
{
$qb = $this->getQueryBuilder()
->where('u.id = :id')
->setParameter('id', $id);
$result = $qb->groupBy('u.id')->setMaxResults(1)->getQuery()->getOneOrNullResult();
return $result;
}
I test query on Sequel Pro, and it's OK, here is query:
select user.id, user.email, GROUP_CONCAT(DISTINCT(CONCAT(IFNULL(`caseType`,'NULL')))) as dfCaseType from user, userCases where user.id = userCases.userId AND user.id = 44613
and here is result:
id emai dfCaseType
1 aaa#gmail.com. 0,1,2,3,NULL
But when I apply in project symfony with query builder , it error :
[Syntax Error] line 0, col 231: Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got '('
Please help me, thanks!
I need help on a particular codeigniter 3.1.6 active record query that keeps returning an error.
The code itself is:
$this->db->select('ms_payments.id, ms_payments.user_id, ms_payments.memberships_user_id, ms_payments.staff_id, ms_payments.rate_amount, ms_payments.tax, ms_payments.coupon, ms_payments.total, ms_payments.currency, ms_payments.pp, ms_payments.date,
u1.first_name AS user_name, u1.last_name AS user_last_name, u2.first_name AS staff_name, u2.last_name AS staff_last_name,
ms_gateways.displayname AS pp, ms_memberships_users.id, ms_memberships.title AS membership')
->from('ms_payments')
->join('auth_users as u1', 'u1.id = ms_payments.user_id')
->join('auth_users as u2', 'u2.id = ms_payments.staff_id')
->join($this->gTable, 'ms_gateways.id = ms_payments.pp')
->join($this->muTable, 'ms_memberships_users.id = ms_payments.memberships_user_id')
->join($this->mTable, 'ms_memberships.id = ms_memberships_users.membership_id')
->where('ms_payments.box_id =', $this->box_id);
$result = $this->db->get()->result();
The issue is related to the first two joins, I suspect is because the table name aliases.
The SQL query generated by $this->db->get_compiled_select() query works perfectly:
SELECT `ms_payments`.`id`, `ms_payments`.`user_id`, `ms_payments`.`memberships_user_id`, `ms_payments`.`staff_id`, `ms_payments`.`rate_amount`, `ms_payments`.`tax`, `ms_payments`.`coupon`, `ms_payments`.`total`, `ms_payments`.`currency`, `ms_payments`.`pp`, `ms_payments`.`date`,
`u1`.`first_name` AS `user_name`, `u1`.`last_name` AS `user_last_name`, `u2`.`first_name` AS `staff_name`, `u2`.`last_name` AS `staff_last_name`, `ms_gateways`.`displayname` AS `pp`,
`ms_memberships_users`.`id`, `ms_memberships`.`title` AS `membership`
FROM `ms_payments`
JOIN `auth_users` as `u1` ON `u1`.`id` = `ms_payments`.`user_id`
JOIN `auth_users` as `u2` ON `u2`.`id` = `ms_payments`.`staff_id`
JOIN `ms_gateways` ON `ms_gateways`.`id` = `ms_payments`.`pp`
JOIN `ms_memberships_users` ON `ms_memberships_users`.`id` = `ms_payments`.`memberships_user_id`
JOIN `ms_memberships` ON `ms_memberships`.`id` = `ms_memberships_users`.`membership_id`
WHERE `ms_payments`.`box_id` = '1'
and yet I still get this codeigniter error, any clues why?
Blockquote An uncaught Exception was encountered
Blockquote Type: Error
Blockquote Message: Call to a member function result() on boolean
This is a search function that returns each member with their most recent year registered.
I got it working with a DB::raw() call. But can't get it working with the query builder.
Working Code:
$query = DB::table('membership as m');
$query->join(
DB::raw(
'(SELECT my.*
FROM membership_years my
INNER JOIN (
SELECT member_id,MAX(membership_year) AS max_my
FROM membership_years
GROUP BY member_id
) my2
ON my.member_id = my2.member_id
AND my.membership_year = my2.max_my
) my'
)
,'m.id','=','my.member_id');
My attempt on query builder code:
$query = DB::table('membership as m');
$query->join('membership_years as my',
function($j1){
$j1->join('membership_years as my2',
function($j2){
$j2->where('my.membership_year','=','MAX(my2.membership_year)')
->on('my.member_id','=','my2.member_id');
}
)->on('m.id','=','my.member_id');
}
);
The resulting error is:
Call to undefined method Illuminate\Database\Query\JoinClause::join()
I'm not sure if this is because the $j2 doesn't have access to the join method anymore?
Raw MySQL query:
SELECT my.membership_year,m.*
FROM membership AS m
INNER JOIN
(
SELECT my1.*
FROM membership_years my1
INNER JOIN
(
SELECT member_id,MAX(membership_year) AS max_my
FROM membership_years
GROUP BY member_id
) my2
ON my1.member_id = my2.member_id
AND my1.membership_year = my2.max_my
) my
ON m.id = my.member_id
ORDER BY m.id ASC
Way 1. You can write part of the query with builder:
$query = DB::table('membership as m')
->select('my.membership_year', 'm.*')
->join(DB::raw('(
SELECT my1.*
FROM membership_years my1
INNER JOIN (
SELECT member_id, MAX(membership_year) AS max_my
FROM membership_years
GROUP BY member_id
) my2
ON my1.member_id = my2.member_id
AND my1.membership_year = my2.max_my
) my'),
'm.id', '=', 'my.member_id')
->orderBy('m.id');
Way 2. Also you can write subqueries and use toSql() method:
$sub1 = DB::table('membership_years')
->select('member_id', DB::raw('MAX(membership_year) AS max_my'))
->groupBy('member_id');
$sub2 = DB::table('membership_years as my1')
->select('my1.*')
->join(DB::raw('(' . $sub1->toSql() . ') my2'),
function ($join) {
$join
->on('my1.member_id', '=', 'my2.member_id')
->on('my1.membership_year', '=', 'my2.max_my');
});
$query = DB::table('membership as m')
->select('my.membership_year', 'm.*')
->join(DB::raw('(' . $sub2->toSql() . ') my'), 'm.id', '=', 'my.member_id')
->orderBy('m.id');
I have this Query in MySql
SELECT l.id as like_id, l.spotted_id as spotted_id,count(l.spotted_id) as numero_likes
FROM sn_like_spotted l
LEFT JOIN prof_foto f
ON f.id = l.spotted_id
LEFT JOIN sn_profilo p
ON f.profilo_id = p.id
WHERE p.id = 3
GROUP BY l.spotted_id
ORDER BY numero_likes DESC LIMIT 0,10
I try to do this in Doctrine
public function getFoo($profilo_id){
$em = $this->getEntityManager();
$query = $em->createQuery('
SELECT l.id as like_id, l.spotted_id as spotted_id,count(l.spotted_id) as numero_likes
FROM SNLikeBundle:LikeSpotted l
LEFT JOIN SNFotoBundle:Foto f
WITH f.id = l.spotted_id
LEFT JOIN SNProfiloBundle:Profilo p
WITH f.profilo_id = p.id
WHERE p.id = :profilo_id
GROUP BY l.spotted_id
ORDER BY numero_likes DESC
')->setFirstResults(0)
->setMaxResults(10)
->setParameter('profilo_id', $profilo_id);
$results = $query->getResult();
return $results;
}
Then i have this error
[Semantical Error] line 0, col 36 near 'spotted_id as': Error: Class SN\LikeBundle\Entity\LikeSpotted has no field or association named spotted_id
I change SELECT in:
SELECT l.id as like_id, l.spotted as spotted_id,count(l.spotted) as numero_likes
and I have this Error:
[Semantical Error] line 0, col 36 near 'spotted as spotted_id,count(l.spotted)': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
Then i Try With IDENTITY
SELECT IDENTITY l.id as like_id, l.spotted as spotted_id,count(l.spotted) as numero_likes
But i Have This Error
[Syntax Error] line 0, col 26: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '.'
I try to create also in QueryBuilder (more criteria, same result)
$q = $this->createQueryBuilder('l');
$q->leftJoin("l.spotted", 's');
$q->leftJoin("s.profilo", 'p');
$q->leftJoin("p.utente", 'u');
$q->where('(s.foto_eliminata IS NULL OR s.foto_eliminata != 1)');
$q->andWhere('p.fase_registrazione = :fase');
$q->andWhere('u.locked = :false');
$q->andWhere('p.id = :profilo_id');
$q->setParameter(':fase', 100);
$q->setParameter('false', false);
$q->setParameter('profilo_id', $profilo_id);
$q->groupBy('l.spotted');
$q->orderBy($q->expr()->count('l.spotted'), 'desc');
$q->setMaxResults(10);
$dql = $q->getQuery();
$results = $dql->execute();
return $results;
My error is
[Syntax Error] line 0, col 285: Error: Expected end of string, got '('
The problem is in
$q->orderBy($q->expr()->count('l.spotted'), 'desc');
For CreateBuilder Case I Resolved:
$q = $this->createQueryBuilder('l');
$q->addSelect('count(l.spotted) as numero_likes');
$q->leftJoin("l.spotted", 's');
$q->leftJoin("s.profilo", 'p');
$q->leftJoin("p.utente", 'u');
$q->where('(s.foto_eliminata IS NULL OR s.foto_eliminata != 1)');
$q->andWhere('p.fase_registrazione = :fase');
$q->andWhere('u.locked = :false');
$q->andWhere('p.id = :profilo_id');
$q->setParameter(':fase', 100);
$q->setParameter('false', false);
$q->setParameter('profilo_id', $profilo_id);
$q->groupBy('l.spotted');
$q->addOrderBy('numero_likes','DESC');
$q->setMaxResults(10);
$dql = $q->getQuery();
$results = $dql->execute();
return $results;
if i don't want numero_likes in my results i can do this:
$q->addSelect('count(l.spotted) as HIDDEN numero_likes');
But i'd like how can i do this in createQuery
I have this query
SELECT
arl_kind,
IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
arl_display_nr
FROM
tof_art_lookup
LEFT JOIN
tof_brands
ON
bra_id = arl_bra_id
INNER JOIN
tof_articles
ON
tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
tof_suppliers
ON
tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
arl_art_id = #art_id
AND arl_kind IN (3)
ORDER BY
arl_kind,
bra_brand,
arl_display_nr limit 100;
when I execute this on phpmyadmin or in any software that has sql comand capabilities everything works and the output is as i should.
The problem appears when I try to insert this in codeigniter, and my main probles is with the if statement, I tried to insert it like this
return $query = $this->db
->SELECT('
arl_kind,
IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
arl_display_nr
FROM
tof_art_lookup
LEFT JOIN
tof_brands
ON
bra_id = arl_bra_id
INNER JOIN
tof_articles
ON
tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
tof_suppliers
ON
tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
arl_art_id = #art_id
AND arl_kind IN (3)
ORDER BY
arl_kind,
bra_brand,
arl_display_nr limit 100')
->get();
and second option like that
return $query = $this->db
->select('ARL_KIND, IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,ARL_DISPLAY_NR')
->from('tof_art_lookup')
->join('tof_brands','bra_id = arl_bra_id','LEFT')
->join('tof_articles','tof_articles.art_id = tof_art_lookup.arl_art_id','INNER')
->join('tof_suppliers','tof_suppliers.sup_id = tof_articles.art_sup_id','INNER')
->where('ARL_ART_ID',$id)
->where_in('ARL_KIND',array('3'))
//->where('A.ARL_DISPLAY','0')
->group_by('arl_kind','bra_brand','arl_display_nr')
->get();
both of them work but partialy without the second select option the one with the if.
Can anyone help me to fix this.
I believe your code should look like this
return $query = $this->db
->select('arl_kind, IF(tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,ARL_DISPLAY_NR', FALSE)
->from('tof_art_lookup')
->join('tof_brands', 'bra_id = arl_bra_id', 'LEFT')
->join('tof_articles', 'tof_articles.art_id = tof_art_lookup.arl_art_id', 'INNER')
->join('tof_suppliers','tof_suppliers.sup_id = tof_articles.art_sup_id', 'INNER')
->where('arl_art_id', $id)
->where_in('arl_kind', array('3'))
->order_by('arl_kind','bra_brand','arl_display_nr')
->limit(100)
->get();
Note: second parameter of select() method is set to FALSE and order_by() is used instead of group_by() as in your original query.
use like this
$query_string = "SELECT
arl_kind,
IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
arl_display_nr
FROM
tof_art_lookup
LEFT JOIN
tof_brands
ON
bra_id = arl_bra_id
INNER JOIN
tof_articles
ON
tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
tof_suppliers
ON
tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
arl_art_id = #art_id
AND arl_kind IN (3)
ORDER BY
arl_kind,
bra_brand,
arl_display_nr limit 100";
$query=$this->db->query($query_string);
if ($query->num_rows () > 0) {
return $query->result_array ();
} else {
return FALSE;
}