Cakephp gives a limit on associative/relational data.

Example we have two tables, one is articles and another is comments table. Articles hasMany relations with comments. 

Example our articles table looks like 

articles 

id |  title | created | modifier 

Comments

Id | comment | article_id | created | modified 

Assuming in articles table we have build  a  hasMany relation with comments like below example

 

$this->hasMany('Comments', [
     'foreignKey' => 'article_id',
 ]);

Here, article_id is the foreign key.

Now, in the ArticlesController/index method we will write a query where we want only the last two comments. 

So, we can write our index method like below

public function index()
{
   $article = $this->Articles->find()
       ->contain('Comments', function($q){
       return $q
              ->limit(2)
              ->order(['Comments.created' => 'DESC']);
       }
   );
   $this->set(compact('articles'));
}

So now in view you will get articles with 2 recent comments.

CakePHP sum a column then group by month.

I have below sql query to get summation of a column name amount , where the result will be grouped by month.

SELECT 
  MONTHNAME( created ) as monthname,
  sum( amount ) as total
FROM 
  transactions
GROUP BY
  monthname

Example, In transactions table I have an amount field where the amount is a numeric value field and transactions have created and modified fields.

So, we can write this query in query builder like below way

public function index()
{
    $query = $this->Transactions->find();
    $query->select([
        ‘monthname’ => $query->func()->monthname([
            ‘created’ => ‘identifier’
         ]),
         ‘total’ => $query->func()->sum(‘Transactions.amount’)
    ])
    ->group([‘month’]);
}

After iteration we should get the result like below

monthname  total
July                    100
May                  300
….. …..

What is VAR hosting in JavaScript ?

If you write a variable and assigned it before declare, like below example

a = 10
console.log(a+20)
var a

It will work perfectly.
Here, I have assigned a variable by value 10, and I also used this variable in console.log() , and it has given output 30.
This behavior is called “hoisting“. Where a variable can appear to be used before it’s declared.

In JavaScript new feature they has introduced LET . Now if you change var a  to let a . You will get an error. If we want to avoid var hosting we can write like below example.

let a
a = 10 
console.log(a+20)

Display a simple object using Vue js.

We will display below object using Vue js

links: [
   {
     name:'facebook',
     href:'http://facebook.com'
   },
   {
     name:'google',
     href:'http://google.com'
   },
   {
     name:'yahoo',
     href:'http://yahoo.com'
   }
]

We have some link name and url. We will display it in a list.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Document</title>
   </head>
   <body>
      <div class="container" id="el">
      <p>{{name}}</p>
      <ul>
         <li v-for='link in links'>
         <a :href='link.href'>
            {{ link.name }}
         </a>
         </li>
      </ul>
      </div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<script>
   var vue = new Vue({
   el:'#el',
   data: {
     name:'Top Nav',
     links: [
             {
                name:'facebook',
                href:'http://facebook.com'
             },
             {
                name:'google',
                href:'http://google.com'
             },
             {
                name:'yahoo',
                href:'http://yahoo.com'
             }
     ]
   }
 });
</script>
</body>
</html>

Output :

CakePHP 3 : Change Default Layout

To change default layout in CakePHP, We need to go in cakephp AppController.php

In AppController we have to write cakephp beforeFilter method.
beforeFilter() method is executed before every action in the controller.

It will like below example :

Location : src/Controller/AppController.php

Note : Write this method after initialize() method in AppController
Continue reading “CakePHP 3 : Change Default Layout”