logging all queries after execution in codeigniter - codeigniter

i want to log all queries after execution using hooks.
-i enabled hooks in config.php
-this is my hook-->
$hook['post_controller'] = array(
'class' => 'Db_log',
'function' => 'logQueries',
'filename' => 'db_log.php',
'filepath' => 'hooks'
);
-and this is hook defination-->
class Db_log
{
function __construct()
{
}
function logQueries()
{
$CI = & get_instance();
$filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php';
$handle = fopen($filepath, "a+");
$times = $CI->db->query_times;
foreach ($CI->db->queries as $key => $query)
{
$sql = $query . " \n Execution Time:" . $times[$key];
fwrite($handle, $sql . "\n\n");
}
fclose($handle);
}
}
--its creating query_log file
--but no records of queries being stored

Your Code looks fine - the only reason why this doesn't work is in your DB Configuration - take a look #your DB Connection in the database.php under application/config/
There is an option "save_queries" which should be set to true
$db['default'] = array(
...
'save_queries' => TRUE
);

For me it only worked when I define de $hook array in config/hooks.php .

Related

laravel livewire intervention images

I am trying to use Intervention image with Livewire to reduce the sizes and I am not succeeding. They can guide me or tell me if Livewire may not allow it.
I am trying to pass this methodology:
foreach ($this->imagenes as $pathGaleria) {
$imgUrl = $pathGaleria->store('imagenesPropiedades');
$img = imgPropiedades::create([
'url' => $imgUrl,
'property_id' => $this->propiedadId
]);
to this other way:
foreach ($this->imagenes as $pathGaleria) {
$imgUrl = $pathGaleria->store('imagenesPropiedades');
Image::make($pathGaleria)->resize(1200, null, function ($constraint) {
$constraint->aspectRatio();
})
->save($imgUrl);
$img = imgPropiedades::create([
'url' => $imgUrl,
'property_id' => $this->propiedadId
]);
}
but the page remains blank. Thank you.
I found this today, may work for you
https://gist.github.com/daugaard47/659984245d31b895d00ee5dcbdee44ec
+
$images = Collection::wrap($request->file('file'));
$images->each(function ($image) use ($id) {
$basename = Str::random();
$original = $basename . '.' . $image->getClientOriginalExtension();
$thumbnail = $basename . '_thumb.' . $image->getClientOriginalExtension();
ImageManager::make($image)
->fit(250, 250)
->save(public_path('/images/' . $thumbnail));
$image->move(public_path('/images/'), $original);
Model::create([
]);
});

Codeigniter db queries is not capturing some specific queries

am trying to capture all queries and record a log of interactions, however some queries are not being captured, this specific query for example:
$this->db->update('showtec.motoristas', $motorista);
I capture the queries using the Hooks function like this:
Hooks.php:
$hook['post_controller'] = array(
'class' => 'Db_query_log',
'function' => 'log_db_queries',
'filename' => 'db_log.php',
'filepath' => 'hooks'
);
db_log.php:
<?php
class Db_query_log {
function __construct() {
}
function log_db_queries() {
$CI =& get_instance();
$CI->load->model('auditoria');
$data = array();
$queries = $CI->db->queries;
$data['id_usuario_gestor'] = $CI->auth->get_login('id_user');
foreach ($queries as $query){
if (preg_match($pattern = '/' . 'INSERT' . '/', $query)){
$queryArray = explode("`", $query);
if($queryArray[3] != 'ip'){
$data['query'] = $query;
$data['clause'] = 'insert';
break;
}
}elseif (preg_match($pattern = '/' . 'UPDATE' . '/', $query)){
$data['query'] = $query;
$data['clause'] = 'update';
break;
}elseif (preg_match($pattern = '/' . 'DELETE' . '/', $query)){
$data['query'] = $query;
$data['clause'] = 'delete';
break;
}
}
if(array_key_exists('query', $data)){
$CI->auditoria->salvarAuditoria($data);
}
}
}

Lang sub folder detection codeigniter

In my lang load I would like to be able to try and make it so that if type admin then would pick up subfolder in admin and find the controller lang file. Same as what do with the glob.
How would that be possible for language load function?
Unable to load the requested language file:
language/english/admin/*/dashboard_lang.php
$files = glob(FCPATH . 'application/modules/admin/controllers/*/*.php');
if ($files) {
foreach ($files as $file) {
$controller = basename(strtolower($file), '.php');
$this->lang->load('admin/*'. $controller, 'english');
$data['controller_files'][] = array(
'controller' => $controller,
'install' => '',
'installed' => in_array($controller, $controller_files)
);
}
}
You could do something like this if you wanted to load a language file
in a subfolder with the same name as the "controller".
language/english/admin/dashboard/dashboard_lang.php
$controller = '';
$path = FCPATH . 'application/modules/admin/controllers/*/*.php';
$files = glob($path, GLOB_BRACE);
if(!$files || empty($files)){
log_message('error', "Unable to find any matches : $path");
}
foreach($files as $file){
$basename = basename(strtolower($file));
$pathinfo = pathinfo($basename);
$controller = $pathinfo['filename'];
$this->lang->load("admin/$controller/$controller", 'english');
$data['controller_files'][] = array(
'controller' => $controller,
'install' => '',
'installed' => in_array($controller, $controller_files)
);
}

Uploading an image with Code Igniter?

I'm trying to set a site up so I can upload images using a web form in Code Igniter(CI). I'm not getting any errors but the file is not being saved either. I wanted to know if those that were successful in uploading images could help explain what might be the issue?
View:
<?php
echo form_open_multipart('admin/galleryUpload') . "\n";
echo "<div class='span-8'><span class='text'>Image:</span>" . form_upload('uploadImg') . "</div>";
foreach ($gallery as $picture)
{
$order[] = $picture->order;
}
$order[] = count($order) + 1;
echo "<div class='span-6 last'><span>Image Order #:</span>" . form_dropdown('order', $order) . "</div><div class='span-14'> </div>";
$conf = array('name' => 'alt_text', 'size' => '75');
echo "<div class='span-14 last'><span>Discription:</span>" . form_input($conf) . "<br /></div>";
echo form_hidden('propertyID', "$propertyID");
echo form_submit('upload', 'Upload');
echo form_close();
?>
Controller:
class Admin extends Controller
{
function galleryUpload()
{
if (! $this->session->userdata('is_admin'))
{
redirect('admin/index');
}
else
{
$this->load->model('admin_model');
$this->admin_model->imgUpload();
}
}
}
Model:
class Admin_model extends Model
{
function imgUpload()
{
$id = $this->input->post('propertyID');
$order = $this->input->post('order');
$alt_text = $this->input->post('alt_text');
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
//'upload_path' => '../' . $this->imgPath($id),
'upload_path' => '../img/galleries/temp/',
'max_size' => '5000', // 5MB files max
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->imgPath($id) . '/thumbs',
'maintain_ratio' => TRUE,
'width' => '60'
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
From the user guide:
By default the upload routine expects the file to come from a form
field called userfile, and the form must be a multipart type
So you either have to change the name of the form field to userfile:
form_upload('userfile')
or pass the name of your form field to do_upload:
$this->upload->do_upload('uploadImg');

multiple where condition codeigniter

How can I convert this query to active record?
"UPDATE table_user
SET email = '$email', last_ip = '$last_ip'
where username = '$username' and status = '$status'";
I tried to convert the query above to:
$data = array('email' => $email, 'last_ip' => $ip);
$this->db->where('username',$username);
$this->db->update('table_user',$data);
How about using the where clausa status?
# must i write db->where two times like this?
$this->db->where('username',$username);
$this->db->where('status',$status);
I also tried this:
$this->db->where('username',$username,'status',$status);
you can use an array and pass the array.
Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
Or if you want to do something other than = comparison
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);
Yes, multiple calls to where() is a perfectly valid way to achieve this.
$this->db->where('username',$username);
$this->db->where('status',$status);
http://www.codeigniter.com/user_guide/database/query_builder.html
you can try this function for multi-purpose
function ManageData($table_name='',$condition=array(),$udata=array(),$is_insert=false){
$resultArr = array();
$ci = & get_instance();
if($condition && count($condition))
$ci->db->where($condition);
if($is_insert)
{
$ci->db->insert($table_name,$udata);
return 0;
}
else
{
$ci->db->update($table_name,$udata);
return 1;
}
}
$wherecond = "( ( ( username ='" . $username . "' OR status='" . $status . "') AND (id='" . $id . "') ) )";
$this->db->where($wherecond);
If you want to add AND and OR conditions at a time. this will work.
Try this
$data = array(
'email' =>$email,
'last_ip' => $last_ip
);
$where = array('username ' => $username , 'status ' => $status);
$this->db->where($where);
$this->db->update('table_user ', $data);
it's late for this answer but i think maybe still can help, i try the both methods above, using two where conditions and the method with the array, none of those work for me i did several test and the condition was never getting executed, so i did a workaround, here is my code:
public function validateLogin($email, $password){
$password = md5($password);
$this->db->select("userID,email,password");
$query = $this->db->get_where("users", array("email" => $email));
$p = $query->row_array();
if($query->num_rows() == 1 && $password == $p['password']){
return $query->row();
}
}
you can use both use array like :
$array = array('tlb_account.crid' =>$value , 'tlb_request.sign'=> 'FALSE' );
and direct assign like:
$this->db->where('tlb_account.crid' =>$value , 'tlb_request.sign'=> 'FALSE');
I wish help you.

Resources