saveall with hasOne saves blank foreign key - cakephp

I have th e following model relationships:
Enquiry:
var $hasOne = array(
'SeminarAttendence' => array(
'className' => 'SeminarAttendence'
)
);
SeminarAttendence:
var $belongsTo = array(
'Enquiry' => array(
'className' => 'Enquiry',
'foreign_key' => 'enquiry_id',
)
);
my post data looks like this:
[Enquiry] => Array
(
[first_name] => joe
[last_name] => soap
[email_address] =>
[tel_home] =>
[tel_work] =>
[tel_cell] =>
)
[SeminarAttendence] => Array
(
[branch_id] => 178 // this has no table relation it's for a web service
)
I saveAll this in a controller:
$this->Enquiry->saveAll($this->data, array('validate' => 'first', 'atomic' => false
when I am done I get result like this in the SeminarAttendence
id branch_id enquiry_id
1 4 0
2 4 0
3 3 0
4 1 0
It worked fine on php5 yesterday, now when i ported it to our dev server (php4 ) it doesnt work?

It's not cakephp problem. because cakephp is made for php4, and works properly on php5. maybe something wrong with datebase, or config

Related

CakePHP How to get Photo from different table?

Hi i have two table friends and galleries I want to use CakePHP model Association between two table which Friend.friend_id = 1 and Gallery.user_id = 1
friends
id friend_id
-------------
1 5
2 1
3 6
galleries
id user_id photo
------------------------
1 1 photo1.jpg
2 6 photo2.jpg
3 5 photo3.jpg
Above two table i want to join. But i am unable to join. Please help me.
After lots of research finally i got my answer. This code helps to other-
//$user_id=$this->Session->read('Auth.User.id');
$user_id=1;
$this->Friend->bindModel(
array('hasOne' => array(
'Gallery' => array(
'className' => 'Gallery',
'foreignKey' => FALSE,
'conditions' => array('Friend.friend_id=Gallery.user_id'),
'type' => 'LEFT'
)
)
)
);
$this->Friend->bindModel(array('belongsTo' => array('User' => array('className' => 'User', 'foreignKey' => 'friend_id', 'conditions'=>array('Friend.user_id'=>$user_id)))));
$data = $this->Friend->find('all');
pr($data);

Cake php multimodel form post parameters

I'm a cakephp newbie, and I was ordered to use the 1.3 version.
I can't understand (and both guides and api docs don't tell it) how I could create an HABTM association in a POST request.
I'm trying to create a wine, that could be made of many vines. For example I'm creating a "soave" whine, that is made of "garganega" and "chardonnay" vines.
How should the POST params should be?
Given theses models
class Wine extends AppModel{
var $hasAndBelongsToMany = array(
'Vine' => array(
'className' => 'Vine',
'joinTable' => 'wine_vines',
'foreignKey' => 'wine_id',
'associationForeignKey' => 'vine_id',
'with' => 'WineVine',
),
);
}
class Vine extends AppModel{
var $hasAndBelongsToMany = array(
'Wine' => array(
'className' => 'Wine',
'joinTable' => 'wine_vines',
'foreignKey' => 'vine_id',
'associationForeignKey' => 'wine_id',
'with' => 'WineVine',
),
);
}
class WineVine extends AppModel{
var $name = "WineVine";
public $belongsTo = array("Wine", "Vine");
}
I tried a POST like this:
Array
(
[Wine] => Array
(
[denomination] => Soave DOP
[fantasy_name] =>
[kind] => White
)
[Vine] => Array
(
[0] => Array
(
[name] => garganega
)
[2] => Array
(
[name] => chardonnay
)
)
)
but it does not perform any inserts in vine table, only in wine.
Here's the log:
2 INSERT INTO `wines` (`denomination`, `fantasy_name`, `kind`, `modified`, `created`) VALUES ('', '', '', '2013-10-25 17:27:14', '2013-10-25 17:27:14') 1 55
3 SELECT LAST_INSERT_ID() AS insertID 1 1 1
4 SELECT `WineVine`.`vine_id` FROM `wine_vines` AS `WineVine` WHERE `WineVine`.`wine_id` = 2 0 0 1
5 SELECT `Vine`.`id`, `Vine`.`name`, `Vine`.`created`, `Vine`.`modified` FROM `vines` AS `Vine` WHERE 1 = 1 5 5 0
6 SELECT `Wine`.`id`, `Wine`.`denomination`, `Wine`.`fantasy_name`, `Wine`.`kind`, `Wine`.`created`, `Wine`.`modified`, `Wine`.`producer_id`, `WineVine`.`id`, `WineVine`.`wine_id`, `WineVine`.`vine_id`, `WineVine`.`created`, `WineVine`.`modified` FROM `wines` AS `Wine` JOIN `wine_vines` AS `WineVine` ON (`WineVine`.`vine_id` IN (1, 2, 3, 4, 5) AND `WineVine`.`wine_id` = `Wine`.`id`)
after saving the wine, try injecting its id into the data array
$this->data['Wine']['id'] = $this->Wine->id;
and then call an overloaded Model::saveAssociated() that will save all vines and update the join table by itself.
this overloaded method is described at:
http://bakery.cakephp.org/articles/ccadere/2013/04/19/save_habtm_data_in_a_single_simple_format
edit: sorry, that's for cake 2.x
i just realized 1.3 has no saveAssociated method
edit 2: but it does work in cake 1.3 if you change the last line of the saveAssociated method to
return parent::saveAll($data, $options);

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.

CakePHP: SaveAll() and hasMany data replace problem

I made one big form with many associations hasMany/HABTM. All is workin great on creating (!). When updating all is working nice too, but in tables where association is hasMany, data is not updated or replaced, but just inserted. This brings many rows with trash data. How can I make saveAll() do the update/replace in hasMany fields:
Model:
class MainModel extends AppModel {
var $hasAndBelongsToMany = array(
'HABTMModel1',
...
'HABTMModeln',
);
var $hasMany = array(
'Model1' => array(
'dependent' => true
),
...
'Modeln' => array(
'dependent' => true
),
);
}
One of the problematic hasMany models look like:
class Model1 extends AppModel {
var $belongsTo = array(
'MainModel'
);
}
And his table have:
id <- Primary key, auto increment, int (11)
main_model_id <- Foreign_key int (11)
name <- text field, string
The $this->data is looking like:
array(
[MainModel] => array(
'id' => 123
*** aditional data named identicaly to table fields (working great)***
),
[Model1] => array(
[0] => array(
[name] => Test1
),
[2] => array(
[name] => Test2
),
),
*** all other models ***
);
Model1 table results after first creating and after updating:
id main_model_id name
--------------------------------------------------------------
11 306 Test1
12 306 Test2
13 306 Test1 (Thease are dublicates)
14 306 Test2 (Thease are dublicates)
What can I do to update/replace data in hasMany and not insert new values un edit using saveAll ?
Thank you.
In the view, just put hidden input for all the Model1, Model2 ids
echo $form->create('MainModel');
echo $form->hidden('Model1.0.id');
// more stuffs...
echo $form->end('Save');
You probably specify the 'fields' for Model1, Model2 to have only 'name'. That's why $this->data looks like that. So just add 'id' to that.
So I made not so beautiful, but working:
// If edit, then delete data from hasMany tables based on main_model_id
if(isset($this->data['MainModel']['id']) && !empty($this->data['MainModel']['id'])) {
$conditions = array('main_model_id' => $this->data['MainModel']['id']);
// Delete Model1
$this->MainModel->Model1->deleteAll($conditions, false);
...
all other models
...
}
$this->MainModel->saveAll($this->data);
You need to set the id field for your hasMany model too e.g.
array(
[MainModel] => array(
[id] => 123
*** aditional data named identicaly to table fields (working great)*** ),
[Model1] => array(
[0] => array(
[id] => 1,
[name] => Test1
),
[2] => array(
[id] => 2
[name] => Test2
), ), *** all other models *** );

Cakephp paginator sort method with a habtm relation question

Here's an example...
I'm using a table to manage all of my Statuses with the tree behavior. I have a Book model which has a status for it's condition and one for it's availability. So the Book model HABTM Statuses even thought it can only have one condition and one availability.
Is there a way to use the $paginator->sort('Condition', '?') with a HABTM ? I would like to sort the title from the array below.
This is what $this->data is giving me :
[Status] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[lft] => 2
[rght] => 3
[title] => Mint
[created] => 2010-01-18 16:20:54
[modified] => 2010-01-18 16:20:54
[active] => 1
[BooksStatus] => Array
(
[id] => 7
[book_id] => 3
[status_id] => 2
)
)
)
i would use 2 x belongsTo with different names.
$belongsTo = array(
'BookStatus' => array(
'className' => 'Status',
'foreignKey' => 'book_status_id'
),
'BookCondition' => array(
'className' => 'Status',
'foreignKey' => 'book_condition_id'
)
)
dont know how this would then fit into your tree strut
if that doesnt work then Try $paginator->sort('Condition', 'BooksStatus.status_id');
you will need to make sure the query used to retrieve the above does a join(unBind/bind on the fly)

Resources