CakePHP Xml utility library triggers DOMDocument warning

This is a bug in PHPs DOMDocument::createElement() method. Here are two ways to avoid the problem. Create Text Nodes Create the textnode separately and append it to the element node. $dom = new DOMDocument; $dom ->appendChild($dom->createElement(‘element’)) ->appendChild($dom->createTextNode(‘S & T: ERROR’)); var_dump($dom->saveXml()); Output: string(58) “<?xml version=”1.0″?> <element>S &amp; T: ERROR</element> ” This is the originally intended … Read more

CakePHP remember me with Auth

In your user controller: public function beforeFilter() { $this->Auth->allow(array(‘login’, ‘register’)); parent::beforeFilter(); } public function login() { if ($this->request->is(‘post’)) { if ($this->Auth->login()) { // did they select the remember me checkbox? if ($this->request->data[‘User’][‘remember_me’] == 1) { // remove “remember me checkbox” unset($this->request->data[‘User’][‘remember_me’]); // hash the user’s password $this->request->data[‘User’][‘password’] = $this->Auth->password($this->request->data[‘User’][‘password’]); // write the cookie $this->Cookie->write(‘remember_me_cookie’, $this->request->data[‘User’], … Read more

How do you make strings “XML safe”?

Since PHP 5.4 you can use: htmlspecialchars($string, ENT_XML1); You should specify the encoding, such as: htmlspecialchars($string, ENT_XML1, ‘UTF-8’); Update Note that the above will only convert: & to &amp; < to &lt; > to &gt; If you want to escape text for use in an attribute enclosed in double quotes: htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, ‘UTF-8’); … Read more

How to use different datasources in a Query using cakephp3?

For now, CakePHP doesn’t take datasource configurations into account when creating joins, and I don’t think that this will be added in the near future, not least because cross database joins aren’t supported “out of the box” (as in, just prepend the database name and you’re set) in Postgres and SQLite. Assuming you are using … Read more

How to setup cronjobs in cake php?

Use a shell The ‘Cake Way’ of using a CakePHP application in cron jobs would be creating shell and then calling it as a cron job. i.e. Create a shell to do the task, and then add it to crontab (crontab -e on linux machine): 0 * * * * cd /path/to/app/ && Console/cake your_shell_name … Read more

Cakephp-3.x: How to change the data type of a selected alias?

As of CakePHP 3.2 you can use Query::selectTypeMap() to add further types, which are only going to be used for casting the selected fields when data is being retrieved. $query = $table ->find() ->select([‘alias’ => ‘actual_field’, /* … */]); $query ->selectTypeMap() ->addDefaults([ ‘alias’ => ‘integer’ ]); You can use any of the built-in data types, … Read more

How do I write a join query across multiple tables in CakePHP?

$markers = $this->Marker->find(‘all’, array(‘joins’ => array( array( ‘table’ => ‘markers_tags’, ‘alias’ => ‘MarkersTag’, ‘type’ => ‘inner’, ‘foreignKey’ => false, ‘conditions’=> array(‘MarkersTag.marker_id = Marker.id’) ), array( ‘table’ => ‘tags’, ‘alias’ => ‘Tag’, ‘type’ => ‘inner’, ‘foreignKey’ => false, ‘conditions’=> array( ‘Tag.id = MarkersTag.tag_id’, ‘Tag.tag’ => explode(‘ ‘, $this->params[‘url’][‘q’]) ) ) ))); as referred to in nate … Read more

.htaccess for cakephp

The answer is that there are 3 different .htaccess files: /var/www/app/webroot/.htaccess <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] </IfModule> /var/www/app/.htaccess <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule> /var/www/.htaccess <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] </IfModule> It’s … Read more