Aug 28, 2015

CakePHP and PHP version

I was trying to code at my project. It was OK on my localhost webserver. But... when I moved the files on the real webserver I had a problem.
I didn't know what could that be. I got errors just on some pages. Not at all pages of my website.
Then, I realized that, perhaps, my version of PHP could be too old. Actually, not my version but real webserver version.

Indeed, the problem it was with PHP version which is 5.3 and this version doesn't know about the arrays declared like this:

$ar = ['key1' => 'value1', 'key2' => 'value2'];

It knows only like this:

$ar = array('key1' => 'value1', 'key2' => 'value2');

I got this issue when I tried to make a Form and I finished to have this PHP code

echo $this->Form->input('name', [
                'label'=>[
                    'class' =>  'ContactInput',
                    'text'  =>  'Name:' . $messgError['name']
                ]

            ]);



Aug 26, 2015

CakePHP 2 - Forms

When you try to create a new form in CakePHP framework using FormHelper, you may have this error:

Missing Database  Table
And if you read very carefully  the Book of CakePHP will see something like that:

You can also pass false for $model. This will place your form data into the array: $this->request->data (instead of in the sub-array: $this->request->data['Model']). This can be handy for short forms that may not represent anything in your database.
So, in your view file like APP/View/Contact/index.ctp, you have  to start with this line:

echo $this->Form->create(false, array('url' => '/contact'));

Good luck!

Aug 19, 2015

CakePHP 2.* how to use custom complex sql Query with model

Let's suppose that we have MySQL database with table categories:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category` varchar(64) DEFAULT NULL,
  `description` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;


Then, in CakePHP framework, you want to call a simple query as you did it with mysql_query in PHP with no framework.

For that you have the class Model::query()

First, you build the model. You don't want to use predefined Models for a table (like the model Categories for DB table Categories). Because you want to use perhaps this query for extracting data from two tables which are related.

So,  first you have to create the model. For this you have to create the file APP/Model/Categs.php



class Categ extends AppModel {

    public $useTable = false; // it is mandatory to have this line. Whithout this line, 
                              //                                    the Controller will search for table Categ; it doesn't matter that you have a sql query  

    public function getDataFromMyTable(){
        $d = $this->query("SELECT * FROM  categories");
        return $d;
    }

}


Second, you have to create the Controller. The file will be: APP/Controller/CategsController.php


class CostsController extends AppController {
    
    
    public function index(){
        $this->loadModel('Categ');
    
        $rezQuery = $this->Categ->getDataFromMyTable();
        $this->set('cs', $rezQuery);
    }
}
?>


And now we have to create the view. We create folder APP/View/Categs (if this folder doesn't exists). And then we create the file index.ctp into this folder. The name of the ctp file should be the  same as name of the function from Controller. In index.ctp we write:


Aug 14, 2015

CakePHP and .ctp files

If you use CakePHP as PHP framework then you'll see some different types of files: CTP. And you'll need to see them as a php file in your PHP editor, Eclipse. For this you'll do this:

Preference>General>Content Types>Php then add *.ctp

And then you need to restart Eclipse IDE for have an effect.



CakePHP

Last 2 weeks I tried to put ZF2 to my project www.mateonline.net. It was endless pain to configure Zend Framework 2 for me as a beginner with this. ZF2 as syntax is very nice. But something got wrong with server configuration.
So, then I said I will try Symfony 2. But, I quit immediately. I though it will be easier, but it is no so nice as ZF2.
And, then I pass to CakePHP. Which I like. Except the fact that it has no PDO. I don't like  how is linked to MySQL DB but I can use to it.

Aug 3, 2015

PHP namespace

When you use namespace on PHP scripts you have to use require(), require_once(), include() or include_once() . It doesn't work without one of these like you can see on this example:

php namespaces are case-insensitive:

file1.php:
namespace foo;

function 
bar()
{
    echo 
'from bar';
}
?>
file2.php:


fOo
\bar() // output: from bar
?>
This example is here:
http://php.net/manual/en/language.namespaces.rationale.php

A better example is here:
http://www.sitepoint.com/php-53-namespaces-basics/