How to paginate associated records?

The paginator doesn’t support paginating associations, you’ll have to read the associated records manually in a separate query, and paginate that one, something along the lines of this: $product = $this->Products ->findBySlug($slug_prod) ->contain([‘Metas’, ‘Attachments’]) ->first(); $categoriesQuery = $this->Products->Categories ->find() ->innerJoinWith(‘Products’, function (\Cake\ORM\Query $query) use ($product) { return $query->where([ ‘Products.id’ => $product->id, ]); }) ->group(‘Categories.id’); $paginationOptions … Read more

How to output serialized JSON view data as an array of objects, instead of wrapped in an outer object?

Set a string for the _serialize option instead of an array. An array indicates that there might be multiple view vars that need to be serialized, and that requires them to be packed into separate object properties. $this->set(array( ‘platformusers’ => $platformusers, ‘_serialize’ => ‘platformusers’ )); That should give you the desired result.

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

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

Undefined variable: $_SESSION

You need make sure to start the session at the top of every PHP file where you want to use the $_SESSION superglobal. Like this: <?php session_start(); echo $_SESSION[‘youritem’]; ?> You forgot the Session HELPER. Check this link : book.cakephp.org/2.0/en/core-libraries/helpers/session.html