cakephp-3.x

Include utility class all over the application cakephp 3

别来无恙 提交于 2019-12-08 08:16:11
问题 I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass. I can include the Utility class in controller .e.g. use App\Utility\ArrayUtil; and then call the class in my controllers e.g. ArrayUtil::myMethod(); And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well. I tried to load it in config/bootstrap.php but I get this

Cakephp 3 How to make session array

时光怂恿深爱的人放手 提交于 2019-12-08 04:19:31
问题 I am trying to write session in controller. My structure is $_SESSION['a'][0] = 1; $_SESSION['a'][1] = 2; $_SESSION['a'][2] = 3; And I am trying this Configure::write('Session', ['a' =>'1']) But it is not working. How do this in cakephp 3 way 回答1: To write variable in Session in CakePHP 3 you need to write following code : $this->request->session()->write('Your Key',Your_array); To know more information you can visit here : http://book.cakephp.org/3.0/en/development/sessions.html 回答2: You can

Sort query results by nested association in cakephp 3

前提是你 提交于 2019-12-07 18:23:15
问题 The Problem: I have a cakephp 3.x query object with two nested associations, Organizations.Positions.Skills , that is being set to a view variable, $Organizations . I'm trying to sort the query's resulting top level array by a column in the first nested association. That is, I want to sort $Organizations by a column in Positions , specifically Positions.Ended ). public function index() { $this->loadModel('Organizations'); $this->set('Organizations', $this->Organizations->find('all') ->contain

Include utility class all over the application cakephp 3

落花浮王杯 提交于 2019-12-06 16:06:28
I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass. I can include the Utility class in controller .e.g. use App\Utility\ArrayUtil; and then call the class in my controllers e.g. ArrayUtil::myMethod(); And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well. I tried to load it in config/bootstrap.php but I get this error: Error: Class 'ArrayUtil' not found Any idea You can add this line at the top of the page, be it

CakePHP 3- Showing all error and buildRules messages in controller

泪湿孤枕 提交于 2019-12-06 12:47:32
问题 I have this Model/Table/UsersProfilesTable.php where I have specified all the error messages and buildRules. My intention is to list all the validation errors in the controller while attempting to save the data. The code is mentioned below. // Model/Table/UsersProfilesTable.php class UserProfilesTable extends Table{ public function validationDefault(Validator $validator){ $validator = new Validator(); $validator ->notEmpty("first_name","First name cannot be empty.") ->requirePresence("first

CakePHP 3.x checkbox formatting issue

China☆狼群 提交于 2019-12-06 07:23:58
问题 This may seem obvious to some of you, but I really am struggling to find a straight answer. I've generally googled, as well as read both the CakePHP manual and the API for an answer to the following question: When creating an input, the following code creates the following outputs: // in the view echo $this->Form->input('notes'); // resultant html <div class="input textarea"> <label for="notes">Notes</label> <textarea id="notes" rows="5" name="notes"></textarea> </div> Note: this is

Check if object exists or is empty in view template

一世执手 提交于 2019-12-05 20:12:06
How do you check within the view template if the result object contains any entries? (There was a similar question already, but this one is slightly different) Take the CakePHP 3 blog tutorial for example. They show how to list all articles on one page: // src/Controller/ArticlesController.php public function index() { $this->set('articles', $this->Articles->find('all')); } And the view template: <!-- File: src/Template/Articles/index.ctp --> <table> <tr> <th>Id</th> <th>Title</th> </tr> <?php foreach ($articles as $article): ?> <tr> <td><?= $article->id ?></td> <td> <?= $this->Html->link(

Authorize users based on roles in CakePHP 3

人走茶凉 提交于 2019-12-05 02:34:22
问题 I would like to authorize users based on few roles. All visitors should be able to reach method show. So I wrote in AppController: public function beforeFilter(Event $event) { $this->Auth->allow(['show']); } It works. In initialize() method of AppController I've got also: $this->loadComponent('Auth', [ 'authorize' => 'Controller' ]); I would like to allow logged users with role "user" to reach all "index", and "add" methods, so I wrote in AppController: public function isAuthorized($user) {

CakePHP 3.x checkbox formatting issue

ぐ巨炮叔叔 提交于 2019-12-04 14:15:38
This may seem obvious to some of you, but I really am struggling to find a straight answer. I've generally googled, as well as read both the CakePHP manual and the API for an answer to the following question: When creating an input, the following code creates the following outputs: // in the view echo $this->Form->input('notes'); // resultant html <div class="input textarea"> <label for="notes">Notes</label> <textarea id="notes" rows="5" name="notes"></textarea> </div> Note: this is consistent across most input types; and because it's consistent it's great for formatting. However, with a

QueryBuilder limit is working for only first row in cakephp 3.2 [duplicate]

倖福魔咒の 提交于 2019-12-04 06:29:06
问题 This question already has an answer here : How to limit contained associations per record/group? (1 answer) Closed 3 years ago . Hii i am new to cakephp 3.2 v. Here i have used model association (hasMany). Here in bind section (campaign_videos) ,i want to fetch only one record , so for this ,i have put below code to manage it. my actual data in db. [ { "id": 1, "user_id": 95, "campaign_videos": [ { "id": 1, "campaign_id": 1, }, { "id": 3, "campaign_id": 1, } ] }, { "id": 2, "user_id": 95,