# |
Dec 1st 2017, 23:34 |
obinoob |
phantomwatson I've found that public function beforeSave method is not triggered from controller but from table... the problem is that beforeSave($event, $entity, $options) { ... } in table has access at one entity at a time, not to all of the entities.... also how can I mark only one phone number as obligatory without refactoring the all database |
# |
Dec 1st 2017, 23:24 |
phantomwatson |
*Answering my own question:* Turns out I should have been using `$this->request->getEnv('FOO')` to read the variable instead of `env('FOO')`. |
# |
Dec 1st 2017, 23:07 |
d-fens |
why doesn't the baked code order by id , iirc that was default in older versions? |
# |
Dec 1st 2017, 23:04 |
phantomwatson |
I'm avoiding `configRequest()` to stay DRY and avoid having to duplicate the other environment variables that are set in this test class's `setUp()` method. |
# |
Dec 1st 2017, 23:00 |
phantomwatson |
In other words, this is my situation: ``` // In test public function testFoo() { $this->_request['environment']['FOO'] = 'bar'; $this->get('/login'); } // In controller public function login() { debug(env('FOO')); // expected: 'bar'; actual: null } ``` |
# |
Dec 1st 2017, 22:27 |
phantomwatson |
In an integration test, I'm using `$this->configRequest(['environment' => ['foo' => 'bar']])` to set an environment variable before a request, but in the controller method that gets executed, the `foo` env var is always the default value, and `configRequest()` doesn't seem to be doing what I intend it to. Does anyone have any advice about setting env vars in an integration test? |
# |
Dec 1st 2017, 22:26 |
angelxmoreno |
something tells me @dereuromark already has the solution hidden somewhere :P |
# |
Dec 1st 2017, 22:26 |
angelxmoreno |
Greetings bakers! I am working with a CakeForm class and I am defining its schema. I feel weird having to change the view when I change the form object in order to add new fields. It feels "uncake-like". Is there an undocumented helper that takes the form object and creates the view using the already defined schema in the Form object? |
# |
Dec 1st 2017, 21:51 |
obinoob |
phantomwatson actually I can only see beforeSave in table and data it will be accessed with $event->getData('entity) https://book.cakephp.org/3.0/en/controllers/components/authentication.html |
# |
Dec 1st 2017, 21:47 |
obinoob |
and my english doesn't help much either |
# |
Dec 1st 2017, 21:46 |
obinoob |
I thought that cake had some key value option to do this kind of work hidden in the book somewhere... |
# |
Dec 1st 2017, 21:45 |
phantomwatson |
It definitely takes some work. Remember to check https://stackoverflow.com/ for answers too. |
# |
Dec 1st 2017, 21:44 |
obinoob |
*I'm |
# |
Dec 1st 2017, 21:44 |
obinoob |
I starting now with events so it is a total new thing for me today i've used for the first time beforeMarshal() I'm alone here man only count with channel help and cake website it's hard some times I take more than a day to achieve some basic stuff! |
# |
Dec 1st 2017, 21:42 |
phantomwatson |
It's okay. This stuff is complicated. |
# |
Dec 1st 2017, 21:41 |
obinoob |
yes I got that I wasn't paying attention I apologise |
# |
Dec 1st 2017, 21:41 |
phantomwatson |
So the code that I posted would normally be wrapped in `if ($this->request->is('post') {}` |
# |
Dec 1st 2017, 21:41 |
obinoob |
sorry I was read https://book.cakephp.org/3.0/en/orm/table-objects.html#beforesave |
# |
Dec 1st 2017, 21:40 |
phantomwatson |
The code that I posted was in the context of a controller method. `$this->request` isn't available in a table. |
# |
Dec 1st 2017, 21:39 |
obinoob |
beforeSave() in table right? |
# |
Dec 1st 2017, 21:37 |
obinoob |
and make them ->empty('number') |
# |
Dec 1st 2017, 21:37 |
phantomwatson |
Like ``` $data = $this->request->getData(); foreach ($data as $datum) { ... (if a number is empty(), remove that 'Phone' member from $data) ... } ``` |
# |
Dec 1st 2017, 21:36 |
phantomwatson |
Though there's likely a more elegant solution involving putting that logic in a `beforeSave()` callback. |
# |
Dec 1st 2017, 21:36 |
phantomwatson |
If it were me, I'd loop through the request data and remove empty phone numbers before saving. |
# |
Dec 1st 2017, 21:34 |
obinoob |
the thing is how can it be done? |
# |
Dec 1st 2017, 21:34 |
obinoob |
exactly |
# |
Dec 1st 2017, 21:34 |
phantomwatson |
It sounds like you need to actually `allowEmpty('number')` and then add some logic in your controller or `PhonesTable` that ignores any `Phone`s with empty numbers instead of saving them to the database. |
# |
Dec 1st 2017, 21:32 |
phantomwatson |
Is there any error that is still occurring that you would like help solving? |
# |
Dec 1st 2017, 21:32 |
obinoob |
but then I need to provide two phone number as they are not allowed to be empty... |
# |
Dec 1st 2017, 21:32 |
obinoob |
no, with not Empty() it will fail in validation as expected |
# |
Dec 1st 2017, 21:31 |
phantomwatson |
Even after you added the `notEmpty()` validation rule? |
# |
Dec 1st 2017, 21:30 |
obinoob |
at phones table |
# |
Dec 1st 2017, 21:30 |
obinoob |
no they are not however the entity is filling up the id and client_id automatically and filling the phone number with null |
# |
Dec 1st 2017, 21:29 |
phantomwatson |
So they're not obligated to enter two phone numbers? |
# |
Dec 1st 2017, 21:29 |
obinoob |
only saving whats available |
# |
Dec 1st 2017, 21:29 |
obinoob |
quite the opposite I only want to save registry in database if available I can't predict if client has or wants to give phone number that are the business rules get that so I can't force them to be required |
# |
Dec 1st 2017, 21:27 |
phantomwatson |
It sounds like you need some client-side validation to require values for both phone numbers. if you also include `$validator->requirePresence('number')`, I think the FormHelper will automatically mark the field as required, and modern browsers will automatically prevent the form from being submitted until a value is entered. |
# |
Dec 1st 2017, 21:24 |
obinoob |
but the deal is it will fail and I'm obligated to enter two phone numbers |
# |
Dec 1st 2017, 21:24 |
obinoob |
sorry not it doesn't |
# |
Dec 1st 2017, 21:23 |
obinoob |
yes it does |
# |
Dec 1st 2017, 21:23 |
phantomwatson |
Even after you add the `notEmpty()` validation rule it still saves empty values for `number`? |