Why I'm getting the error: "Parameter 1 to Credis_Client::scan() expected to be a reference"? - class

I'm trying to call this class function:
public function scan(&$Iterator, $pattern = null, $count = null)
{
return $this->__call('scan', array(&$Iterator, $pattern, $count));
}
From my class:
$itScan = NULL;
while($arr_keys = $this->redisClient->scan($itScan, '', 10000)) {
foreach($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
I understand this is something related to the & pointer but I can't figure who to call it from inside my class.
Thank you!

This is the proper way to iterate over your results :
$i = null;
$allResults = [];
do {
$someResults = $redis->scan($i, "*", 10000);
if (!empty($someResults)) {
$allKeys = array_merge($allKeys, $someResults);
}
}
while ($i);

Related

Laravel returning LogicException "must return a relationship instance."

I am getting the error whenever the function getCips is being called:
App\Models\Employee::getAllCips must return a relationship instance.
The function that is causing this error is:
private function getCips($user, $page, $search){
if($page == null)$page = 1;
$count = 7;
$cips = null;
if(get_class($user) == User::class)
{
$cips = Visitable::all()->whereNotNull('cip');
}else{
$cips = $user->getAllCips(); //The error is caused because of this line.
}
if($search!=null){
$cips = $cips->filter(function($cip, $i)use($search){
return strpos($cip->name, $search)!==false||strpos($cip->email, $search)!==false||strpos($cip->phone, $search)!==false||strpos($cip->location->name, $search)!==false||strpos($cip->cip->Associated_hospital, $search)!==false;
});
}
$lastPage = ($cips->count()%$count == 0)? intdiv($cips->count(), $count):intdiv($cips->count(), $count)+1;
$page = ($cips->count() == 0)?0:intval($page);
return ["cips"=>$cips->values()->skip(($page-1)*$count)->take($count)->all(),"lastPage"=>$lastPage, "currentPage"=>$page];
}
As marked in the line of code above, the $user is an instance of Employee class.
Now the function getAllCips() in class Employee is as:
public function getAllCips():Collection{
$allCips = $this->headQuarter->getAllCips();
foreach($this->subordinates as $subordinate){
$allCips = $allCips->concat($subordinate->getAllCips);
}
return $allCips;
}
I am using Laravel 8. It would be very helpful if anybody could point out what I am doing wrong here.

mybatis interceptor sql log process

i make mybatis sqllog intercepor in my project.
in case. #{__frch_CUST_0} #{__frch_CUST_1} ~~~ #{__frch_CUST_N}
how can i get parameters value "__frch_CUST_n" ?
// mybatis interceptor sql log function
public String getSqlLog(StatementHandler handler)
{
String sql = handler.getBoundSql().getSql();
Object param = handler.getParameterHandler().getParameterObject();
List<ParameterMapping> paramMapping = handler.getBoundSql().getParameterMappings();
// change \? value to replace point
for(ParameterMapping mapping:paramMapping)
{
sql = sql.replaceFirst("\\?", "#{"+mapping.getProperty()+"}");
}
for(ParameterMapping mapping:paramMapping)
{
String sqlparam = "#{"+mapping.getProperty()+"}";
String sqlparamname = mapping.getProperty();
Object sqlparamvalue = ((Map) param).get(sqlparamname);
if( !sqlparamname.startsWith("__frch_") )
{
if(isNull(sqlparamvalue))
{
sql = sql.replace(sqlparam, "NULL");
}
else
{
if(sqlparamvalue instanceof String)
{
try
{
sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
}
catch(Exception e)
{
sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
}
}
else
{
sql = sql.replace(sqlparam, sqlparamvalue.toString());
}
}
}
else
{
**// HOW CAN I DO HERE?**
}
}
return sql;
}
thank you for read my question..
i resolve my self.
mybatis make new arrayList parameter. and use it.
example list value
DATE = ['20180101','20180102','20180103'];
STEP = ['PLAN', 'MAKE', 'SETUP'];
__frch_DATE_0
__frch_DATE_1
__frch_DATE_2
__frch_STEP_3
__frch_STEP_4
__frch_STEP_5
mybatis __frch_ = ['20180101','20180102','20180103','PLAN', 'MAKE', 'SETUP']
so i use it. my question is resolved..
public String getSqlLog(StatementHandler handler)
{
String sql = handler.getBoundSql().getSql();
Object param = handler.getParameterHandler().getParameterObject();
List<ParameterMapping> paramMapping = handler.getBoundSql().getParameterMappings();
List foreachlist = new ArrayList();
for(ParameterMapping mapping:paramMapping)
{
sql = sql.replaceFirst("\\?", "#{"+mapping.getProperty()+"}");
}
for(ParameterMapping mapping:paramMapping)
{
String sqlparam = "#{"+mapping.getProperty()+"}";
String sqlparamname = mapping.getProperty();
Object sqlparamvalue = ((Map) param).get(sqlparamname);
if( !sqlparamname.startsWith("__frch_") )
{
if(isNull(sqlparamvalue))
{
sql = sql.replace(sqlparam, "NULL");
}
else
{
if(sqlparamvalue instanceof String)
{
try
{
sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
}
catch(Exception e)
{
sql = sql.replace(sqlparam, "'"+getSQLString(sqlparamvalue.toString())+"'");
}
}
else
{
sql = sql.replace(sqlparam, sqlparamvalue.toString());
}
}
}
else
{
if( foreachlist.size() == 0 )
{
Map parammap = (Map)param;
Iterator iterator = parammap.keySet().iterator();
while(iterator.hasNext())
{
String key = iterator.next().toString();
Object value = (Object)parammap.get(key);
if( value instanceof List )
{
List valuelist = (List)value;
for(int i=0;i<valuelist.size();i++)
{
foreachlist.add(valuelist.get(i));
}
}
}
}
String buff = sqlparamname.split("__frch_")[1];
int index = Integer.parseInt(buff.substring(buff.lastIndexOf("_")+1));
try
{
sql = sql.replace(sqlparam, "'"+getSQLString((String)foreachlist.get(index))+"'");
}
catch(Exception e)
{
;
}
}
}
return sql;
}

CodeIgniter POST/GET default value

Can I set default value for POST/GET data if it's empty/false, something like
$this->input->post("varname", "value-if-falsy")
?
So I don't have to code like
$a = $this->input->post("varname") ?
$this->input->post("varname") :
"value-if-falsy"
Just found out not very long ago that I can also use ?:, eg.
$name = $this->input->post('name') ?: 'defaultvalue';
You have to override the default behavior.
In application/core create MY_Input.php
class MY_Input extends CI_Input
{
function post($index = NULL, $xss_clean = FALSE, $default_value = NULL)
{
// Check if a field has been provided
if ($index === NULL AND ! empty($_POST))
{
$post = array();
// Loop through the full _POST array and return it
foreach (array_keys($_POST) as $key)
{
$post[$key] = $this->_fetch_from_array($_POST, $key, $xss_clean);
}
return $post;
}
$ret_val = $this->_fetch_from_array($_POST, $index, $xss_clean);
if(!$ret_val)
$ret_val = $default_value;
return $ret_val;
}
}
And then in your controller :
$this->input->post("varname", "", "value-if-falsy")
It works for me, I have used the #AdrienXL trick.
Just create your application/core/MY_Input.php file and call the parent method (you can find this methods inside CodeIgniter system folder system/core/Input.php:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class MY_Input extends CI_Input
{
function post($index = NULL, $default_value = NULL, $xss_clean = FALSE)
{
$value = parent::post($index, $xss_clean);
return ($value) ? $value : $default_value;
}
function get($index = NULL, $default_value = NULL, $xss_clean = FALSE)
{
$value = parent::get($index, $xss_clean);
return ($value) ? $value : $default_value;
}
}
So, when call the method, pass the default value:
$variable = $this->input->post("varname", "value-if-falsy");
You can simplify the code block as below
public function post($index = NULL, $xss_clean = NULL, $default_value = NULL )
{
if( is_null( $value = $this->_fetch_from_array($_POST, $index, $xss_clean ) ) ){
return $default_value;
}
return $value;
}
You can use this code before set_rules
empty($_POST['varname']) ? $_POST['varname'] = 'value if empty' : 0;

How can I programmatically create nested where clauses with Zend\Db\Sql component?

I've tried using the following code to accomplish it, but the unnest function returns an error:
foreach($params as $key=>$param) {
if(strpos($param, ',') !== false) {
$where = new \Zend\Db\Sql\Where();
$where->nest();
$param_arr = explode(',', $param);
$entered_once = 0;
foreach($param_arr as $p) {
$where->equalTo($key, $p);
if($entered_once < count($param_arr)) {
$where->or;
}
$entered_once++;
}
$where->unnest();
$select->where($where);
}
else {
$select->where(array($key => $param));
}
}
I've figured out what I'm doing wrong: I didn't realize that the nest() function returned a brand new PredicateSet and that's what I needed to call everything on. The solution is as follows:
foreach($params as $key=>$param) {
if(strpos($param, ',') !== false) {
$where = new \Zend\Db\Sql\Where();
$predicate_set = $where->nest();
$param_arr = explode(',', $param);
$entered_once = 0;
foreach($param_arr as $p) {
$predicate_set->equalTo($key, $p);
if($entered_once < count($param_arr)) {
$predicate_set->or;
}
$entered_once++;
}
$predicate_set->unnest();
$select->where($where);
}
else {
$select->where(array($key => $param));
}
}

Zend db select how to know return zero rows?

This is my code:
public function is_existing($url) {
$query = $this->select()
->where("url=?",$url);
if(!$query) {
return false;
} else {
$row = $this->fetchRow($query);
$row = $row->toArray();
return $row;
}
}
But sometimes, I receive
Fatal error: Call to a member function toArray() on a non-object in...
Is it right to use if(!$query) to check the existing rows in DB?
Change your code to this
public function is_existing($url) {
$query = $this->select()
->where("url=?",$url);
$row = $this->fetchRow($query);
if(!$row) {
return false;
} else {
return $row->toArray();
}
}
Because $query object will always be created irrespective $url matches record or not hence will always pass if condition .
Is it right to use if(!$query) to check the existing rows in DB ?
No, you should evaluate the result of fetchRow to determine if there are results, fetchRow either returns a Zend_Db_Table_Row or null if no rows are found.
public function is_existing($url) {
$query = $this->select()
->where("url=?",$url);
$row = $this->fetchRow($query);
if(is_null($row)) {
return false;
} else {
return $row->toArray();
}
}