When I echo a Zend_Db query it contains 'ON Array', why? - php

Sql Query:
SELECT test.* FROM test JOIN test_shares
WHERE test.test_id = test_shares.test_id
AND test_shares.email_address = '$email'
AND test.url is NOT NULL ORDER BY title ASC;
Rewritten as Zend_Db_Select statement.
$select = $db->select ()
->from ( 'test', 'test.*' )
->join ( 'test_shares', array () )
->where ( 'test.test_id = test_shares.test_id')
->where ( 'test_shares.email_address = ?', '$email')
->where ( 'test.url is NOT NULL')
->order ( 'title' );
echo $select;
Output:
SELECT test.* FROM test JOIN test_shares ON Array
WHERE test.test_id = test_shares.test_id
AND test_shares.email_address = '$email'
AND test.url is NOT NULL ORDER BY title ASC
Please suggest why this 'ON Array' is showing.

Try this:
$select = $db ->select ()
->from ( 'movie', 'movie.*' )
->join(array('movie_shares'), 'movie.movie_id = movie_shares.movie_id');
->where ( 'movie_shares.email_address = ?', '$email')
->where ( 'movie.url is NOT NULL')
->order ( 'title' );
echo $select;
You're supplying an array where a string is expected for the ON clause, so PHP literally prints array() instead of the string.

Related

how to search using the sum of case when in cakephp

I want to do implement a search in the generated data of the sum count of a case when in cakephp with or or conditions in each data.
SELECT `Branches`.`name` AS `Branches__name` , `Branches`.`code` AS `Branches__code` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END )
) AS `carried` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =0
THEN 1
END )
) AS `unCarried`
FROM `branches` `Branches`
INNER JOIN `products_carried_per_branches` `prodCarried` ON ( prodCarried.company_id = Branches.company_id
AND prodCarried.branch_code = Branches.code )
WHERE (
`Branches`.`company_id` =200017
AND `Branches`.`deleted` =0
AND `prodCarried`.`deleted` =0
)
GROUP BY `code`
HAVING COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END ) =39
the picture shows the mysql result without the having code
while this is my cakephp code, I want to implement the having sql into cakephp or condition to search the generated sum count or search the data name and code. is this possible or nah
$carriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '1']),
1,
'integer'
);
$unCarriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '0']),
1,
'integer'
);
//disctinct code
$query ->select([
'name',
'code',
'carried' => $query->func()->count($carriedCase),
'unCarried' => $query->func()->count($unCarriedCase),
'prodCarried.id',
'prodCarried.validity_start',
'prodCarried.validity_end',
]);
$query ->distinct([
'code'
]);
$query->join([
'prodCarried' => [
'table' =>'products_carried_per_branches',
'type' => 'inner',
'conditions' => [
'prodCarried.company_id = Branches.company_id',
'prodCarried.branch_code = Branches.code',
]
]
]);
$query->where(function (QueryExpression $exp, Query $q) use($option,$search){
$exp->eq('Branches.company_id', $option['company_id']);
$exp->eq('Branches.deleted', 0);
$exp->eq('prodCarried.deleted', 0);
if(!empty($search)){
$orConditions = $exp->or_(function (QueryExpression $or) use ($search) {
$or->like('name', "%$search%");
$or->like('code', "%$search%");
//****************************
return $or;
});
$exp->add($orConditions);
}
return $exp;
});
Depending on your DBMS you can refer to the aggregate of the select list:
$query->having([
'carried' => $searchCount,
]);
Otherwise you have to recreate the aggregate:
$query->having(
function (
\Cake\Database\Expression\QueryExpression $exp,
\Cake\ORM\Query $query
) use (
$carriedCase,
$searchCount
) {
return $exp->eq(
$query->func()->count($carriedCase),
$searchCount
);
}
);
See also
Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having

Is it Possible to do a datatable search using the generated sumcount of the sum case in CAKEPHP [duplicate]

I want to do implement a search in the generated data of the sum count of a case when in cakephp with or or conditions in each data.
SELECT `Branches`.`name` AS `Branches__name` , `Branches`.`code` AS `Branches__code` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END )
) AS `carried` , (
COUNT(
CASE WHEN `prodCarried`.`carried` =0
THEN 1
END )
) AS `unCarried`
FROM `branches` `Branches`
INNER JOIN `products_carried_per_branches` `prodCarried` ON ( prodCarried.company_id = Branches.company_id
AND prodCarried.branch_code = Branches.code )
WHERE (
`Branches`.`company_id` =200017
AND `Branches`.`deleted` =0
AND `prodCarried`.`deleted` =0
)
GROUP BY `code`
HAVING COUNT(
CASE WHEN `prodCarried`.`carried` =1
THEN 1
END ) =39
the picture shows the mysql result without the having code
while this is my cakephp code, I want to implement the having sql into cakephp or condition to search the generated sum count or search the data name and code. is this possible or nah
$carriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '1']),
1,
'integer'
);
$unCarriedCase = $query->newExpr()
->addCase(
$query->newExpr()->add(['prodCarried.carried' => '0']),
1,
'integer'
);
//disctinct code
$query ->select([
'name',
'code',
'carried' => $query->func()->count($carriedCase),
'unCarried' => $query->func()->count($unCarriedCase),
'prodCarried.id',
'prodCarried.validity_start',
'prodCarried.validity_end',
]);
$query ->distinct([
'code'
]);
$query->join([
'prodCarried' => [
'table' =>'products_carried_per_branches',
'type' => 'inner',
'conditions' => [
'prodCarried.company_id = Branches.company_id',
'prodCarried.branch_code = Branches.code',
]
]
]);
$query->where(function (QueryExpression $exp, Query $q) use($option,$search){
$exp->eq('Branches.company_id', $option['company_id']);
$exp->eq('Branches.deleted', 0);
$exp->eq('prodCarried.deleted', 0);
if(!empty($search)){
$orConditions = $exp->or_(function (QueryExpression $or) use ($search) {
$or->like('name', "%$search%");
$or->like('code', "%$search%");
//****************************
return $or;
});
$exp->add($orConditions);
}
return $exp;
});
Depending on your DBMS you can refer to the aggregate of the select list:
$query->having([
'carried' => $searchCount,
]);
Otherwise you have to recreate the aggregate:
$query->having(
function (
\Cake\Database\Expression\QueryExpression $exp,
\Cake\ORM\Query $query
) use (
$carriedCase,
$searchCount
) {
return $exp->eq(
$query->func()->count($carriedCase),
$searchCount
);
}
);
See also
Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having

How to write subquery IN conditional zend 2

I have MySQL Query i want to write this query zend 2.
select billsec, call_status, Count(*)
from (
select
billsec,
if(ANSWERED_NUM is Null, 'Missed', 'Answered') call_status
from cust_info
where billsec in (
select id
from `users`
where `account_id` = 452 and `added_by` = 20694 and `status` = 'active'
)
)a
group by a.billsec,a.call_status
in zend 2 am trying to write like this but
$adapter = $this->getAdapter();
$resultset = $this->select( function( Select $select ) use ( $request, $adapter ) {
$sub1 = new Select( 'users' );
$sub1->columns( array( 'id' ) );
$sub1->where( array( 'account_id' => '452','added_by' => '20694','status' => 'active' ) );
$sub2 = new Select( 'cust_info' );
$sub2->columns(array("id","billsec", "if(ANSWERED_NUM is Null, 'Missed','Answered')"=>"call_status"));
$sub2->where('billsec IN(?)', new \Zend\Db\Sql\Expression( '?', array( $sub1 ) ));
var_dump( $sub2->getSqlString( $adapter->getPlatform() ) );die();
});
When am print this query output like this:
"SELECT `cust_info`.`id` AS `id`, `cust_info`.`billsec` AS `billsec`, `cust_info`.`call_status` AS `if(ANSWERED_NUM is Null, 'Missed','Answered')` FROM `cust_info` WHERE billsec IN('')"
Here am not able to write IN condition Query, thank in advance.

Eloquent query builder if clause implementation

I have a query like this
SELECT
`id`,
`advert_id`,
IF(
`chats`.`owner_id` = ?,
`chats`.`sender_id`,
`chats`.`owner_id`
) AS `sender_id`,
IF(
`chats`.`owner_id` = ?,
`chats`.`owner_access_time`,
`chats`.`sender_access_time`
) AS `access_time`,
`created_time`
FROM
`chats`
WHERE 1
AND `id` = ?
AND `owner_id` = ?
AND `project_id` = ?
UNION
SELECT
`id`,
`advert_id`,
IF(
`chats`.`owner_id` = ?,
`chats`.`sender_id`,
`chats`.`owner_id`
) AS `sender_id`,
IF(
`chats`.`owner_id` = ?,
`chats`.`owner_access_time`,
`chats`.`sender_access_time`
) AS `access_time`,
`created_time`
FROM
`chats`
WHERE 1
AND `id` = ?
AND `sender_id` = ?
AND `project_id` = ?
ORDER BY `id` ASC
i am trying to implement it with eloquent query builder like this
$chats = $database
->table('chats')
->where('sender_id', '=', $arguments['user_id'])
->union(
$database
->table('chats')
->where('owner_id', '=', $arguments['user_id'])
->get(
[
'advert_id',
'owner_id',
'sender_id',
'id'
]
)
)
->get(
[
'advert_id',
'owner_id',
'sender_id',
'id'
]
);
But i am getting error Method getBindings does not exist., also i can't find how to write "if" clause with eloquent, all i find is when method, but as far as i understood it works just for "if argument exist, add additional clause to your query", what is not what i need.
Solution
$chats = $database::table('chats')
->where('sender_id', '?')
->select(
'id',
'advert_id',
$database::raw('
IF(
`chats`.`owner_id` = ?,
`chats`.`sender_id`,
`chats`.`owner_id`
) AS `sender_id`
'),
$database::raw('
IF(
`chats`.`owner_id` = ?,
`chats`.`owner_access_time`,
`chats`.`sender_access_time`
) AS `access_time`
')
)
->setBindings(
[
$arguments['user_id'],
$arguments['user_id'],
$arguments['user_id'],
]
)
->union(
$database::table('chats')
->where('owner_id', '?')
->select(
'id',
'advert_id',
$database::raw('
IF(
`chats`.`owner_id` = ?,
`chats`.`sender_id`,
`chats`.`owner_id`
) AS `sender_id`
'),
$database::raw('
IF(
`chats`.`owner_id` = ?,
`chats`.`owner_access_time`,
`chats`.`sender_access_time`
) AS `access_time`
')
)
->setBindings(
[
$arguments['user_id'],
$arguments['user_id'],
$arguments['user_id'],
]
)
)
->get();

How to Order a query

I have an array $sorted_array its value is
Array ( [0] => 3 [1] => 1 [2] => 6 )
Now based on the $sorted_array i created an array
$first_array = Yii::app()->db->createCommand()
->select('*')
->from('form_fields')
->where(array('not in', 'id', $sorted_array))
->andWhere('form_id=:form_id', array(':form_id'=>$form_id))
->queryAll();
$sorted_array value is the id (Primary key) of table form_fields.
When i run this query i get the array $first_array but not in the order in which i want it. ie, I will get an array in order $id=1,3,6.
Now my wanted order is 3,1,6 (exactly as $sorted_array). How can i get $first_array in that order?
You can do using ->order() Add order( 'id' ) to your code this way:
$first_array = Yii::app()->db->createCommand()
->select('*')
->from('form_fields')
->where(array('not in', 'id', $sorted_array))
->andWhere('form_id=:form_id', array(':form_id'=>$form_id))
->order('id')
->queryAll();
otherwise if is not possibile build the query you need with query builder you can use
Yii::app()->db->createCommand("select * from your_table")->queryAll();
and for binding param
Yii::app()->db->createCommand(
'select * from your_table where yuor_field =:your_param')->
bindValue('your_param',$yuor_value)->queryAll();
this returns all rows using a specified SQL statement
$sql = "select * from form_fields
where id not in (3,1,6)
and form_id = " . $form_id .
"order by field(id, 3,6,1);";
$first_array = Yii::app()->db->createCommand($sql)->queryAll();

Categories