cakephp-3.x

Check if record exists in CakePHP3.5

早过忘川 提交于 2019-12-01 05:17:35
The Following Code returns only a error :Record not found in table "users" if($this->Users->get($uid)->isEmpty()) { //do something } Because the Table is empty, i want to customize this if the table is empty, and call a new page in browser Table::get() will immediately evaluate the query and return an entity, or throw an exception if the record with the given primary key doesn't exist, ie it doesn't return a query, you cannot call isEmpty() on the result. If you are using Table::get() you can catch the RecordNotFoundException : try { $user = $this->Users->get($uid); // ... } catch (\Cake

How to get last run query in CakePHP 3.2?

拜拜、爱过 提交于 2019-11-30 04:06:17
问题 I want to get the last executed query in CakePHP 3.2, I have used the following in CakePHP 2.x before:- function getLastQuery() { Configure::write('debug', 2); $dbo = $this->getDatasource(); $logs = $dbo->getLog(); $lastLog = end($logs['log']); $latQuery = $lastLog['query']; echo "<pre>"; print_r($latQuery); } How can i do it in CakePHP 3.x? 回答1: In plain English: All you need to do is modify config/app.php Find the Datasources configuration and set 'log' => true 'Datasources' => [ 'default'

How to match records that are associated with a specific set of other records?

若如初见. 提交于 2019-11-29 18:13:51
I am trying to add to two different search variations to my project. There is a model "User" and a model "Tag". A User has many Tags. Now I want to be able to search the Users with specific Tags. Either I want to show all Users that has any of the specified tags. I got this working this way: $query = $this->Users->find(); $query->matching('Tags', function ($q) { return $q->where(['Tags.name' => 'Tag1']) ->orWhere(['Tags.name' => 'Tag2']); }); But now I want to find all Users that have both Tags at the same time. I tried ->andWhere instead of ->orWhere , but the result is always empty. How can

cakephp 3 Cron Job not working in cpanel

我怕爱的太早我们不能终老 提交于 2019-11-29 14:57:34
I am trying to implement cron job in cakephp 3 shell script but it is not working in cpanel. below is my cron job code blog is my cakephp 3 folder cd /home/mmentert/public_html/abc.com/blog && bin/cake hello main Cakephp 3 shell class file namespace App\Shell; use Cake\Console\Shell; use App\Controller\UsersController; class HelloShell extends Shell { public function main() { $userinfo=new UsersController(); $data=$userinfo->useremail(); $this->out($data); } } I assume you are using shared hosting, the syntax suggested on CakePHP 3 Docs does not work for shared hosting, this is what worked for

How to select fields of a contained association?

前提是你 提交于 2019-11-29 12:44:01
I would like to execute the following query where I'd like to read only the necessary fields from associated. I used a dot notation in select() below to better explain what I want. Basically the select() seems to concern Users only. Is it possible to specify the fields of Sites ? $orders = $this->Orders->find() ->contain([ 'Sites.Users'=> function ($q) { return $q ->select([ 'Sites.id', 'Sites.user_id', 'Users.id', 'Users.name', 'Users.owner_id', 'Users.firstname', 'Users.lastname' ]) ->autoFields(false); }, ]) ->first(); You have to configure the containments individually, selecting fields

How to paginate associated records?

喜你入骨 提交于 2019-11-28 02:25:22
Products belongsToMany Categories and Categories hasMany Products , inside my Product view I'm showing a list of all it's categories but I want to paginate or limit these results. My current code on ProductsController is: $product = $this->Products ->findBySlug($slug_prod) ->contain(['Metas', 'Attachments', 'Categories']) ->first(); $this->set(compact('product')); I know I need to set $this->paginate() to paginate something but I can't get it working to paginate the categories inside the product. I hope you guys can understand me. UPDATE: Currently I have this going on: $product = $this-

POST Requests for CakePHP 3 API are not working

馋奶兔 提交于 2019-11-27 08:14:59
问题 I am developing an API using CakePHP 3.x documentation. To develop this API I am using their official documentation: https://book.cakephp.org/3.0/en/development/rest.html When I try to access my api using GET request on url http://localhost/healthcare_portal/eapi/applicants/index.json, I get follow expected json result { "applicants": [ { "applicant_id": 1, "name": "Manender" }, { "applicant_id": 2, "name": "mayank" } ] } But when I access my api using POST request on same url http:/

How to paginate associated records?

£可爱£侵袭症+ 提交于 2019-11-26 23:42:57
问题 Products belongsToMany Categories and Categories hasMany Products , inside my Product view I'm showing a list of all it's categories but I want to paginate or limit these results. My current code on ProductsController is: $product = $this->Products ->findBySlug($slug_prod) ->contain(['Metas', 'Attachments', 'Categories']) ->first(); $this->set(compact('product')); I know I need to set $this->paginate() to paginate something but I can't get it working to paginate the categories inside the

How to output custom HTTP body contents with CakePHP 3.4? Echoing causes “Unable to emit headers” error

北城以北 提交于 2019-11-26 23:08:45
Using CakePHP 3.4, PHP 7.0. I'm attempting to do a really simple controller method to output some JSON. It is outputting "Cannot modify headers...". public function test() { $this->autoRender = false; echo json_encode(['method' => __METHOD__, 'class' => get_called_class()]); } Browser output {"method":"App\\Controller\\SomeController::test", "class":"App\\Controller\\SomeController"} Warning (512): Unable to emit headers. Headers sent in file=... Warning (2): Cannot modify header information - headers already sent by (output started at ...) Warning (2): Cannot modify header information -

How to output custom HTTP body contents with CakePHP 3.4? Echoing causes “Unable to emit headers” error

六眼飞鱼酱① 提交于 2019-11-26 08:34:05
问题 Using CakePHP 3.4, PHP 7.0. I\'m attempting to do a really simple controller method to output some JSON. It is outputting \"Cannot modify headers...\". public function test() { $this->autoRender = false; echo json_encode([\'method\' => __METHOD__, \'class\' => get_called_class()]); } Browser output {\"method\":\"App\\\\Controller\\\\SomeController::test\", \"class\":\"App\\\\Controller\\\\SomeController\"} Warning (512): Unable to emit headers. Headers sent in file=... Warning (2): Cannot