I am saving form data in a Session, and trying to save to a model called Property using the session array. Please see the array below. I think it has something to do with the Session array but I am not sure.
When I try to save like so, it does not save:
$this->Property->save($propertyData) where $propertyData is the property array.
sql_dump:
INSERT INTO `fra`.`properties` (`type`, `address`, `city`, `state`, `zip`, `price`, `bed_rooms`, `bath_rooms`, `lot_size_sq_ft`)
VALUES ('0', '2720 Acapulco way', 'modesto', 'ca', '95355', 310000, 4, 3, 6040)
The session array is:
Array
(
[house_details] => Array
(
[form] => Array
(
[section] => house_details
)
[Property] => Array
(
[type] => 0
[address] => 2720 Acapulco way
[city] => modesto
[state] => ca
[zip] => 95355
[price] => 310000
[prop_year_build] => 2007
[prop_year_remodel] =>
[bed_rooms] => 4
[bath_rooms] => 3
[garage_spaces] => 3
[lot_size_sq_ft] => 6040
[house_size_sq_ft] => 3720
[stories] => 2
[condition_rating] => 8
)
You should be able to do this quite simply. You probably have a problem with $propertyData being set as the wrong thing so it doesn't provide a valid value for the model save.
What do you get if you do debug($propertyData)?
Does this work?
$propertyData = $this->Session->read('house_details.Property');
$this->Property->save(propertyData);
Related
I am a bloody CakePHP beginner starting with the latest CakePHP version 4.1.0.
I get the attribute for identity successfully with $identity = $this->request->getAttribute('identity');
Below the print_() dump for the identity object. And from here on I am stuck as I have no clue how to get the values id, name, email, ... inside the data object.
My intention is to get the current user properties. What did I understand wrong, how is the correct way to get the user data?
Authentication\Identity Object
(
[config] => Array
( fieldMap] => Array ( [id] => id )
)
[data] => App\Model\Entity\User Object
(
[id] => 1
[name] => Some Name
[email] => name#example.com
[password] => $2y$10$s******
[role] => admin
[created] => Cake\I18n\FrozenTime Object
(
[date] => 2020-07-27 15:23:59.000000
[timezone_type] => 3
[timezone] => UTC
Oh oh, it's much simpler than thought.
$identity->id|name|email is the solution.
Is it possible to update existing data and save new data at the same time without having to loop through the array? I would like the array with the id to update and the array without an id to create a new entry. See the example below. Thank you for your help.
[Workload] => Array
(
[0] => Array
(
[phase_count] => 1
[id] => 17
[value] => {"phases":[{"rep_range":"20-30","rep_set_count":"1"}]}
[user_id] => 1
)
[4] => Array
(
[phase_count] => 1
[value] => {"phases":[{"rep_range":"20-30","rep_set_count":"1"}]}
[user_id] => 1
)
);
and then this
$this->Workload->saveAll($this->data['Workload']);
EDIT ======================================
Here is the code that actually saves this array
if($this->data){
array_walk($this->data['Workload'], function (&$value,$index){
// This will need to be changed once users are setup
if(empty($value['user_id'])){
$value['user_id'] = 1;
}
$value['value'] = json_encode($value['value']);
});
debug($this->data);
$this->Workload->saveAll($this->data['Workload']);
In fact, I do exactly that in one of my apps. If there is no 'id' a table entry will be created. If the 'id' is specified, the record will be updated.
I have a page for editing records of the Venue model in my app. This page was working at some stage, but is now broken.
in the controller action, debugging $this->data gives the expected array of form values. However, in the Venue model, debugging $this->data in beforeSave gives only the values for fields from a related (HABTM) model, Category:
app/models/venue.php (line 89)
Array
(
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 8
)
)
)
What could be happening to this data between the form being submitted to the controller action, and the call to beforeSave? Where should I be looking to debug this?
THanks
Edit - here's what's in $this->data in the controller (actual data changed to remove phone numbers, addresses etc).
app/controllers/venues_controller.php (line 63)
Array
(
[Venue] => Array
(
[id] => 19
[city_id] => 1
[user_id] => 130
[name] => Acme Zoo
[email] => events#acmezoo.org.uk
[description] =>
Some text...
[blurb] => Truncated description...
[contact_id] =>
[address_1] => Acme Zoo
[address_2] => Some Road
[postcode] => PP9 4DD
[telephone] => 010101010101
[website] =>
[latitude] => 55.21222
[longtitude] => -2.111111
[featured] => 0
[active] => 1
[flagged] => 0
[smg] => 0
[smg_custom_icon] => 1
[listings] => 1
[send_email] => 0
[file] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
[Category] => Array
(
[Category] => Array
(
[0] => 3
[1] => 6
[2] => 10
)
)
)
And here's my code to save the data...
if (!empty($this->data)) {
if ($this->Venue->save($this->data)) {
$this->Session->setFlash('The venue has been saved','success');
$countryId = $this->Venue->City->field('country_id',array('id'=>$this->data['Venue']['city_id']));
if (!empty($this->data['Venue']['send_email'])){
$this->_emailVenue($this->Venue->id,'venue_added',$countryId);
}
$this->redirect(array('action' => 'index','city'=>$this->data['Venue']['city_id']));
} else {
$this->Session->setFlash('The venue could not be saved. Please, try again.','failure');
}
}
I think i found a solution to this but I am really unsure if this should be considered a "good" solution.
I backup the request data before the save and then restore it if it fails.
$temp = $this->request->data;
if ($this->Post->save($this->request->data)) {
}else{
$this->request->data = $temp;
}
Maybe a stupid question, but do you pass the content of controller $data to the model when you call the save() method ?
$this->Venue->save($this->data)
Are you trying to save an entry to the categories table at the same time? If so, you can use $this->Venue->saveAll($this->data) instead of save(). If you just want to save the Venue data, just pass that in to save() instead of the entire $this->data like this: $this->Venue->save($this->data['Venue']);
I'm saving multiple Widgets and associated WidgetsItems to a menu, which I have working fine.
The problem is, my edit function is not working. I understand that the data array structure needs to be as following, as it is what is saving my data properly in the first place:
Array (
[Widget] => Array
(
[23] => Array
(
[title] => Cocktails
[id] => 23
[WidgetsItem] => Array
(
[147] => Array
(
[item] => Martini: Noilly Pratt, Ginor Vodka
[price] => 24
[id] => 147
)
[148] => Array
(
[item] => Negroni: Campari, Gin, Sweet Vermouth
[price] => 16
[id] => 148
)
)
)
using the following controller code:
foreach($this->data['Widget'] as $widgetKey => $widget) :
$widgetData = array(
'title' => $widget['title'],
'id' => $widget['id']
);
$saveableWidget = Set::insert($widget, 'Widget', $widgetData);
if($this->Widget->saveAll($saveableWidget)) : $saveSuccess = true; endif;
endforeach;
Which is copied and pasted from the initial save function, then modified a little for editing. Instead, it's creating new entries, not editing them. I know it's something to do with the IDs, but it's just not saving. What am I doing wrong?
Please help, I feel like I'm very close to a nearly finished product.
Thanks,
~harley
I have a problem with automagic and related model data. I have 4 models: Exercice, Ecriture, Ligne, Compte. Exercice hasmany Ecriture and Ecriture hasmany Ligne and Compte hasmany Ligne in two relation given by to different foreign keys. I want to use automagic to populate my form. So using $this->data, I give this array to the view:
Array
(
[Exercice] => Array
(
[id] => 1
[theme] => marchandises
)
[Ecriture] => Array
(
[0] => Array
(
[id] => 1
[exercice_id] => 1
[numero] => 1
[enonce] => Quelle est la dincee?
[Ligne] => Array
(
[0] => Array
(
[id] => 1
[ecriture_id] => 1
[compte_debit_id] => 2
[compte_credit_id] => 1
[montant_debit] => 23
[montant_credit] => 23
[libelle] => Achat de marchandises
[student_id] => 1
[CompteDebit] => Array
(
[id] => 2
[nom] => achat marchandises
)
[CompteCredit] => Array
(
[id] => 1
[nom] => caisse
)
)
)
)
Now if I want to access to the first level I simply use:
$this->Form->input('Ecriture.0.enonce');
And everything works fine!
But I can't access the seconde level using:
$this->Form->input('Ecriture.0.Ligne.0.libelle');
Why is that so? Can somebody help me?
Try the following:
Within Controller:
$this->data = array(
'Exercice' => array('column1' => 'value1', 'column2' => 'value2', etc.),
'Ecriture' => array(0 => array('column1' => 'value1', 'column2' => 'value2', etc.)),
'Ligne' => array(0 => array('column1' => 'value1', 'column2' => 'value2', etc.)),
)
As you can see, what I'm proposing is to place the data for associated models in the same level and NOT nested one within the other. So, by this approach, you can set the form elements like: $this->Form->input('Exercice.column1'); or $this->Form->input('Ecriture.0.column1');
By the way, now that I think about it, you should be able to populate the $this->data array automatically (for the form fields) by using read(), like: $this->data = $this->ModelName->read(null, $recordId). See more here in the cookbook.