Wednesday, November 7, 2018

How can I use my own external class in CakePHP 3.0?

What are the naming conventions? Do I need to use a specific namespace?
Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net/manual/en/language.namespaces.rationale.php
Where do I put the file that contains the PHP class?
  1. Create a folder by author(here might be your name, as you are the author) in vendor
  2. Then create your class inside of it convention is vendor/$author/$package . You can read more http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
How can I include it and use it in a controller or a view?
a) To include:
require_once(ROOT .DS. 'Vendor' . DS . 'MyClass' . DS . 'MyClass.php');
(replace MyClass by your foldername and MyClass.php by your filename.php)
b) To use it:
add use MyClass\MyClass; in your controller
For example I want to add MyClass in a controller. Steps that worked for me
  1. Creating vendor\MyClass folder
  2. Pasting MyClass.php in that folder
  3. adding namespace MyClass; at the top of MyClass.php
MyClass.php have following code for example:
namespace MyClass;


class MyClass
{
    public $prop1 = "I'm a class property!";

    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }

    public function getProperty()
    {
        return $this->prop1 . "<br />";
    }
}
  1. Adding use MyClass\MyClass; at the top of controller
  2. Then including it in my controller action. My action sample
       public function test()
     {
         require_once(ROOT .DS. "Vendor" . DS  . "MyClass" . DS . "MyClass.php");
    
         $obj = new MyClass;
         $obj2 = new MyClass;
    
         echo $obj->getProperty();
         echo $obj2->getProperty();
         exit;
     }

cakephp : how to use mysql now() function in cakephp for date fields?

I have this code in my cakephp project:
$this->data['MyObject']['expire_date'] = 'NOW()';
and...
$this->MyObject->save($this->data);
But this won't save data in database. After googling I found I can use now() like this. But it seems wrong, what is the correct way to use it?
PS: My MySQL field is of type 'DATE'. It works if I use like this:

It should be called like this: $db = $this->MyObject->getDataSource(); $this->data['MyObject']['expire_date'] = $db->expression('NOW()'));

Cakephp: Multiple save in a for loop for cakephp

I'm using 2 databases. I fetch data from one database for a particular id from multiple tables and save it in another database. While I'm doing this, the data which requires a for loop returns the same data multiple times, instead of different data. Please help me in rectifying the error. Below is the code:

When saving data in a loop you need to call
$this->YourModel->clear();
after every save.
$q5 = $this->Model->function($empcode[$j]);
  if (!empty($q5)) {
    for ($i = 0; $i < count($q5); $i++) {
      $name = $q5[0][0]['tf_rel_fname'] . ' ' . $q5[0][0]['tf_rel_mname'] . ' ' . $q5[0][0]['tf_rel_lname'];
      $data5 = array('emp_cd' => $empcode[$j],
        'ppo_number' => $empcode[$j],
        'family_member_name' => $name,
        'relationship' => $q5[0][0]['tf_rel_cd'],
        'is_family_pensioner' => 'N',
        'dob' => date('Y-m-d', strtotime($q5[0][0]['tf_rel_dob'])),
        'disability_percentage' => $q5[0][0]['ph_disab_per'],
        'physically_handicapped' => $q5[0][0]['ph_cd'],
      );
      $this->ppmfamily_details->clear();
      $this->ppmfamily_details->create(false);
      $this->ppmfamily_details->save($data5);
    }
  }

https://stackoverflow.com/questions/41888382/multiple-save-in-a-for-loop-for-cakephp

Wednesday, October 31, 2018

Laravel 5.5 create custom facade example from scratch

In this article, here i will give you step by step guide to create a custom facade in Laravel 5.5 application. You can also learn how to create custom service provider and custom aliases for your class in laravel 5.
Ok So, We may need to create custom facade for creating general helper, some method that always use in whole application like here in example, i created getUserImage() method for getting pull path of user image, Here i will check file is exist or not on following on path then return full path. But it is as an example, So that way you can create your method that you reuse in whole application.
So, Just follow few step to create custom facades in Laravel 5.5 application

How To Create Custom Helper In Laravel 5.5

Today in this article we are share with you how to make custom helper in laravel. you need some time make your own custom helper for some repeted code or logic. like get fullname of current user. convert currency helper, number formate etc...
So, you can create above all logic in one time in one helper function then you can use this custom helper function in everywere in laravel application. you use this custom helper in your laravel blade file and also use in any controller.
in this article we are make one custom helper which get current user full name and how to use this custom helper in blade file and laraval controller.
Please follow step to create custom helper in laravel application

How to Create a Laravel Helper

As we discussed earlier, there are plenty of helpers available in the core of the Laravel framework. They are grouped together based on the functionality they provide. Here’s a list of helper groups.
Helpers in this group provide functionality to manipulate array elements. More often than not, you're in the situation where you want to perform different operations on array elements. So this is the place where you should look first to see if what you're looking for already exists.
I find helpers in this category most useful. They return the fully qualified path of different directories like app, storage, config, and the like. I bet you're using most of these helpers already in your Laravel application.
String manipulation is something inevitable in your day-to-day application development. Although there are plenty of string manipulation functions provided by PHP itself, you'll find a few more useful goodies in this section.
You'll find very few in this category, but they are used throughout the application. They are used to generate route, asset, and form action URLs.
This category contains helpers that provide a variety of functionalities ranging from logging to debugging and many more.