I have used the form helper to create a date time selection and when I access the $this->data.
It looks like the following.
[Timetable] => Array
(
[event_id] => 133
[location_id] => 39
[start] => Array
(
[hour] => 09
[min] => 06
[day] => 11
[month] => 03
[year] => 2011
)
)
But I want this to look more like this...
[Timetable] => Array
(
[event_id] => 133
[location_id] => 39
[start] => 2011-03-11 09:06:00
)
Is there a way to transform it to this?
You can just rebuild the $this->data['Timetable']['start'] var in your controller like so:
$this->data['Timetable']['start'] = $this->data['Timetable']['start']['year']
.'-'.$this->data['Timetable']['start']['month']
.'-'.$this->data['Timetable']['start']['day']
.' '.$this->data['Timetable']['start']['hour']
.':'.$this->data['Timetable']['start']['min'];
Should work fine.
This is undocumented in the Cookbook, but the function the model uses to convert that array into a database-friendly format is Model::deconstruct($field, $data). You can use it as follows:
$startAsString = $this->Timetable->deconstruct(
'start', $this->data['Timetable']['start']
);
This solution has the benefit of not breaking the MVC abstraction by having your controller know anything about the structure of data submitted by a form or how the database stores it.
Since you're using the form helper to generate your code, you can't get it into a single index unless you change your form helper.
You're probably using
$this->Form->input('start')
Changing it to the below will make it come out the way you want.
$this->Form->input('start', array('type'=>'text'));
However, if you do this, you'll lose out on all the dropdowns that cake auto generates for you. Not a problem if you use a datepicker.
Related
Figured out a strange (to me) undefined index error and wanted to share in case anyone else was banging their heads against the wall.
Short answer: Type casting the argument INTO a function made sure the array that got returned could be used in strings with no "undefined index" error.
Here's what happened: The code took a value from the URL ($params),
passed it into a function to get a big array of data back,
then put one of the elements of the array into a string.
$wid = $params[2] ?? null; // get id from URL parameter
$wk = \Workshops\get_workshop_info($wid); // get a big array
if (isset($wk['title'])) {
$message = "The title of this workshop is: {$wk['title']}"; // no warning
}
$message = "The title of this workshop is: {$wk['title']}"; // undefined index?
print_r($wk);
I got an "undefined index" when I tried to include $wk['title'] in a string outside of an "isset" clause.
The output for print_r($wk) showed the 'title' index existing with a value. (I edited out some of this array that had people's emails)
Array
(
[id] => 425
[title] => Sketch Writing
[start] => 2021-11-07 17:00:00
[end] => 2021-11-07 19:30:00
[cost] => 225
[capacity] => 8
[notes] => A new WGIS sketch writing class for advanced sketch students and brand new students alike. If you’re an experienced writer, come hone your craft. Learn new sketch technique, new styles, new formats. If you’re brand new, come learn how to take your fun idea and turn it into a stageable, shootable, performable sketch. Great for improv students who want to transition into writing. Whatever your background- the focus will be on writing in your voice. Take the stuff that YOU find funny and turn it into a great script. This is an active writing class. You’ll leave with at least two complete sketches by the end of the five weeks!
[location_id] => 8
[sold_out_late] => -1
[when_public] => 1969-12-31 16:00:00
[teacher_id] => 8
[co_teacher_id] =>
[reminder_sent] => 0
[application] => 0
[address] =>
[city] =>
[state] =>
[zip] =>
[place] => Online
[lwhere] =>
[soldout] => 0
[showstart] => Sun Nov 7 5pm (PDT)
[showend] => 7:30pm (PDT)
[when] => Sun Nov 7 5pm-7:30pm (PDT)
[short_title] => Sketch Writing
[costdisplay] => $225 USD
[total_class_sessions] => 5
[total_show_sessions] => 0
[total_sessions] => 5
[time_summary] => 5 classes
[full_when] => Sun Nov 7 5pm-7:30pm (PDT)<br>
Sun Nov 14 5pm-7:30pm<br>
Sun Nov 21 5pm-7:30pm<br>
Sun Nov 28 5pm-7:30pm<br>
Sun Dec 5 5pm-7:30pm<br>
[upcoming] => 1
[actual_revenue] => 0
[enrolled] => 5
[waiting] => 0
[dropped] => 0
[applied] => 0
[paid] => 0
[open] => 3
)
The solution surprised me -- I had to explicitly cast the parameter I used in my function to be an int. This fixed everything:
$wid = (int) ($params[2] ?? 0);
$wk = \Workshops\get_workshop_info($wid); // get a big array
PHP thought the argument $wid was a string. But the function get_workshop_info() assumed it was getting an int. There was no error thrown because I had not done any type declaration in get_workshop_info(). But passing in what PHP thought was a string got back an array that I just couldn't use without getting undefined index errors.
I put type declarations in all my function arguments after this.
This code is from a handmade huge MVC nightmare that I've created. I didn't even bother asking because there were just so many places that could be causing problems.
I had to explicitly cast the parameter I used in my function to be an int. This fixed everything:
$wid = (int) ($params[2] ?? 0);
$wk = \Workshops\get_workshop_info($wid); // get a big array
PHP thought the argument $wid was a string. But the function get_workshop_info() assumed it was getting an int. There was no error thrown because I had not done any type declaration in get_workshop_info(). But passing in what PHP thought was a string got back an array that I just couldn't use without getting undefined index errors.
Using opencart 3 and trying to make use of the events but I'm unable to find the identifier.
E.g trying to extend the returns form added an event:
$this->model_setting_event->addEvent('mail_account_return_after', 'catalog/model/account/return/addReturn/after', 'extension/module/return/returnAdd');
Controller:
class ControllerExtensionModuleReturn extends Controller {
public function returnAdd(&$route, &$args, &$output) {
print_r($args);
exit;
$args is missing the main return_id identifier:
Array
(
[0] => Array
(
[firstname] => Foo
[lastname] => Bar
[email] => test#gmail.com
[telephone] => 01234556789
[order_id] => 29
[date_ordered] => 2017-06-29
[product] => Canon EOS 5D
[model] => Product 3
[quantity] => 1
[return_reason_id] => 4
[opened] => 0
[comment] => Test comment
)
)
Tried with $this->db->getLastId() but this returns 0. Tested with other events and appears to be missing the main identifiers.
Where does opencart set what data is passed to the before/after events?
If I'm not mistaken you should be able to access the auto increment return_id in $output, which holds the output of method addReturn(), i.e., $this->db->getLastId();.
As to why calling getLastId() a second time from within your event doesn't work, that's a good question. I would assume there may be some other query happening in between - though by default I don't think there should be. Is it possible there is another trigger running a query before your event gets triggered?
Look in system/engine/loader.php at for the strings before and after. There are methods that do view, controller, configuration and language.
I baked something on cake bake and entering values on local host in that application. The name of the value that i want to retrieve for php script is temperature. how can i get that value?
Here is the some information laying behind the application:
[useDbConfig] => default
[useTable] => temperature_readings
[id] => 32
[data] => Array
(
[TemperatureReading] => Array
(
[temperature] => 15
[location_id] => 5
[created] => 1437572170
[id] => 32
that temperature value, which is 15, is the variable that i want to use in php script.
You can access temperature like this:
$temperature = $array['data']['TemperatureReading']['temperature'];
echo $temperature; //Returns 15
I have a form that includes an hour and minute drop down using this: 'dateFormat'=>'NONE', 'timeFormat'=>'24' But now when i save the form $this->data ends up looking like the array below and because eventStartDate & eventStartDate are arrays it makes an SQL error saying eventStartDate can not be null etc.
Array
(
[Work] => Array
(
[eventStartDate] => Array
(
[hour] => 12
[min] => 00
)
[eventEndDate] => Array
(
[hour] => 12
[min] => 45
)
[description] => 234
[projectID] => 7105
[taskID] => 1
[userid] => 8
)
)
Shouldnt cakephp make these arrays into strings so they can be put into the database automatically?
What values for day, month etc will be used?
This might help, as it worked for me:
Your view code should look something like this
echo $form->dateTime('your_column_name', 'NONE', '24');
I'm building a filter for some paginated lists, and i want to be able to show the elements created between two dates. But I'm not sure how to do it correctly.
The view:
<?php echo $this->Form->create('Logs');?>
<fieldset>
<?php
echo $this->Form->input('start',array('type'=>'date'));
echo $this->Form->input('end',array('type'=>'date'));
?>
</fieldset>
<?php echo $this->Form->end('Filter');?>
The controller:
...
$conditions['Logs.created BETWEEN ? AND ?'] = array( $this->data['Logs']['start'],$this->data['Logs']['end']);
...
the problem is that $this->data['Logs']['start'] and $this->data['Logs']['end'] are arrays and i need strings:
[Logs] => Array
(
[start] => Array
(
[month] => 04
[day] => 19
[year] => 2011
)
[end] => Array
(
[month] => 04
[day] => 19
[year] => 2011
)
)
I know that i could use some php functions to transform the array into string, but there must be some function or something in cake. I feel that i'm not constructing the view correctly
Thanks for your help.
I think the best bet for you would be to write your own helper function and call it in your controller.
This thread talks about how to create your helper : How best to convert CakePHP date picker form data to a PHP DateTime object?
You can load it into the controller like so
App::import('Helper', 'DateHelper');
$dateHelper = new DateHelper();
Also, why not just switch to a regular text input and maybe a JS datepicker? This would solve your problems without having to resort to the helper route. You'd lose the dropdown availability though.
you can use the time helper, and it's daysAsSql function
daysAsSql( $begin, $end, $fieldName, $userOffset = NULL )
daysAsSql returns a string in the format "($field_name >= '2008-01-21 00:00:00') AND ($field_name <= '2008-01-25 23:59:59')". This is handy if you need to search for records between two dates inclusively.
See Time helper