CakePHP - Writing to 2 db tables from the same view - cakephp

I'm trying to write a small expenses cake app. Basicaly I have a expenseClaim that hasMany Expenses (expense belongsTo expenseClaim). When you add/edit a expenseClaim I want to be able to add multiple expenses for that expense claim from within that view. Could someone point me in the right direction?
Any tutorials / sample code would be massively appreciated. Thanks in advance
using cake 2.1

Put in expenseClaim/add:
$this->expenseClaim->Expense->create();
It should work if the relationship is well set in the model.
If not,
$this->loadModel('Expense');
$this->Expense->create();
should always work.
Edit:
$data=array
(
[Expense] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
$this->loadModel('Expense');
$this->Expense->create();
if ($this->Expense->save($data)) {
$this->Session->setFlash(__('Done.'));
} else {
$this->Session->setFlash(__('Failure.'));
}
Edit2: if you want to pass data from the view:
if ($this->request->is('post')) {
$this->loadModel('Expense');
$this->Expense->create();
if ($this->Expense->save($this->request->data)) {
$this->Session->setFlash(__('Done.'));
} else {
$this->Session->setFlash(__('Failure'));
}
}
The request is created automatically if you use the form helper in the view.

Related

Creating two HABTM associated records, simultaneously

In a CakePHP, app, let's assume I have two models: Team and User. Those models are associated with each other in a HABTM relationship (joined by table teams_users).
Say we have a form where someone can specify details to create a new Team and User, at the same time.
What is the cleanest, most straightforward way to create those records simultaneously, with an association to each other?
What are the "gotchas" for naming form fields, and processing in the Controller?
I want to do this in the most Cake friendly way, so that I'm able to return validation errors on both models.
This method would go into your Team model, assuming all assocs and HABTM are set up right.
public function createTeam($postData) {
$this->set($postData);
$this->User->set($postData);
$validTeam = $this->validates();
$validUser = $this->User->validates()
if ($validTeam && $validUser) {
$this->create();
$this->save($postData, array('validate' => false);
$this->User->create();
$this->User->save($postData, array('validate' => false);
$this->TeamsUser->create();
$this->TeamsUser->save(array(
'TeamsUser' => array(
'user_id' => $this->getLastInsertId()
'team_id' => $this->User->getLastInsertId()
)
));
return true;
}
return false;
}

cakephp saving data for a related model's related model (not typo)

I've run into a bit of a problem saving data in cake php.
here are the models/relationships.
District hasMany Departments
Department hasMany Groups
I am in a view for creating new district, in which I've allowed the user to create multiple new departments. while creating each department, the user may create multiple groups for that dept. Now the trouble is I'm unsure of how to save the group data.
for each department that is created on the fly, im using the multiple index method for the inputs (i.e. "Department.0.name", Department.0.type) so this will be a cinch to save using the saveAll method. However, for each group that is created, i will need a department_id, and since none of the District's departments have yet been saved, they don't have an id. how can i save this new district's data, saving the new departments, and their associated new created groups? is there a way that i can address the name attribute of the group inputs that will create the proper association, something like "Department.0.Group.0.name", for instance?
Thanks in advance!!! if anything is unclear, please don't hesitate to say so, I'll be glad to rephrase.
What does your POST data array look like?
<?php
debug($this->data);
?>
If it is not in the correct format, the associated models won't get saved.. Cake knows to grab the "lastInsertId()" of the models which haven't been saved yet, so you don't have to worry about those... What i'm not sure about, and the docs don't really go into, is how deep the save goes. The example provided is as follows:
$this->data =
Array
(
[Article] => Array
(
[title] => My first article
)
[Comment] => Array
(
[0] => Array
(
[comment] => Comment 1
[user_id] => 1
)
[1] => Array
(
[comment] => Comment 2
[user_id] => 2
)
)
)
$this->Article->saveAll($this->data);
This is the correct structure (cakephp 1.3) for saving associated models of a 'hasMany' relationship, but i'm not sure if it goes any deeper than one child.
One thing that comes to my mind is to build the array according to the format above, but leave the parent model out. Then manually save the parent model data, grab the ::getLastInsertId(); then do a saveAll on departments and groups.
[UPDATE]
I just tested your theory and it will work the way you intend.
<?php
echo $this->Form->input('Department.0.Group.0.name');
?>
Will produce:
<input name="data[Department][0][Group][0][name]" type="text" id="Department0Group0name">
[UPDATE 2]
I did some exploring in lib/Cake/Model/Model.php and found this:
<?php
...
public function saveAssociated($data = null, $options = array()) {
...
... // code omitted.
...
if ($options['deep']) { // This will recurse infinitely through all associations
$saved = $this->{$association}->saveAssociated($values, array_merge($options, array('atomic' => false)));
}
...
...
... // code omitted.
...
?>

Multiple models and uploading files

At first - I'm new to the Yii Framework. I did some research on my own but I couldn't find a precise solution to my issue.
Assume there are two related models - Product and Image. A single Product may have multiple Images assigned. What is the best approach at creating the create / update forms that would be able to manage this kind of scheme?
The Image model consists of various fields, along with a path to the image file, so it's not just a "container" for the path itself. What's more - I need to have a thumbnail generated for every uploaded image and its path stored within the same model.
What I need to achieve is pretty much similar to the admin inline functionality known from Django - there should be a section in the Product create / update form which would allow users to add / modify / delete Images.
I tried the multimodelform extension but I couldn't get file uploading to work. What's the best way of getting it done and not having to build the whole file-upload-enabled-multiple-model-form structure manually?
The detailed solution depends on if you are using CActiveForm or CHtml form. Since you have 2 related models I assume you are using CActiveForm and will point out some thing you need to keep in mind.
For this example i am gonna assume some definitions
Product with fields id, name
Product with ONE to MANY relation to 'images' on ProductImage
ProductImage with fields id, productId, path
I also assume there gonna be 1 upload / edit, but multi delete
Here's the view:
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
echo $form->labelEx($product, 'name');
echo $form->fileField($product, 'name');
echo $form->error($product, 'name');
echo $form->checkBoxList($product, 'path', $product->images);
echo $form->labelEx($productImage, 'path');
echo $form->fileField($productImage, 'path');
echo $form->error($productImage, 'path');
$this->endWidget();
And your action
public function actionUpdate($productId) {
$product = Product::model()->findByPk($productId)->with('images');
$productImage = new ProductImage();
if(isset($_POST['Item']))
{
$product->attributes=$_POST['Product'];
foreach($product->images as $im) {
if(in_array($im->path, $_POST['Item']['Image']))
$im->delete();
}
$productImage->image=CUploadedFile::getInstance($productImage,'path');
if($productImage->save())
{
$productImage->image->saveAs('some/new/path');
// redirect to success page
}
}
$this->render('update', array(
'product'=>$product,
'productImage'=>$productImage,
));
}
Now note that this solution is not tested so there will be bugs, but it should give you an idea on how to write your own form.
Resources:
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
http://www.yiiframework.com/wiki/384/creating-and-updating-model-and-its-related-models-in-one-form-inc-image

Saving related model data (3 levels)

Really struggling with saving model info a few levels down.
Firstly, I have a expenseClaim – that hasMany expenses (expenses belongTo expenseClaim).
Expenses HABTM ExpenseCode. (these are things like train fare, petrol costs etc).
So, I have a Add a new Expense page – (ExpenseClaimsController /add).
This kinda looks a bit like a spreadsheet and it allows you to enter multiple rows (each row being a expense). On each row theres a drop down to select the expenseCode. The issue is that the expenseCode is not getting written to the join table (expenses_expense_codes).
If I add a single expense, through the unused and soon to be removed expenseController /add it works fine, so the issue is saving assoisiated models, models. Cant figure out how to do this as expenseCode is not directly related to the expenseClaim model and therefore not saving when I do ExpenseClaimsController /add.
Hope that makes sense….
Heres my ExpenseClaimsController code:
public function add() {
if ($this->request->is('post')) {
//debug($this->request->data);
// Test
$this->loadModel('ExpenseCode');
// Create the claim and the expenses
$this->ExpenseClaim->create();
$this->ExpenseClaim->Expense->create();
// Set the user ID
$this->request->data['ExpenseClaim']['user_id'] = $this->Auth->user('id');
// Set the claim status based on which submit btn was pressed
if ($this->request->data['submit'] == 'Submit') {
$this->request->data['ExpenseClaim']['claim_status_id'] = '2';
} else {
$this->request->data['ExpenseClaim']['claim_status_id'] = '1';
}
// Set the claim status
$now = date('Y-m-d H:i:s');
$this->request->data['ExpenseClaim']['date_submitted'] = $now;
// Save both expenses and the expense claim
if ($this->ExpenseClaim->saveAll($this->request->data)) {
$this->Session->setFlash('The expense claim has been saved', 'flash_success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The expense claim could not be saved. Please, try again.', 'flash_failure');
}
}
$users = $this->ExpenseClaim->User->find('list');
$claimStatuses = $this->ExpenseClaim->ClaimStatus->find('list');
$expenseCodes = $this->ExpenseClaim->Expense->ExpenseCode->find('list');
$mileageRate = $this->Auth->user('mileage_rate');
$this->set(compact('users', 'claimStatuses', 'expenseCodes', 'mileageRate'));
// Set errors for top of page
$this->set('errors', $this->ExpenseClaim->validationErrors);
}
Can anyone point me in the right direction? Thanks in advance.
Managed to track down the issue, I had to rename the field in ExpenseClaim add to
$this->Form->input('Expense.0.ExpenseCode.ExpenseCode');
This then solved the issue.
Hopefully this will help someone.

CakePHP: Can I ignore a field when reading the Model from the DB?

In one of my models, I have a "LONGTEXT" field that has a big dump of a bunch of stuff that I never care to read, and it slows things down, since I'm moving much more data between the DB and the web app.
Is there a way to specify in the model that I want CakePHP to simply ignore that field, and never read it or do anything with it?
I really want to avoid the hassle of creating a separate table and a separate model, only for this field.
Thanks!
Daniel
As #SpawnCxy said, you'll need to use the 'fields' => array(...) option in a find to limit the data you want to retrieve. If you don't want to do this every time you write a find, you can add something like this to your models beforeFind() callback, which will automatically populate the fields options with all fields except the longtext field:
function beforeFind($query) {
if (!isset($query['fields'])) {
foreach ($this->_schema as $field => $foo) {
if ($field == 'longtextfield') {
continue;
}
$query['fields'][] = $this->alias . '.' . $field;
}
}
return $query;
}
Regarding comment:
That's true… The easiest way in this case is probably to unset the field from the schema.
unset($this->Model->_schema['longtextfield']);
I haven't tested it, but this should prevent the field from being included in the query. If you want to make this switchable for each query, you could move it to another variable like $Model->_schemaInactiveFields and move it back when needed. You could even make a Behavior for this.
The parameter fields may help you.It doesn't ignore fields but specifies fields you want:
array(
'conditions' => array('Model.field' => $thisValue), //array of conditions
'fields' => array('Model.field1', 'Model.field2'), //list columns you want
)
You can get more information of retrieving data in the cookbook .
Another idea:
Define your special query in the model:
function myfind($type,$params)
{
$params['fields'] = array('Model.field1','Model.field2',...);
return $this->find($type,$params);
}
Then use it in the controller
$this->Model->myfind($type,$params);
Also try containable behaviour will strip out all unwanted fields and works on model associations as well.
Containable
class Post extends AppModel { <br>
var $actsAs = array('Containable'); <br>
}
where Post is your model?
You can add a beforeFilter function in your Table and add a select to the query
Excample:
public function beforeFind(Event $event, Query $query){
$protected = $this->newEntity()->hidden;
$tableSchema = $event->subject()->schema();
$fields = $tableSchema->columns();
foreach($fields as $key => $name){
if(in_array($name,$protected)){
unset($fields[$key]);
}
}
$query->select($fields);
return $event;
}
In this excample I took the hidden fields from the ModelClass to exclude from result.
Took it from my answer to a simular question here : Hidden fields are still listed from database in cakephp 3

Resources