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