cakephp-3.0

Is it possible to use AppController on error pages? (Cakephp 3.1)

ぐ巨炮叔叔 提交于 2019-12-10 18:33:28
问题 I am trying to render error templates (eg error400.ctp) but with the default layout (site header and footer) which relies on components and variables set in AppController. How do I tell Cake to use AppController when rendering error pages? I have already tried making an ErrorController which extends AppController, but it breaks for missing actions. 回答1: Here's my working ErrorController in case anyone comes looking for it: <?php namespace App\Controller; use App\Controller\AppController; use

CakePHP 3. Containable select

自古美人都是妖i 提交于 2019-12-10 17:33:35
问题 I have a many to many relation where TrainingPrograms can contain many Exercises. They are a linked via the linktable ExercisesTrainingPrograms. I want to select certain fields from my exercises: $trainingPrograms = $this->TrainingPrograms->find() ->contain(['Exercises' => function ($q) { return $q ->select(['id','name','description']); } ]) ->select(['id','name','description']) ->where(['user_id' => $this->Auth->user('id')]); The result i get looks like so: "trainingPrograms": [ { "id": 1,

CakePHP 3 - Catch Error

孤街醉人 提交于 2019-12-10 17:12:38
问题 use Cake\Core\Exception\Exception; for($i=1; $i<count($values); $i++) { $entity = $table->newEntity(); // irrelevant code try { $table->save($entity); } catch (Exception $e) { $errors[$i-1] = $values[$i]; } finally { if(count($errors) == 0) $this->Flash->success('All rows are successfully imported. '); else { $this->Flash->error('Not all rows are successfully imported. '); debug($errors); } } } What I want to do is catch the conflicted entities and show these to the user. What I get is an PDO

MongoDB configuration in CakePHP 3.x

落爺英雄遲暮 提交于 2019-12-10 16:37:44
问题 All data I found on the Internet is about CakePHP V2. In V3 I can't configure MongoDB with cakePHP 3. I dont know how to configure datasource for mongoDB. My Default Databse Configuration is as follows: 'Datasources' => [ 'default' => [ 'className' => 'Cake\Database\Connection', 'driver' => 'Cake\Database\Driver\Mysql', 'persistent' => false, 'host' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'users', 'encoding' => 'utf8', 'timezone' => 'UTC', 'cacheMetadata' => true

CakePHP 3.0 Can't save hasMany associated data

此生再无相见时 提交于 2019-12-10 14:58:51
问题 I've problem when saving data with hasMany association This is my table 1) post table: each item has an unique id. id | title | ... 1 | Aloha | ... 2) images table id | post_id | image | ... 1 | 1 | abc.jpg | ... 2 | 1 | efg.jpg | ... My Model (Table) Posts Model // PostsTable.php <?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\Table; use Cake\Validation\Validator; class PostsTable extends Table { public function initialize(array $config) { $this->table('posts'); $this-

CakePHP 3 manual installation

徘徊边缘 提交于 2019-12-10 12:35:13
问题 Currently I use CakePHP 2.6.8. I want to migrate to CakePHP 3. Intallation guide recommends to use Composer. But composer doesn't make all jobs done, and documentation isn't clear. So I need a step by step guide. Current input is 2.x directory structure: https://github.com/cakephp/cakephp/tree/2.6.8 Basicly which files and folders should be changed for version 3 ? For example lib/Cake files here and here. How can we join them ? https://github.com/cakephp/cakephp/tree/3.0.3 https://github.com

Cakephp3 using recursive function in view cells

杀马特。学长 韩版系。学妹 提交于 2019-12-10 11:53:08
问题 I want to use this Recursive Function to convert a php array into an array of parent and childs. I have a this Cell: class MenuCell extends Cell { public function display($menu) { $this->loadModel('Menus'); $menus = $this -> Menus -> find('all', [ 'contain' => ['MenuItems'] ]) -> where(['id' => $menu]); $menus = $this -> buildTree($menus); $this -> set(compact('menus')); $this -> set('_serialize', ['menus']); } public function buildTree($ar, $pid = null) { $op = array(); foreach($ar as $item)

How to check CSRF token using AJAX and CakePHP 3 when user is not logged in?

半城伤御伤魂 提交于 2019-12-10 11:47:04
问题 So I made my site live and I am entering into the public realm where people aren't always nice. I just started learning about CSRF and saw that it was something I needed when I made my cakephp 3 site live. As seen here! I added the csrf component and the security component to my site, but I have 1 major problem. Now, when users want to sign up they can't. I use a custom form for stripe to send payment, but also add a user using ajax to my database. The user gets added first and then the

Convert time object of cakephp 3 in Y-m-d format

♀尐吖头ヾ 提交于 2019-12-10 11:31:51
问题 I am working in cakephp 3 and I want to print my time object in Y-m-d format. This is my object 'expiry' => object(Cake\I18n\Time) { 'time' => '2015-07-31T00:00:00+0000', 'timezone' => 'UTC', 'fixedNowTime' => false }, Can anyone help me with this? 回答1: In Cakephp 3.* you use the Time::i18nFormat() method. It worked for me $customformat = $date->i18nFormat('YYY-MM-dd'); Edit: I looked through the 3.3 docs and found this good example. There are constants you can use to format dates for common

Session accessibility in model and behavior

那年仲夏 提交于 2019-12-10 10:39:03
问题 I am working in Cake 3.x and need the logged in users id. In Cake 2.x you were able to get this via the session or the AuthComponent (how dirty) AuthComponent::user(); But now in Cake 3.x... How am I able to access the Session in a clean way to get the users id? I've found: http://book.cakephp.org/3.0/en/development/sessions.html But it says it's easy to get the session from Controllers, Components, Views, and more... EDIT: A very very dirty solution could be to get the $_SESSION variable.