Save Related Model Data - php

I am having trouble understanding how to save Related Model Data in Cakephp 1.3. I have a model called Policy and a Model called Declinations. Policy hasMany Declinations. Declinations belongsTo Policy. All I want to do is save the policy_id in the Policy table in a field called policy_id in the Declinations table. So the question is how do I grab the Policy_Id from the corresponding model? Do I need to set the Policy_Id first in the Declination model or controller and then pass it to a hidden field in the view?
Here is my declination model
public $belongsTo = array(
'Policy' => array(
'className' => 'Policy',
'foreignKey' => 'policy_id'
My Policy Model is as follows
public $hasMany = array(
'Declination' => array(
'className' => 'Declination',
'foreignKey' => 'policy_id',
'dependent' => true
)
Here is my controller
public function add($id = null) {
if (!empty($this->data)) {
$this->Declination->create();
if ($this->Declination->saveAll($this->data['Declination'])) {
$this->Session->setFlash(__('Declinations saved.', true));
$this->redirect(array(
'controller' => 'coverages',
'action' => 'view',
$id
));
} else {
$this->Session->setFlash(__('Declinations failed to save.', true));
}
}
$reasons = $this->Declination->Reason->find('list');
$contactTypes = $this->Declination->ContactType->find('list');
$this->set(compact('id', 'reasons', 'contactTypes'));
}
Here is my view
<?php echo $this->UiForm->input("Declination.{$i}.first_name"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.last_name"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.company"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.contact_type_id"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.phone_number"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.reason_id"); ?>
<?php echo $this->UiForm->input("Declination.{$i}.other", array(
'label' => 'If other, please supply a reason'
)); ?>
<?php echo $this->UiForm->input("Declination.{$i}.dated", array(
'type' => 'text',
'readonly' => 'readonly',
'data-datepicker' => ''
)); ?>
Any input would be greatly appreciated! Thanks!

Somehow, you must know WHICH Policy id you want to save - so you can either add that as a field in your view:
echo $this->UiForm->input("Declination.{$i}.policy_id", array('value'=>$policyId));
Or you can set it in your Controller before you save:
$this->request->data['Declination']['policy_id'] = $policyId;
Either way will work - use whichever makes more sense for your site.
How and where you GET the policy id is also up to you. You can use a find() in the Controller, then set() it so it's accessible in the view. Or, you can use a find() and retrieve it just before the save. Without knowing what data you have available where, it's hard to give more detail than that.

Related

CakePHP - Correct way to do hasMany through

I have 3 models
Categories:
class Category extends AppModel {
public $belongsTo = array(
'Parent' => array(
'className' => 'Category',
'foreignKey' => 'parent_id'
),
);
public $hasMany = array(
'Children' => array(
'className' => 'Category',
'foreignKey' => 'parent_id'
),
'UserCategoryMeta'
);
}
Users:
class User extends AppModel {
public $hasMany = array(
'UserCategoryMeta' => array(
'className' => 'UserCategoryMeta',
'foreignKey' => 'user_id',
),
);
}
UserCategoryMeta:
class UserCategoryMeta extends AppModel
{
public $belongsTo = array(
'User', 'Category'
);
}
What I need to do is have each user be able to choose many categories and for each of those associations I need the user to set search terms which is just 1 field in the DB.
So the UserCategoryMeta table looks like this:
id | user_id | category_id | search_terms
I've found a way which might work but it seems very hacky.
In the usercontroller I have:
$Categories = $this->User->Category->find('list');
Then in the add view I have the checkboxes:
echo $this->Form->input('Category.Category',array(
'type' => 'select',
'multiple' =>'checkbox',
'options' => $Categories,
));
Then the only way I could get each of those checkboxes to have a search terms input next to them is to do this in the add view:
foreach ($Categories as $key => $category){
echo '<input type="text" id="Category'.$key.'SearchTerms" name="data[Category][search_terms]['.$key.']"><br/>';
}
This produces what I want but obviously since I'm just creating random inputs when the form get's submitted it get's black holed. I have managed to get passed this but I know I'm doing it the wrong way and hopefully someone can help me do it the right way.
Also then once I have this data array in the controller im not sure how to save it to the database correctly.
Thanks in advance for any help!
Just do what you're doing, but in the repeat, use $this->Form->input instead of just manually writing the HTML.

CakePHP to save relationship in other model

I have these models:
class Prefix extends AppModel {
public $displayField = 'prefix';
public $hasMany = array(
'State' => array(
'className' => 'State',
'foreignKey' => 'prefix_id',
'dependent' => false,
),
);
}
class State extends AppModel {
public $displayField = 'name';
public $belongsTo = array(
'Prefix' => array(
'className' => 'Prefix',
'foreignKey' => 'prefix_id',
),
);
}
Then I have this admin_add method, from the automatic scaffolder:
public function admin_add() {
if ($this->request->is('post')) {
$this->Peefix->create();
if ($this->Prefix->save($this->request->data)) {
$this->redirect(array('action' => 'index'));
} else {
// Error message
}
}
$states = $this->Prefix->State->find('list');
$this->set(compact('states'));
}
I also have the list of them in my form:
<?php echo $this->Form->input('State', array('multiple' => 'checkbox', 'type' => 'select',)); ?>
Now I can set the States for the Prefix. However, when I submit the form, the selection disappears. It is not saved in the database.
What did I do wrong?
You linked the models as if there is only one state per prefix, and many prefixes "assigned" to one state. That means you cannot use 'multiple' => 'checkbox'. So either remove this or change model associations to HABTM.
First, both foreign keys for hasMany and belongsTo must be the same. If in the parent model you provided invoice_circle_id as the key, then the same must be provided in the child model also. Obviously, that field must exist in the child table. See this for more info http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
Second - you might want to use the saveAll() or saveAssociated() method for linked model data saving. Again - http://book.cakephp.org/2.0/en/models/saving-your-data.html contains all the ifnormation you need.
As for naming the input fields for hasMany, you name them like this:
$this->Form->input('ParentModel.fieldname');
$this->Form->input('ChildModel.0.fieldname');

CakePHP 2.X: HABTM with Bookmarks / Tags, saving from same form

I'm trying to build a simple bookmarking site as a project to learn some CakePHP, but am having significant trouble figuring out how to do HABTM (or whatever I might need) for Bookmarks/Tags.
I have a simple form which has the fields 'Title', 'Url', 'Tags', which is used to create a new bookmark.
My tables are set up as follows:
bookmarks: id (primary), uid, title, url, private (boolean), time
tags: id (primary), tag
bookmarks_tags: id (primary), bookmark_id, tag_id
There's also a users table.
My bookmark model (Bookmark.php) is pretty simple so far, looks like:
class Bookmark extends AppModel {
public $name = "Bookmark";
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',
'joinTable' => 'bookmarks_tags',
'foreignKey' => 'bookmark_id',
'associationForeignKey' => 'tag_id',
'unique' => 'keepExisting',
)
);
And then some validation.
The tag model looks like:
class Tag extends AppModel {
public $name='Tag';
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Bookmark' => array(
'className' => 'Bookmark',
'joinTable' => 'bookmarks_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'bookmark_id',
'unique' => 'keepExisting',
)
);
The form on the frontend so far looks like this:
<div id="addBookmark" class="card">
<div id="addBookmarkTopSpan" class="topSpan">add new bookmark</div>
<?php echo $this->Form->create(null, array('url' => array('controller' => 'bookmarks', 'action' => 'add'))); ?>
<fieldset>
<?php echo $this->Form->input('Bookmark.title');
echo $this->Form->input('Bookmark.url');
echo $this->Form->input('Bookmark.private', array('type' => 'checkbox'));
echo $this->Form->input('Bookmark.uid', array('type' => 'hidden', 'value' => $user['User']['id']));
echo $this->Form->input('Bookmark.Tag');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
As I said, I'm pretty new to Cake so I'm not sure what's going on here..currently if I enter data into that form and hit submit the bookmark gets created just fine but nothing at all happens with the tag. How do I save the data into the associated join table and the Tags table?
In your view change
echo $this->Form->input('Bookmark.Tag');
to
echo $this->Form->input('Tag.id');
or
echo $this->Form->input('Tag.0.id');
echo $this->Form->input('Tag.1.id');
echo $this->Form->input('Tag.2.id');
...
Just couple of side notes about your code ...
Since you're following CakePHP conventions, you don't need to specify the parameters for the relationship;
public $hasAndBelongsToMany = array('Tag');
and
public $hasAndBelongsToMany = array('Bookmark');
is enough.
Rather than setting the user ID in the form, which could be modified, set it in your controller:
BookmarksController.php
public function add() {
if ($this->request->is('post')) {
$this->request->data['Bookmark']['uid'] = $this->Auth->user('id');
if ($this->Note->save($this->request->data)) {
...

cakePHP bind hasmany through relationship

Ok this is kind of hard to explain but I'll try my best.
I have 3 tables
companies products product_availabilities
--------- -------- ----------------------
id id id
name name company_id
product_id
buys (tinyint)
sells (tinyint)
And their models
class Company extends AppModel
{
public $name = 'Company';
public $hasMany = array(
'ProductAvailability'
);
class Product extends AppModel
{
public $name = 'Product';
public $hasMany = array(
'ProductAvailability'
);
class ProductAvailability extends AppModel
{
public $name = 'ProductAvailability';
public $belongsTo = array(
'Company',
'Product'
);
}
What I want to do is when I create a company, I want to be able to select products that the company buys or sells. I've seen an example of a hasMany through relationship in the book (http://book.cakephp.org/1.3/view/1650/hasMany-through-The-Join-Model) but they are creating the form from the "join table" controller. Is it possible to bind the productAvailability model to my company model to be able to select the products while creating the company?
Edit : Here is how I've done it. I know it is not optimal as there is a lot of looping involved but it works.
Company controller :
$products = $this->Company->ProductAvailability->Product->find('list', array('fields' => array('Product.id', 'Product.label')));
$this->set('products', $products);
if($this->request->is('post')){
if($this->Company->save($this->request->data)){
foreach($products as $product)
{
$tmpArray = array(
'company_id' => $this->Company->id,
'product_id' => $product['Product']['id']
);
foreach($this->request->data('BuyProducts.product_id') as $buyProduct)
{
if($buyProduct == $product['Product']['id'])
$tmpArray['buys'] = 1;
}
foreach($this->request->data('SellProducts.product_id') as $sellProduct)
{
if($sellProduct == $product['Product']['id'])
$tmpArray['sells'] = 1;
}
if(count($tmpArray) > 2)
{
$this->Company->ProductAvailability->create();
$this->Company->ProductAvailability->set($tmpArray);
$this->Company->ProductAvailability->save();
}
}
$this->Session->setFlash('Yay', 'success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Nay', 'error');
}
}
Company add form :
<?php echo $this->Form->create('Company'); ?>
<?php echo $this->Form->input('name', array( 'div' => 'full-form')); ?>
<?php echo $this->Form->input('BuyProducts.product_id', array('multiple' => 'checkbox', 'options' => $products, 'div' => 'full-form', 'label' => false)); ?>
<?php echo $this->Form->input('SellProducts.product_id', array('multiple' => 'checkbox', 'options' => $products, 'div' => 'full-form', 'label' => false)); ?>
<?php echo $this->Form->end(array('label' => __('Save'), 'div' => 'center', 'class' => 'bouton-vert')); ?>
You have two options. Either let cakePHP do some magic with the hasAndBelongsToMany relationship or doing it manually which is necessary if you add attributes to the join table
1. CakePHP HABTM
Using the capabilities of CakePHP and making a straight forward solution I would make these changes:
Model
If one company has more than one product, and the products belong to many companies. It is a hasAndBelongsToMany relationship between Company<->Product
// company.php
...
var $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'companies_products',
'foreignKey' => 'company_id',
'associationForeignKey' => 'product_id',
'unique' => true,
)
);
...
// similar in product.php
Add the required table 'companies_products' in the database.
Controller
Then in the add function from the Company Controller there should be something like:
$products = $this->Company->Product->find('list');
$this->set(compact('products'));
View
Finally insert the products in the add.ctp, the select should allow multiple selections and let cakePHP do some magic, like this:
echo $this->Form->input('products', array(
'label' => 'Products to buy (Ctr+multiple choice)'
'type' => 'select',
'multiple' => true,
));
2. Manually
When the HABTM becomes more 'exotic' and includes some attributes like in your case 'buy' or 'sell' you need to do the manual way. This is in the Product Controller setting manually the fields before inserting them in the database. Something like:
foreach($availableProducts as $availableProduct ){
$this->Product->ProductAvailabilities->create();
$this->Product->ProductAvailabilities->set( array(
'company_id' => $this->Product->id,
'product_id' => $availableProduct['Product']['id'],
'buys' => $availableProduct['Product']['buy'],
'sells' => $availableProduct['Product']['sell']
// or however you send it to the controller
));
$this->Product->ProductAvailabilities->save();
}
Let's hope this helps you ...
you are planning a habtm-relationship (m:n) with the possibility to have extra fields in the join table. Even though this can be done with regular habtm I prefer the way you choose and implement the m:n as 1:n:1, which is simply the same and gives you more options when saving your data.
Here is a similar question and answer
As for your question: You can collect the the data from your products table like this:
$this->Company->ProductAvailability->Product->find('all', $params);
Also you might want to have a look at the containable-behaviour which is very useful for this use case:
$params['conditions'] = array(
'Company.id' => $id
);
$params['contain'] => array(
'ProductAvailability' => array(
'conditions' =>array(
'buys' => 1
),
'Product' => array(
'order' => array(
'name' => 'ASC'
)
)
)
);
$this->Company->find('all', $params);

CakePHP 2.0 Adding Related Data fails with missing database table

I'm trying to save data to two models (a model and related hasMany) simultaneously, and have had some success in a number of places in my app, however this approach doesn't seem to be working with tables/models which have underscored names, i.e.
//View Code
echo $this->Form->create('Product');
echo $this->Form->input('name',array('between' => '<br />'));
echo $this->Form->input('Component.0.text',array('between' => '<br />'));
echo $this->Form->end(__('Submit'));
//Controller Code
if ($this->Product->saveAssociated($this->request->data)) {
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
The above works fine, however I have a model ProductComponent (db table product_components)
//View Code
echo $this->Form->create('Product');
echo $this->Form->input('name',array('between' => '<br />'));
echo $this->Form->input('ProductComponent.0.text',array('between' => '<br />'));
echo $this->Form->end(__('Submit'));
//Controller Code
if ($this->Product->saveAssociated($this->request->data)) {
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
The form is picking this up correctly as it's showing a Textarea rather than a standard input, however when saveAssociated is run, I get the response:
Database table product__components for model ProductComponent was not found.
Why is cake looking for a table with a double underscored name?
Any ideas?
Update for Issem Danny:
public $hasMany = array(
'ProductComponent' => array(
'className' => 'Product_Component',
'foreignKey' => 'product_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
A few things:
If you have these three models: Product, Component, ProductComponent, it seems to me you're trying to setup hasAndBelongsToMany relations. Check out the cakephp docs.
Using Component as or in a model name seems confusing to me..
If you have a model for a table product_components: your model should be named "ProductComponent" without an underscore (cake will add that automatically). If you dont follow cake's conventions you can declare the "useTable" property. Your model should look like this:
Code:
// model
class ProductComponent extends AppModel {
public $name = "ProductComponent";
// Only needed when not following cake's conventions
public $useTable = "product_components";
// rest of model code ...
}
// And the hasmany relation
public $hasMany = array(
'ProductComponent' => array(
'className' => 'ProductComponent',
'foreignKey' => 'product_id'
)
);
Hope that helps, good luck.
Have you tried to use
$this->Product->saveAll($this->data))
Try this link SaveAll

Categories