CakePHP - $this->data disappears before Model::save - cakephp

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']);

Related

CakePHP same array from the direct and associated model

I have simple model relationship in CakePHP 1.3 with Categories -> Products
Category hasMany Products
There is slight difference between the data arrays which I get in the different controllers. The Product data is in the main product array when getting as associated model in the Categories controller and is separated when getting it in Products.
For Example to get 'Product1'
in Categories - $category['Product'][0]['title']
and in Products - $product[0]['Product']['title']
I would like to use same element for displaying the products. It does not matter which array scheme will be used just to be the same. And where is the right place to make the modification? I can modify those arrays after getting them, but don't think that it is the best option.
When I am in the Categories controller and get a category I get this:
// $this->Category->findById('12');
Array
(
[ProductCategory] => Array
(
[id] => 12
[title] => Category 1
[updated] => 2013-02-24 10:06:15
[created] => 2013-02-24 10:06:15
)
[Product] => Array
(
[0] => Array
(
[id] => 4
[parent_id] => 12
[title] => Product1
[updated] => 2013-02-24 10:17:01
[created] => 2013-02-24 09:12:59
)
[1] => Array
(
[id] => 6
[parent_id] => 12
[title] => Product2
[updated] => 2013-02-24 10:16:54
[created] => 2013-02-24 09:13:53
)
)
And when getting all the products inside the Products controller:
// $this->Product->find('all');
Array
(
[0] => Array
(
[Product] => Array
(
[id] => 10
[parent_id] => 12
[title] => Product1
[updated] => 2013-02-24 10:16:42
[created] => 2013-02-24 09:16:35
)
)
[1] => Array
(
[Product] => Array
(
[id] => 8
[parent_id] => 12
[title] => Product2
[updated] => 2013-02-24 10:16:47
[created] => 2013-02-24 09:15:39
)
)
)
)
One of your finds is a find('all') and the other is a findById() (which uses find('first')).
Both of these return data in a different format, since find('first') knows you only want one item, and find('all') is an unknown set of item(s).
Just use find('all') for both, but set your limit based on whether you need only one or more than one. Then, your data will be returned exactly the same.
Which Controller you retrieve your data from has no effect on the data returned. Which MODEL however, does - so just make sure you're doing your find from the same model.
Eg.
//in your ProductsController
$this->Product->find('all');
//in your CategoriesController
$this->Category->Product->find('all');
// in some other controller
$this->loadModel('Product);
$this->Product->find('all');
PS - BUT it's better if you don't do your "finds" in your Controller - make a method in your Model, and call it from your Controller(s) so instead of $this->Product->find(), it would be $this->Product->getProducts() (or whatever you want to call it). (read more about "fat models, skinny controllers" for reasons/examples...etc).
Dave is right, the difference is the method you're using... Even though you claim that associated data is always merged, your find on the 'Product' model is NOT associated data, so the format WILL always be different.
I've been here for a short while and I've already noticed that Dave knows his stuff. :)
I agree with the fat models/skinny controllers paradigm for clean, efficient code.
If you changed:
<?php
$this->Category->contain(array(
'Product'
));
$this->Category->find('all',
array(
'conditions' => array(
'Category.id' => $id // 12 for your OP.
),
'limit' => 1
)
);
?>
Should give you:
<?php
array
(
[0] => array
(
'Category' => array
(
[id] => 12
[title] => Category 1
[updated] => 2013-02-24 10:06:15
[created] => 2013-02-24 10:06:15
),
'Product' => array
(
[0] => array
(
...
),
[1] => array
(
...
)
)
)
)
?>
Please correct me if I am mistaken, thanks!
Or if you want "Products" to look like:
<?php
'Product' => array
(
[0] => array
(
'Product' => array
(
...
)
)
)
?>
when fetching data from the category model, you would need to fetch the associated data manually, e.g.:
<?php
$this->Category->contain();
$cats = $this->Category->find('all');
foreach ($cats as &$cat) {
$this->Category->Product->contain(); // You have to contain for each find.
$cat['Product'] = $this->Category->Product->find('all',
array(
'conditions' => array(
'Product.category_id' => $cat['Category']['id']
)
)
);
}
?>

CakePHP - two models on one page, can't get this->request->data automagic field population

I have two controllers: events and results. Events hasMany results, results belongsTo Events. I can save just fine, but when I go to edit, I can only get the information for the Event part of the form, to come in automagically.
I build the Results form info like this:
$option_number = 5;
for ($i = 0; $i < $option_number; $i++) {
echo $this->Form->select("Result.{$i}.object_id", $qual_options, array('empty' => false, 'class' => 'result-name'));
echo $this->Form->hidden("Result.{$i}.id");
echo $this->Form->hidden("Result.{$i}.type", array('value' => 'qual'));
echo $this->Form->hidden("Result.{$i}.action", array('value' => 'add')); ?>
}
In the backend, when I'm doing this to get the automagic population:
if ($this->request->is('get')) {
$this->request->data = $this->Event->findById($id);
}
It works just fine, but I can't figure out how to get it to show the Results. I've tried many things, most probable being:
$this->request->data['Result'] = $this->Result->findAllByEventId($id);
With that, I end up with a data structure like:
[Result] => Array
(
[0] => Array
(
[Result] => Array
(
[id] => 1
[object_id] => 1
[type] => qual
[action] => add
[amt] => 10
[event_id] => 1
)
)
[1] => Array
(
[Result] => Array
(
[id] => 2
[object_id] => 2
[type] => qual
[action] => add
[amt] => 1
[event_id] => 1
)
)
... etc.
)
)
Which definitely looks fishy, I just can't seem to manipulate it to work.
UPDATE I should have mentioned this; this is what my data looks like when I SAVE it, and I want to mimic this!
[Result] => Array
(
[0] => Array
(
[object_id] => 1
[type] => qual
[action] => add
[amt] => 0
[event_id] => 3
)
[1] => Array
(
[object_id] => 1
[type] => qual
[action] => add
[amt] => 1
[event_id] => 3
)
You can see that each numeric key after just has the information in it; instead, my numeric keys ALSO have an array INSIDE them name Result, and I have no idea how to make that go away properly! :} I could always loop through and build it in the format CakePHP wants, but I want to do it properly. And that single line above is what needs changing, but I have run out of ideas.
What about just using find('first') against the event? Since it's hasMany, it will return the Result model in a single [Result] key with many numeric keys.
$this->request->data = $this->Event->find('first', array(
'conditions' => array(
'Event.id' => $id
),
'contain' => array(
'Result'
)
));
This will return something like:
array(
'Event' => array(
'id' => 1,
'name' => 'event name'
),
'Result' => array(
0 => array(
'id' => 1,
...
),
1 => array(
'id' => 2,
...
)
)
);
You could unset the Event key if you needed to.

hasOne subtype on CakePHP

I have 3 users tables on my DB:
Users
users_twitter
users_web
Users is the parent of both users_twitter and users_web.
And then another table for comments. Both kind of users can comment.
So, when I want to show the comments I need to get information of the author of each comment.
In order to do this I am using the variable $belongsTo on the /Model/Comment.php like this:
public $belongsTo = array(
'users' => array(
'foreignKey' => 'idUser'
)
);
In Controller/CommentController, when i do this:
public function index($idPost) {
$this->Comment->recursive = 0;
return $this->Comment->find('all');
}
I get this kind of array:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
)
)
And what I want is to get more info yet, the one of the subtype of Users, depending on the existence of the ID on the subtype tables.
In case it was an id of a user_web table, I would like to get this:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
[users_web] => Array
(
[id] => 1
[username] => myName
[password] => XXXXX
[verified] => 1
[created] => 1
)
)
Do you know if that's possible with CakePHP?
As far as I know, with $hasOne I only get one level more, I need two.
In your AppModel add
public $actsAs = array('Containable');
In your index:
public function index($idPost) {
return $this->Comment->find('all', array(
'contain' => array('User.UsersWeb')));
}
Your User model has to be associated with a UsersWeb model to make this work.
See http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations for a complete description.
Also why do you return the data in the controller index? Are you using requestAction for this? And why don't you use pagination? Do you really want to display hundreds of comments on one page?
See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

Automagic with related model data

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.

Save multiple records for one model in CakePHP

I would like to save several records for one model. This would have been done pretty easily with saveAll() if it hadn't been for a problem:
I have a notification form, where I select multiple users from a <select> and two fields, for subject and content respectively. Now, when I output what $this->data contains, I have:
Array([Notification] => Array
(
[user_id] => Array
(
[0] => 4
[1] => 6
)
[subject] => subject
[content] => the-content-here
)
)
I've read on Cake 1.3 book, that in order to save multiple records for a model, you have to have the $this->data something like:
Array([Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
So, how do I 'share' the subject and content to all selected users?
First off, this database design needs to be normalized.
It seems to me like a Notification can have many Users related to it. At the same time, a User can have many Notifications. Therefore,
Introduce a join table named users_notifications.
Implement the HABTM Relationship: Notification hasAndBelongsToMany User
In the view, you can simply use the following code to automagically bring up the multi-select form to grab user ids:
echo $this->Form->input('User');
The data that is sent to the controller will be of the form:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[User] => Array
(
[User] => Array
(
[0] => 4
[1] => 6
)
)
)
Now, all you have to do is called the saveAll() function instead of save().
$this->Notification->saveAll($this->data);
And that's the Cake way to do it!
Those values have to be repeat like this
Array([Notification] => Array(
[0] => Array
(
[user_id] => 4
[subject] => subjects
[content] => content
)
[1] => Array
(
[user_id] => 6
[subject] => subject
[content] => contents
)
)
)
$this->Notification->saveAll($data['Notification']); // should work
If you don't pass any column value, this cake will just ignore it
You're going to have to massage your form's output to suit Model::saveAll. In your controller:
function action_name()
{
if ($this->data) {
if ($this->Notification->saveMany($this->data)) {
// success! :-)
} else {
// failure :-(
}
}
}
And in your model:
function saveMany($data)
{
$saveable = array('Notification'=>array());
foreach ($data['Notification']['user_id'] as $user_id) {
$saveable['Notification'][] = Set::merge($data['Notification'], array('user_id' => $user_id));
}
return $this->saveAll($saveable);
}
The benefit here is your controller still knows nothing about your model's schema, which it shouldn't.
In fact, you could probably redefine saveAll in your model, which hands off correctly formatted input arrays to parent::saveAll, but handles special cases itself.
There might be a more cakey way of doing this, but I've used this kind of technique: First, change the form so that you get an array like this:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[selected_users] => Array
(
[id] => Array
(
[0] => 4
[1] => 6
)
)
)
(Just change the multiselect input's name to selected_users.id)
Then loop through the user ids and save each record individually:
foreach( $this->data[ 'selected_users' ][ 'id' ] as $userId ) {
$this->data[ 'Notification' ][ 'user_id' ] = $userId;
$this->Notification->create(); // initializes a new instance
$this->Notification->save( $this->data );
}

Resources