# |
Jun 22nd 2021, 11:12 |
kevin.pfeifer |
``` $cookie = Cookie::create( 'remember_my_data', $data ); $this->response = $this->response->withCookie( $cookie );``` |
# |
Jun 22nd 2021, 11:10 |
neon1024 |
`$this->getRequest()->getCookieCollection()->add($cookie);` I’ve tried this and no cookie shows in my browser debug tools |
# |
Jun 22nd 2021, 11:10 |
neon1024 |
Stupid question but how do I write a cookie which will persist across requests? |
# |
Jun 22nd 2021, 10:21 |
kevin.pfeifer |
even if it is tidious |
# |
Jun 22nd 2021, 10:21 |
kevin.pfeifer |
otherwise I would personally just go from error to error till it works ^^ |
# |
Jun 22nd 2021, 10:20 |
drashti.b.patel |
ok will check thanks for the help |
# |
Jun 22nd 2021, 10:19 |
neon1024 |
Ahh, just found `$this->getRequest()->getSession()->id()` |
# |
Jun 22nd 2021, 10:19 |
kevin.pfeifer |
never done it myself so no promises :shrug: |
# |
Jun 22nd 2021, 10:18 |
kevin.pfeifer |
but I have heard that it can be quite finicky to setup |
# |
Jun 22nd 2021, 10:18 |
kevin.pfeifer |
there is a rector tool which partially upgrades your code https://book.cakephp.org/4/en/appendices/4-0-upgrade-guide.html |
# |
Jun 22nd 2021, 10:17 |
drashti.b.patel |
Or Is there any shortcut or any command line to update it |
# |
Jun 22nd 2021, 10:16 |
neon1024 |
I was looking at `Session::create()` but doesn’t seem to be it |
# |
Jun 22nd 2021, 10:16 |
drashti.b.patel |
In most of the controller I have used the initialize() function Do I need to set the return param every where? |
# |
Jun 22nd 2021, 10:16 |
neon1024 |
Anyone know how I can restore / reconnect to a session by using the ID from the Cookie? |
# |
Jun 22nd 2021, 10:15 |
drashti.b.patel |
public function middleware($middlewareQueue) and when I fixed it to public function middleware($middlewareQueue): MiddlewareQueue |
# |
Jun 22nd 2021, 10:14 |
kevin.pfeifer |
which does not have the same parameters as the core one |
# |
Jun 22nd 2021, 10:14 |
kevin.pfeifer |
your `src/Application.php` has a middleware function |
# |
Jun 22nd 2021, 10:14 |
kevin.pfeifer |
yes, you now need to adjust your code according to the errors shwon |
# |
Jun 22nd 2021, 10:13 |
drashti.b.patel |
It worked for the installation but after that when I tried to run application showing below error: Child process error (exit code 255): PHP Fatal error: Declaration of App\Application::middleware($middlewareQueue) must be compatible with ``` Cake\Http\BaseApplication::middleware(Cake\Http\MiddlewareQueue $middlewareQueue): Cake\Http\MiddlewareQueue in /var/www/html/cp-kickpages/src/Application.php on line 72 |
# |
Jun 22nd 2021, 10:13 |
slackebot1 |
Fatal error: Declaration of App\Application::middleware($middlewareQueue) must be compatible with Cake\Http\BaseApplication::middleware(Cake\Http\MiddlewareQueue $middlewareQueue): Cake\Http\MiddlewareQueue in /var/www/html/cp-kickpages/src/Application.php on line 72``` |
# |
Jun 22nd 2021, 10:09 |
drashti.b.patel |
ok |
# |
Jun 22nd 2021, 10:09 |
kevin.pfeifer |
try to remove the composer.lock and vendor folder and then re-install everything with ```composer install``` |
# |
Jun 22nd 2021, 10:08 |
kevin.pfeifer |
|
# |
Jun 22nd 2021, 10:04 |
slackebot1 |
"dereuromark/cakephp-tools": "^2.0", "cakephp/debug_kit": "^4.0.0", "stripe/stripe-php": "^7.24", "paypal/rest-api-sdk-php": "*", "phpunit/phpunit": "^8.5", "cakephp/bake": "^2.0.3", "cakephp/cakephp-codesniffer": "~4.1.0" }, "require-dev": { "psy/psysh": "@stable", "phpstan/phpstan": "^0.12.18" }, "suggest": { "phpunit/phpunit": "Allows automated |
# |
Jun 22nd 2021, 10:04 |
slackebot1 |
tests to be run without system-wide install.", "cakephp/cakephp-codesniffer": "Allows to check the code against the coding standards used in CakePHP." }, "autoload": { "psr-4": { "App\\": "src", "Elements\\": "./plugins/Elements/src", "Admin\\": "plugins/Admin/src/" } }, "autoload-dev": { "psr-4": { "App\\Test\\": "tests", |
# |
Jun 22nd 2021, 10:04 |
slackebot1 |
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests", "Elements\\Test\\": "./plugins/Elements/tests", "Admin\\": "plugins/Admin/src/" } }, "scripts": { "post-install-cmd": "App\\Console\\Installer::postInstall", "post-create-project-cmd": "App\\Console\\Installer::postInstall", "//post-autoload-dump": "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump" }, |
# |
Jun 22nd 2021, 10:04 |
slackebot1 |
"minimum-stability": "stable", "prefer-stable": true } |
# |
Jun 22nd 2021, 10:04 |
drashti.b.patel |
{ "name": "cakephp/app", "description": "CakePHP skeleton app", "homepage": "http://cakephp.org", "type": "project", "license": "MIT", "require": { "php": ">=5.5.9", "cakephp/cakephp": "^4.0", "mobiledetect/mobiledetectlib": "2.*", "cakephp/migrations": "^3.0@beta", "cakephp/plugin-installer": "*", "dereuromark/cakephp-queue": "^5.4", |
# |
Jun 22nd 2021, 10:03 |
kevin.pfeifer |
can you share your composer.json? |
# |
Jun 22nd 2021, 10:00 |
drashti.b.patel |
I tried to downgrade those version and it is showing the lower version in composer.json file but while running this commend always showing this |
# |
Jun 22nd 2021, 09:52 |
steinkel |
you can also try this nice tool done by @dereuromark https://toolbox.dereuromark.de/utilities/upgrade |
# |
Jun 22nd 2021, 09:52 |
kevin.pfeifer |
your installed plugins require either cakephp 4.1 or 4.2 |
# |
Jun 22nd 2021, 09:52 |
kevin.pfeifer |
try ```composer require --update-with-dependencies "cakephp/cakephp:^4.2"``` |
# |
Jun 22nd 2021, 09:48 |
drashti.b.patel |
Hi everyone, I am upgrading cakephp version from 3.9 to 4.0. I am following the "https://www.cakedc.com/rafael_queiroz/2020/09/15/upgrading-to-cakephp-4" and original doc but having an issue while doing the dependency update for a composer. Please find the error given below: composer require --update-with-dependencies "cakephp/cakephp:4.0.*" ./composer.json has been updated Running composer update cakephp/cakephp |
# |
Jun 22nd 2021, 09:48 |
slackebot1 |
--with-dependencies Loading composer repositories with package information Updating dependencies Your requirements could not be resolved to an installable set of packages. Problem 1 - dereuromark/cakephp-tools is locked to version 2.3.0 and an update of this package was not requested. - dereuromark/cakephp-tools 2.3.0 requires cakephp/cakephp ^4.1 -> found cakephp/cakephp[4.1.0, ..., 4.2.7] but it conflicts with your root |
# |
Jun 22nd 2021, 09:48 |
slackebot1 |
composer.json require (4.0.*). Problem 2 - cakephp/debug_kit is locked to version 4.4.3 and an update of this package was not requested. - cakephp/debug_kit 4.4.3 requires cakephp/cakephp ^4.2.0 -> found cakephp/cakephp[4.2.0, ..., 4.2.7] but it conflicts with your root composer.json require (4.0.*). Problem 3 - cakephp/bake is locked to version 2.5.1 and an update of this package was not requested. - cakephp/bake |
# |
Jun 22nd 2021, 09:48 |
slackebot1 |
2.5.1 requires cakephp/cakephp ^4.1 -> found cakephp/cakephp[4.1.0, ..., 4.2.7] but it conflicts with your root composer.json require (4.0.*). Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions. Installation failed, reverting ./composer.json and ./composer.lock to their original content. |
# |
Jun 22nd 2021, 09:26 |
adrienlambertwac |
[CakePhp2.10][PhpUnit3.7] Import records in Fixture Hi everyone, Do you know the best way to import specific record into fixture ? I want to test specific records with id, and just import this ones. And when i check the *init()* method in *Cake\TestSuite\Fixture\CakeTestFixture.php* the "conditions" option is initialized with empty array. ```$this->records = array(); $query = array( 'fields' => $db->fields($model, |
# |
Jun 22nd 2021, 09:26 |
slackebot1 |
null, array_keys($this->fields)), 'table' => $db->fullTableName($model), 'alias' => $model->alias, 'conditions' => array(), 'order' => null, 'limit' => null, 'group' => null ); $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);``` So i create a fixture in a specific folder, and in this fixture i have overwritten the init() method. It's work but i feel it heavy if there are several fixtures involved. |
# |
Jun 22nd 2021, 09:26 |
slackebot1 |
```<?php /** * Article Fixture */ class ArticleFixture extends CakeTestFixture { /** * Import * * @var array */ public $import = [ 'model' => 'Article', 'records' => false, 'options' => [ 'conditions' => ['Article.id IN' => [1, 2, 3, 4]] ] ]; public $records = []; public function init() { $recordsImportOptions = null; if (array_key_exists('options', $this->import) andand empty($this->records)) { |
# |
Jun 22nd 2021, 09:26 |
slackebot1 |
$recordsImportOptions = $this->import['options']; $this->import['records'] = false; } parent::init(); if (!is_null($recordsImportOptions)) { list($plugin, $modelClass) = pluginSplit($this->import['model'], true); App::uses($modelClass, $plugin . 'Model'); if (!class_exists($modelClass)) { throw new MissingModelException(array('class' => $modelClass)); } $model = new $modelClass( null, null, |
# |
Jun 22nd 2021, 09:26 |
slackebot1 |
!isset($this->import['connection']) ? 'default' : $this->import['connection'] ); $db = $model->getDataSource(); $query = [ 'fields' => $db->fields($model, null, array_keys($this->fields)), 'table' => $db->fullTableName($model), 'alias' => $model->alias, ] + $recordsImportOptions; $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias); if ($records !== false andand |
# |
Jun 22nd 2021, 09:26 |
slackebot1 |
!empty($records)) { $this->records = Hash::extract($records, '{n}.' . $model->alias); } } } }``` May be i missed a configuration in the doc ? Or if you have a better solution than mine. Thanks :) |
# |
Jun 22nd 2021, 09:02 |
etibor |
yes that would be the easier solution thank you @steinkel |
# |
Jun 22nd 2021, 08:50 |
steinkel |
`if ($person_name) { $query->where(...) }` you can check if the $person_name is null in your code and decide which condition to apply in the where |
# |
Jun 22nd 2021, 08:46 |
etibor |
```COALESCE``` ? |
# |
Jun 22nd 2021, 08:44 |
etibor |
do you have any suggest? |
# |
Jun 22nd 2021, 08:43 |
etibor |
and search for e-mail address, return the record with exact e-mail and name is null(not the other fields) |
# |
Jun 22nd 2021, 08:42 |
etibor |
just need a solution when name is null but the email is not |
# |
Jun 22nd 2021, 08:42 |
etibor |
yes i checked this in mysql its works exactly like this |
# |
Jun 22nd 2021, 08:35 |
conehead |
So what are you expecting? |
# |
Jun 22nd 2021, 08:16 |
etibor |
but in this case the rest of the filter fields does not work and return all of the records with name NULL |
# |
Jun 22nd 2021, 08:15 |
etibor |
i tried to combine with following: `->Where(['OR'=>[['People.name IS NULL'],['People.name LIKE'=>'%'.($person_name).'%']]])` |
# |
Jun 22nd 2021, 08:14 |
etibor |
sometimes People's name is null in my db |
# |
Jun 22nd 2021, 08:14 |
etibor |
i have tiny issue with using find with LIKE'=>'%'.$name.'%' because it does not work when the value is NULL `->Where(['People.name LIKE'=>'%'.($person_name).'%'])` |
# |
Jun 22nd 2021, 08:12 |
etibor |
hello everyone |
# |
Jun 22nd 2021, 07:41 |
kevin.pfeifer |
hreflang link tags improve your SEO rating https://webmasters.stackexchange.com/questions/105998/what-is-the-difference-between-html-lang-tag-and-html-hreflang-tag the html lang value is usually only there to indicate the current language of the DOM |
# |
Jun 22nd 2021, 06:56 |
slackebot1 |
Just had problems with auto correction when switching languages and was wondering if this could have anything to do with it |
# |
Jun 22nd 2021, 06:56 |
conehead |
@angelxmoreno yeh, thanks I read that already, but it somehow seemed like it does not make too much of a difference if its set to en or any other language. I know it would be a tiny "tool", but I resolved it with the following line now: ```<html lang="<?= substr(Configure::read('App.defaultLocale', 'en_EN'), 0, 2) ?>">``` And I have to admit although my mother language is german, I have never really used the lang tag as well. |
# |
Jun 22nd 2021, 06:20 |
angelxmoreno |
tbh i never set it in the 21 years i’ve been coding even back in the html4 days. my audience and language has always been the US and english..boring i know |
# |
Jun 22nd 2021, 06:18 |
angelxmoreno |
@conehead why it matters --> https://www.w3.org/International/questions/qa-html-language-declarations#langvalues what “tools” ? would you like to see? |
# |
Jun 22nd 2021, 05:32 |
conehead |
Is there actually a need/advantage to change "<html lang="en">" to the correct language? Does ist affect the page in many ways? Was just wondering cause Cake does not seem to have any tools for this? |
# |
Jun 22nd 2021, 05:32 |
conehead |
Good morning everyone! |
# |
Jun 21st 2021, 19:22 |
kevin.pfeifer |
@vinicius.big I would create a global `var MY_I18N` variable an populate it with the translations you need |
# |
Jun 21st 2021, 19:19 |
kevin.pfeifer |
what :point_up: said :) I though you had other variables outside the anonymous function which you need but accessing variables inside the class/object (you are in a Table object space here) can be done via $this without passing the via the `use` |
# |
Jun 21st 2021, 19:19 |
vinicius.big |
hey folks! What is your suggestion to translate JS strings on Cake? There is a lot of JS libraries for this but, I would like to see your experience on it |
# |
Jun 21st 2021, 19:12 |
greg138 |
As for what do do in the function, do everything that needs to be "all or nothing" saved. |
# |
Jun 21st 2021, 19:11 |
greg138 |
You should be able to access `$this` in that function without any issue, so need real need to pass in the model either (you can use `$this->Tags`). |
# |
Jun 21st 2021, 19:09 |
alamnaryab |
yes that was the issue even without return false it worked now next step @kevin.pfeifer Should I pass $this->request->getData() to this anonymous function or make separate function for each model e.g next I have to save record in notification table saying "Logged-in-user has saved new tag at id = newly-saved-Tag->id" |
# |
Jun 21st 2021, 18:59 |
greg138 |
Any chance you're using a database that just doesn't support rollback? The MyISAM engine in MySQL, for example, doesn't. |
# |
Jun 21st 2021, 18:48 |
alamnaryab |
still redirected to index and record inserted ``` $entity = $this->Tags->patchEntity($tag, $this->request->getData()); $model = $this->Tags; $this->Tags->getConnection()->transactional(function () use ($model, $entity) { $model->save($entity, ['atomic' => false]); return false; }); return $this->redirect(['action' => 'index']);``` |
# |
Jun 21st 2021, 18:46 |
greg138 |
Try returning `false` from inside your "transactional" function. |
# |
Jun 21st 2021, 18:45 |
alamnaryab |
I tried below code, I have not written `commit` still it is saved in table ``` $entity = $this->Tags->patchEntity($tag, $this->request->getData()); $model = $this->Tags; $this->Tags->getConnection()->transactional(function () use ($model, $entity) { $model->save($entity, ['atomic' => false]); }); return $this->redirect(['action' => 'index']);``` |
# |
Jun 21st 2021, 18:39 |
greg138 |
Doesn't the save code check that it's not already in a transaction before starting a transaction? |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
https://stackoverflow.com/questions/41896991/why-is-the-automatic-transaction-option-in-cakephp-is-called-atomic/41898090 |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
```->save($entity, ['atomic' => false]);``` |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
is the term that says that it shouldn't have a transaction |
# |
Jun 21st 2021, 18:21 |
kevin.pfeifer |
```'atomic' => false``` |
# |
Jun 21st 2021, 18:18 |
alamnaryab |
I got this `use` but I am not getting how can I convert my code to achieve transactions as you also said `->save()` already has its own `commit` |
# |
Jun 21st 2021, 18:10 |
kevin.pfeifer |
so everything you expect inside your function needs to be passed via the `use` at the end of the first line, otherwise you don't have access to these variables |
# |
Jun 21st 2021, 18:09 |
kevin.pfeifer |
basically ```$result = 0; $one = function() { var_dump($result); }; $two = function() use ($result) { var_dump($result); }; $three = function() use (and$result) { var_dump($result); }; $result++; $one(); // outputs NULL: $result is not in scope $two(); // outputs int(0): $result was copied $three(); // outputs int(1)``` |
# |
Jun 21st 2021, 18:07 |
kevin.pfeifer |
they are called php anonymous functions https://www.php.net/manual/en/functions.anonymous.php |
# |
Jun 21st 2021, 18:07 |
alamnaryab |
in your second last message you said "need to pass inside via `use`" |
# |
Jun 21st 2021, 18:07 |
kevin.pfeifer |
yes, thats why you would have to wrap all those ->saveOrFail() inside the ->transactional(); |
# |
Jun 21st 2021, 18:04 |
alamnaryab |
`->saveOrFail()` will also revert that entity only not all those which saved successfully before it in same process |
# |
Jun 21st 2021, 18:01 |
kevin.pfeifer |
instead of the normal ->save() function which only returns the $entity with the errors |
# |
Jun 21st 2021, 18:00 |
kevin.pfeifer |
there is also a `->saveOrFail($entity)` function which throws an exception if something couldn't be saved. |
# |
Jun 21st 2021, 17:50 |
kevin.pfeifer |
well everything from "outside" the transactional function you need to pass inside via the `use` at the end Other then that it should be pretty much the same from your current logic |
# |
Jun 21st 2021, 17:47 |
alamnaryab |
But I think I can not use `->transactional()` also because all these entities are not for same model, also each entity properties has foreign keys from previously saved record. and there are some conditional properties. |
# |
Jun 21st 2021, 17:36 |
kevin.pfeifer |
it seems that usually every `->save($entity)` call has its own transaction, thats why you can't rollback them now |
# |
Jun 21st 2021, 17:35 |
kevin.pfeifer |
looking at https://book.cakephp.org/3/en/orm/saving-data.html#converting-multiple-records there is a `->transactional()` function you could use to wrap all those saves in one transaction ```$this->Model1->getConnection()->transactional(function () use ($articles, $entities) { foreach ($entities as $entity) { $articles->save($entity, ['atomic' => false]); } });``` But this part has been removed from the |
# |
Jun 21st 2021, 17:35 |
slackebot1 |
docs for cakephp 4 https://book.cakephp.org/4/en/orm/saving-data.html#converting-multiple-records But this `->transactional()` function is still present in cake 4 so you could try that I guess :man-shrugging: |
# |
Jun 21st 2021, 17:23 |
alamnaryab |
in cakephp2 I am having something like this ```$datasource = $this->Model1->getDataSource(); $datasource->begin(); try{ if(!$this->Model1->save(...) ){ throw exception('Could not save Model1'); } if(!$this->Model2->save(...) ){ throw exception('Could not save Model2'); } $this->Model3->id = $this->Model2->id; /////insert statement if(!$this->Model3->save(...) ){ throw exception('Could not save |
# |
Jun 21st 2021, 17:23 |
slackebot1 |
Model2'); } if(!$this->Model9->save(...) ){ throw exception('Could not save Model9'); } $datasource->commit(); }catch(Exception $ex){ $datasource->rollback(); $this->Flash->error($ex->getMessage(). ' Line:'.$ex->getLine()); }``` |
# |
Jun 21st 2021, 17:20 |
kevin.pfeifer |
and you get false back from `$this->Tags->save()` if it didn't save |
# |
Jun 21st 2021, 17:19 |
kevin.pfeifer |
well `$this->Tags->save()` will not save if there are errors in the entity and therefore you don't have to manually rollback. You have multiple events you can hook into in the `src/Table/MyTable.php` https://book.cakephp.org/3/en/orm/table-objects.html#lifecycle-callbacks But maybe I don't understand your usecase. |
# |
Jun 21st 2021, 17:16 |
alamnaryab |
https://cakesf.slack.com/archives/C053DPNGT/p1624270771154400 @kevin.pfeifer about transcation topic at above link I have cakephp2 project to convert to cakephp4 by re-writing all there I have used transactions with models not custom sql queries for more detail I have dynamic workflow on each step I have to insert in some table, while make update in some tables at last send eamil if any thing failed I have to revert and show |
# |
Jun 21st 2021, 17:16 |
slackebot1 |
error message to user. it will be difficult to convert all that logic to sql queries. |
# |
Jun 21st 2021, 16:29 |
ndm |
If by forwarding you mean you're just redirecting a user, then it should pick up the session where it left, given that the session cookie is still valid. |
# |
Jun 21st 2021, 16:28 |
ndm |
Yes, very likely that it doesn't send the session cookie. Depending on what that external thing is, it might not even have that cookie. |