# |
Jun 2nd 2021, 15:09 |
ndm |
@y.teruyacookie That's not how the `find()` method works, it only take two arguments, the first being the finder name, and the second being an array of options. Similarly finder methods also only take two arguments, the first being the query object, the second being the options array. |
# |
Jun 2nd 2021, 14:58 |
slackebot |
->where(function (QueryExpression $exp) use { return $exp->between('Models.created', '2021-04-01 00:00:00', '2021-04-10 00:00:00'); }) ->order([ 'Models.created' => 'ASC' ]) ->find('Contain'); }``` |
# |
Jun 2nd 2021, 14:58 |
slackebot |
select the fields (columns) we want from the table with the select method ->where(function (QueryExpression $exp) use ($begin, $end) { return $exp->between('Models.created', $begin, $end); }) ->order([ 'Models.created' => 'ASC' ]) ->find('Contain'); // Use contain method to join other tables as needed }``` `Code that does not cause memory overload` src/Controller/Api/Controller.php ```public function |
# |
Jun 2nd 2021, 14:58 |
slackebot |
index() { // output JSON file return $this->setDatas($begin, $end); } public function setDatas($begin, $end) { $datas = $this->Model->find('Datas'); $spreadSheets = $this->Component->toSpreadSheetDatas($datas); return $this->response->withType('application/json') ->withStringBody(json_encode($spreadSheets)); } src/Model/Table/ModelTable.php public function findDatas(Query $query) { return $query ->find('Fields') |
# |
Jun 2nd 2021, 14:58 |
y.teruyacookie |
Hi, I need your help. ## Environment Mac OS, Chrome, CakePHP version 3 ## What I’m working on Implementing a function that receives the body of a POST request and returns a resource accordingly. ## What I want to do and what I want to solve I would like to write the following code to implement the function mentioned above, but when I run it, I get a memory overload error saying “Fatal error: Allowed memory size of |
# |
Jun 2nd 2021, 14:58 |
slackebot |
... bytes exhausted”. (The error occurs even if I make the difference between $begin and $end in the code below very small. On the other hand, if I set the two arguments $begin and $end of findDatas in the Model to constants, the code will work without memory overload. On the other hand, if I set the two arguments $begin and $end of findDatas in the Model to constants, the code passes without memory overload. It seems that the memory |
# |
Jun 2nd 2021, 14:58 |
slackebot |
usage goes up very high as soon as I add the parameter. Also, is there any way to avoid this memory error? (By the way, I tried to increase the memory_limit in php.ini to 1GB, but it still caused a memory error, even if I increased it to the spec value of my PC. In the following code, I tried to debug() at *1, *2, and *3, and I got memory overload errors except for *1, so I think the memory error occurs right after the findDatas is |
# |
Jun 2nd 2021, 14:58 |
slackebot |
loaded. If anyone knows, please let me know. Thanks in advance. `Code that goes over memory.` src/Controller/Api/Controller.php ```public function setDatas($begin, $end) { // Store the body data of the received POST request $begin = $this->request->getData('begin'); $end = $this->request->getData('end'); *1 // Get data from Model (same as ↓findDatas($this->Model, $begin, $end)) $datas = $this->Model->find('Datas', |
# |
Jun 2nd 2021, 14:58 |
slackebot |
$begin, $end); *2 // Format the data for output to a spreadsheet $spreadSheets = $this->Component->toSpreadSheetDatas($datas); // Store the data in the body of the response return $this->response->withType('application/json') ->withStringBody(json_encode($spreadSheets)); } // src/Model/Table/ModelTable.php public function findDatas(Query $query, string $begin, string $end) { *3 return $query ->find('Fields') // |
# |
Jun 2nd 2021, 13:27 |
greg138 |
It could just as easily be argued that adding tags to a record *is* an update, and so it's doing exactly the right thing. So, I expect that removing the behavior when you don't want that to happen (whether because of this sort of thing or something else) is the right answer. |
# |
Jun 2nd 2021, 09:42 |
k4t |
thank you |
# |
Jun 2nd 2021, 09:42 |
k4t |
understand |
# |
Jun 2nd 2021, 09:40 |
philo.hamel |
and the theme is part of the view |
# |
Jun 2nd 2021, 09:39 |
philo.hamel |
that's the purpose of the helper: it's view context aware |
# |
Jun 2nd 2021, 09:37 |
k4t |
no... Assets class, with helpr this is ok... |
# |
Jun 2nd 2021, 09:36 |
philo.hamel |
using the helper? |
# |
Jun 2nd 2021, 09:36 |
k4t |
the same problem when it is also used directly in the view |
# |
Jun 2nd 2021, 09:34 |
philo.hamel |
unless there's a good reason to keep the view code in the entity, I'd try to move it into the view |
# |
Jun 2nd 2021, 09:30 |
k4t |
and in the view.php it is called in that way: <?= $article->imageUrlForSharing; ?> |
# |
Jun 2nd 2021, 09:30 |
k4t |
``` protected function _getImageUrl() { return Asset::imageUrl('sharing-fallback.png', ['fullBase' => true, 'plugin' => true]); }``` |
# |
Jun 2nd 2021, 09:29 |
k4t |
to be honest inside of entity |
# |
Jun 2nd 2021, 09:27 |
philo.hamel |
inside a template? https://api.cakephp.org/4.0/class-Cake.View.Helper.UrlHelper.html#image() |
# |
Jun 2nd 2021, 09:23 |
k4t |
I even tried tto add plugin = true to the options but it did not solve the problem |
# |
Jun 2nd 2021, 09:21 |
k4t |
In latest CakePHP I am using: https://api.cakephp.org/4.0/class-Cake.Routing.Asset.html#imageUrl() but it look like that function does not take active theme into account so all urls generated by it does not contain active theme prefix. Is this bug or I missed something? |
# |
Jun 2nd 2021, 07:19 |
kgb.acct.personal |
Good day. Is it possible for the bake migration snapshot command to include the index names? |
# |
Jun 2nd 2021, 06:16 |
thomas465 |
yeah, that’s what i’m thinking too, as a stopgap at least, but i’m just wondering if this is something that should be fixed either in the ORM or in `TimeStampBehavior` ? |
# |
Jun 2nd 2021, 02:35 |
khalil |
Solved it :) |
# |
Jun 2nd 2021, 02:17 |
khalil |
Not sure if it's a bug or something I'm doing wrong In my DB schema, I have "Users" and "Shops", the users table has a shop_id field, and the shops table has a user_id (doing this to make querying easier in some complicated cases I have) The issue is happening when trying to add a new user, when a new user is getting added, I want to also create a record in the companies table. The relationships are belongsTo, companies |
# |
Jun 2nd 2021, 02:17 |
slackebot |
belongsTo users and users belongsTo company. Everything is set properly and following the documentation and the conventions, but when I try to save, only the user is getting saved, the shop is not even appearing in the request, unless I change the form field name from for example shop.name to shops.name - even though in the documentation it's clearly stated that in the case of belongsTo you should use singular not plural. If I change |
# |
Jun 2nd 2021, 02:17 |
slackebot |
the relationship to hasMany, and I change the form field to array (shops.0.name) the company saves properly, is this a bug or am I missing something? |
# |
Jun 2nd 2021, 02:12 |
khalil |
Hey guys |
# |
Jun 1st 2021, 21:35 |
corey.taylor.fl |
test |
# |
Jun 1st 2021, 21:30 |
greg138 |
Would your encoded JSON not have something more like `"first_name":"John"` in it anyway? |
# |
Jun 1st 2021, 21:19 |
tyler.adam.lazenby |
Thank you for that solution |
# |
Jun 1st 2021, 21:17 |
tyler.adam.lazenby |
That is really very helpful thank you |
# |
Jun 1st 2021, 21:17 |
tyler.adam.lazenby |
very interesting |
# |
Jun 1st 2021, 21:16 |
thomas465 |
```$responseData = json_decode((string)$this->_response->getBody(), true)``` |
# |
Jun 1st 2021, 21:15 |
kevin.pfeifer |
and assert that with some json you expect |
# |
Jun 1st 2021, 21:15 |
kevin.pfeifer |
after the `$this->post` |
# |
Jun 1st 2021, 21:15 |
kevin.pfeifer |
try ```$response = $this->_response->getBody()->__toString();``` |
# |
Jun 1st 2021, 21:14 |
tyler.adam.lazenby |
but the third assertion fails even though the fixture is set to have the first name as John |
# |
Jun 1st 2021, 21:13 |
tyler.adam.lazenby |
this is my test right now |
# |
Jun 1st 2021, 21:13 |
tyler.adam.lazenby |
```public function testAddToExisting(): void { $data = ['account_id' => 1]; $this->enableCsrfToken(); $this->post('/accounts/add-to-existing/1', $data); $this->assertResponseOk(); $this->assertResponseNotEmpty(); $this->assertResponseContains('first_name: John'); }``` |
# |
Jun 1st 2021, 21:13 |
tyler.adam.lazenby |
Let me make my question more specific. If I expect to json_encode a user entity with a first_name property valued as "john", how would I unit test that? |
# |
Jun 1st 2021, 21:09 |
greg138 |
I've done that. This is what `removeBehavior` is for. :) |
# |
Jun 1st 2021, 21:08 |
kevin.pfeifer |
sorry, haven’t dealt with that till now. Can’t answer that right now ,:) |
# |
Jun 1st 2021, 21:06 |
thomas465 |
tags is linked to products with a `through` class, that class is also defined. again, everything works, but i’d like it not update the modified field :) |
# |
Jun 1st 2021, 21:04 |
thomas465 |
but tag_id = 10 will also have it’s modified date updated (TimeStampBehavoir) |
# |
Jun 1st 2021, 21:03 |
thomas465 |
and its creating a tag_id = 10, product_id = 99 in the link table along with the comment (and other cols for the join table) |
# |
Jun 1st 2021, 21:03 |
tyler.adam.lazenby |
On unit tests, I want to test to see if the response contains a new account id, since I return it in application/json form. How can I do that? |
# |
Jun 1st 2021, 21:02 |
thomas465 |
well, here the tag id is 10 |
# |
Jun 1st 2021, 21:02 |
kevin.pfeifer |
ah ok your junction table has an id |
# |
Jun 1st 2021, 21:01 |
thomas465 |
https://book.cakephp.org/4/en/orm/saving-data.html#saving-additional-data-to-the-join-table |
# |
Jun 1st 2021, 21:01 |
thomas465 |
it looks like the example in the docs ``` 'tags' => [ [ 'id' => 10, '_joinData' => [ 'comment' => '123123' ] ], // Other courses.``` |
# |
Jun 1st 2021, 20:57 |
kevin.pfeifer |
and it does look like that? ```$entity['tags'] = [ '_ids' => [ '23' ] ];``` where 23 is one valid tag id |
# |
Jun 1st 2021, 20:52 |
thomas465 |
(the import script is just doing pathEntity on handcrafted arrays so i would not think it makes a difference) |
# |
Jun 1st 2021, 20:51 |
thomas465 |
let me give it a try |
# |
Jun 1st 2021, 20:47 |
kevin.pfeifer |
does it also happen when you add that entry “normally” via your template or does it just happen with your import script |
# |
Jun 1st 2021, 20:45 |
thomas465 |
My issue is that when running a import/sync it keeps touching the same 5 tags resulting in deadlock errors after a while |
# |
Jun 1st 2021, 20:45 |
thomas465 |
(ok that was terrible english) I’m creating a new link in the link table, (with some extra joindata) all that works fine, but it updates the modified date of the `tag` i’m linking to, is that expected behaviour ? |
# |
Jun 1st 2021, 20:43 |
thomas465 |
Question when saving belongsToMany() association with _joinData … is it expected that the modified date of the target table? So if `products -> products_tags -> tags` it updates the modifed col of `tags` |
# |
Jun 1st 2021, 20:43 |
kevin.pfeifer |
well you get the parsed json data via ```$data = $this->getRequest()->getData()``` if you want the raw body you could try look at your $_POST Variable (if you sent your data via POST) |
# |
Jun 1st 2021, 20:06 |
rightscoreanalysis |
is $this->request->input() the best way to read raw json body |
# |
Jun 1st 2021, 19:10 |
tyler.adam.lazenby |
right, that is what I just did now, but the issue is that I have to add that to the table class itself as a method and I can reuse that |
# |
Jun 1st 2021, 19:05 |
richard |
makes more sense to add something like that to $validator |
# |
Jun 1st 2021, 19:04 |
richard |
@tyler.adam.lazenby you wouldn’t see the error until you attempt a save on buildRules |
# |
Jun 1st 2021, 18:34 |
kevin.pfeifer |
is it the developer mentioned in the comments at the start of the style.css in the theme? |
# |
Jun 1st 2021, 18:33 |
jonathan086 |
Understood Kevin! This is frustrating when I am directed to the wrong avenue. |
# |
Jun 1st 2021, 18:28 |
kevin.pfeifer |
Just FYI: CakePHP is a complete MVC Framework in which you build whole websites with it, not "just" some little library you use to build themes with |
# |
Jun 1st 2021, 18:25 |
jonathan086 |
I am reaching back out to the developer Kevin. Thanks for your time and appreciate it. |
# |
Jun 1st 2021, 18:17 |
kevin.pfeifer |
+ i find it very suspect, that a theme developer tells you (a customer) to update a "library" the theme developer used to build the theme (which I still doubt) This should in my opinion always be the job of the theme developer, not the customer |
# |
Jun 1st 2021, 18:11 |
kevin.pfeifer |
do you have a composer.json inside your theme |
# |
Jun 1st 2021, 18:10 |
jonathan086 |
That is the reason why I contacted you. |
# |
Jun 1st 2021, 18:10 |
jonathan086 |
Thanks Kevin! The developer told me to update the cake PHP. |
# |
Jun 1st 2021, 17:47 |
kevin.pfeifer |
@jonathan086 your errors are related to your Wordpress Theme "Nova" not being compatible with your new PHP Version. So upgrade your Theme (if possible without changes, not always the case) But generally this is a CakePHP support channel, not a Wordpress Support channel and your error currently has nothing to do with cakephp |
# |
Jun 1st 2021, 17:41 |
jonathan086 |
After updating the value of String to CakeText, I am getting this error: *Parse error*: syntax error, unexpected ‘new’ (T_NEW) in */var/www/vhosts/scainc.net/httpdocs/wp-content/themes/nova/app/libs/inflector.php* on line *300* |
# |
Jun 1st 2021, 17:39 |
jonathan086 |
*Fatal error*: Cannot use ‘String’ as class name as it is reserved in */var/www/vhosts/scainc.net/httpdocs/wp-content/themes/nova/app/libs/string.php* on line *28* |
# |
Jun 1st 2021, 17:39 |
jonathan086 |
https://www.scainc.net/ |
# |
Jun 1st 2021, 17:39 |
jonathan086 |
I am in need of help. I used a wordpress theme and its framework was built on CakePHP. Upon upgrading wordpress to the latest version, I had to move the PHP version to 7.*. After updating, I am getting the following error: |
# |
Jun 1st 2021, 17:24 |
tyler.adam.lazenby |
I just tried with __invoke as returning false, and it is still doing that |
# |
Jun 1st 2021, 17:18 |
tyler.adam.lazenby |
```$rules->add(new NotHasOneAsFirstChar('phone', 'You cannot use 1 as the first character of a phone number.'), 'notHasOneAsFirstCharacter');``` |
# |
Jun 1st 2021, 17:18 |
tyler.adam.lazenby |
```<?php namespace App\Model\Rule; use Cake\Datasource\EntityInterface; class NotHasOneAsFirstChar { /** * Name of fields or fields that this rule should be applied to. * @var string */ protected string $_fields; /** * Default message that should be set. Default message is "That value is invalid.". * @var string|null */ protected ?string $message; public |
# |
Jun 1st 2021, 17:18 |
slackebot |
function __construct(string $field, ?string $message = null) { $this->_fields = $field; if ($message) { $this->message = $message; } else { $this->message = __("That value is invalid."); } } public function __invoke(EntityInterface $entity, array $options): bool { $value = $entity->get($this->_fields); $value = $this->cleanValue($value); |
# |
Jun 1st 2021, 17:18 |
slackebot |
return $value[0] != "1"; } protected function cleanValue(string $value): array|string|null { $pattern = "/[^0-9]+/"; $replacement = ""; return preg_replace($pattern, $replacement, $value); } }``` |
# |
Jun 1st 2021, 17:17 |
tyler.adam.lazenby |
I am still struggling with this. All I am trying to do is prevent the user from putting 1 as the first character for their phone number. It is being passed with an input mask so that it is easier for the user to put it in. I can clean off the mask pretty easily. Once I have done that, my test is trying to pass "(123) 567-8910" so that it will fail the validation test. Bu the entity doesn't have any errors. |
# |
Jun 1st 2021, 17:02 |
tyler.adam.lazenby |
ok, thanks |
# |
Jun 1st 2021, 17:02 |
ndm |
Not at all, that's the save operation flow options. Simply store the constructor argument value in a property and use that in the invoke method. |
# |
Jun 1st 2021, 17:02 |
tyler.adam.lazenby |
oh I see now |
# |
Jun 1st 2021, 16:52 |
tyler.adam.lazenby |
Question... how would the $options parameter be used with that constructor? |
# |
Jun 1st 2021, 16:40 |
tyler.adam.lazenby |
ok that should work |
# |
Jun 1st 2021, 16:38 |
ndm |
Add a constructor that takes the name of the field that the rule should check. |
# |
Jun 1st 2021, 16:37 |
tyler.adam.lazenby |
https://book.cakephp.org/4/en/orm/validation.html#creating-custom-rule-objects |
# |
Jun 1st 2021, 16:34 |
tyler.adam.lazenby |
```<?php namespace App\Model\Rule; use Cake\Datasource\EntityInterface; class NotHasOneAsFirstChar { public function __invoke(EntityInterface $entity, array $options) { $value = $entity->get('phone'); // This should be more dynamic return $value[0] != "1"; } }``` |
# |
Jun 1st 2021, 16:33 |
tyler.adam.lazenby |
I am trying to make a new reuseable validation rule that will be able to check if the first character is "1". This will primarily be used on phone fields, but not all phone fields will be labeled as "phone". I am trying to figure out how to create this validation rule object to do that. The logic is pretty straight forward once I can get the deiried value. The documentation doesn't make it clear how to get the |
# |
Jun 1st 2021, 16:33 |
slackebot |
"desired" value name from the entity interface though |
# |
Jun 1st 2021, 16:31 |
tyler.adam.lazenby |
Ok new question |
# |
Jun 1st 2021, 16:31 |
tyler.adam.lazenby |
interesting. |
# |
Jun 1st 2021, 16:30 |
admad |
So either check for PUT in your action or check for PUT or POST |
# |
Jun 1st 2021, 16:28 |
admad |
It always does |
# |
Jun 1st 2021, 16:28 |
tyler.adam.lazenby |
I see the form helper added that hidden field. When does the helper do that? Because this is the first time I have seen that |