PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label cakephp-3.6. Show all posts
Showing posts with label cakephp-3.6. Show all posts

Friday, March 18, 2022

[FIXED] Cakephp 3: execute custom command from command

 March 18, 2022     cakephp, cakephp-3.6, php     No comments   

Issue

In CakPHP 3.6.0 Console Commands have been added to replace Shells and Tasks long term.

I'm currently designing a cronjob command to execute other commands in different time intervals. So I want to run a command from a Command class like this:

namespace App\Command;
// ...

class CronjobCommand extends Command
{
    public function execute(Arguments $args, ConsoleIo $io)
    {
        // Run other command
    }
}

For Shells / Tasks it is possible to use Cake\Console\ShellDispatcher:

$shell = new ShellDispatcher();
$output = $shell->run(['cake', $task]);

but this does not work for Commands. As I did not find any information in the docs, any ideas how to solve this problem?


Solution

You can simply instantiate the command and then run it, like this:

try {
    $otherCommand = new \App\Command\OtherCommand();
    $result = $otherCommand->run(['--foo', 'bar'], $io);
} catch (\Cake\Console\Exception\StopException $e) {
    $result = $e->getCode();
}

CakePHP 3.8 will introduce a convenience method that helps with this. Quote from the upcoming docs:

You may need to call other commands from your command. You can use executeCommand to do that::

// You can pass an array of CLI options and arguments.
$this->executeCommand(OtherCommand::class, ['--verbose', 'deploy']);

// Can pass an instance of the command if it has constructor args
$command = new OtherCommand($otherArgs);
$this->executeCommand($command, ['--verbose', 'deploy']);

See also https://github.com/cakephp/cakephp/pull/13163.



Answered By - ndm
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, January 14, 2022

[FIXED] Filter all find queries on entity CakePHP 3.6

 January 14, 2022     cakephp, cakephp-3.6, php, php-7     No comments   

Issue

let's say that I have an articles database and I want to only display articles that are published on the site (where published = 1). Instead of adding the condition in every find queries like the following:

$articles = $this->Articles->find('all')->where(['published' => 1]);

Is there a way that I can automatically apply this condition on all the find queries in the whole application at one place? If so how?


Solution

You can use beforeFind. This will be fired before every find query on your Article Model. Here is the documentation

Here is how to use it

public function beforeFind($event, $query, $options, $primary)
{

    $query->where(['article.visible' => 1]);

    return $query;
}


Answered By - ascsoftw
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, December 30, 2021

[FIXED] CakePHP 3.6 - Find All Records Created Between Two Dates

 December 30, 2021     cakephp, cakephp-3.6, cakephp-3.x     No comments   

Issue

I am looking for all records between two dates

My variables

 $start = '01/01/2009';
 $end = '07/24/2019';

I have tried

$gross = $this->CartOrders->find('all')->where(['placed >=' => $start])->andWhere(['placed <=' => $end])->all();

Query Snippet for above

... FROM cart_orders CartOrders 
WHERE (placed >= :c0 AND placed <= :c1) 
[params] => Array ( 
     [:c0] => Array ( [value] => 01/01/2009 [type] => datetime [placeholder] => c0 ) 
     [:c1] => Array ( [value] => 07/24/2019 [type] => datetime [placeholder] => c1 ) )

Results in

 Cake\ORM\ResultSet Object ( [items] => Array ( ) )

I have also tried

$gross = $this->CartOrders->find('all')->where(function($exp) use($start,$end) {
        $exp->lte('placed', $end);
        $exp->gte('placed', $start);
        return $exp;
    })->all();

I also have tried

$gross = $this->CartOrders->find('all')->where(function($q) use($start,$end) {
        return $q->between('CartOrders.placed', $start, $end, 'date');
    })->all();

Any ideas on how I can accomplish this?


Solution

This turned out to be a date format issue.

The following solved my problem.

$start = '01/01/2009';
$end = '07/24/2019';

$start = DateTime::createFromFormat('d/m/Y', $start);
$end = DateTime::createFromFormat('d/m/Y', $end);

$gross = $this->CartOrders->find('all')->where([
     'placed >=' => $start->format('Y-m-d')
])->andWhere([
     'placed <=' => $end->format('Y-m-d')
])->all();

This link helped

PHP convert date format dd/mm/yyyy => yyyy-mm-dd



Answered By - Jeffrey L. Roberts
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing