Changing table field using 2 different models - cakephp

I have an IT query system were by users can add their queries, when they add their query the IT department then replies/comments on the query.
How is should work is when the user adds a query it must change the query_type field in the it_queries table to OPEN and when the IT department views the query they reply/comment on it and must change query_type field from OPEN to PENDING then finally when the user has been helped/assisted they should be able to mark the query as closed using a checkbox, that will then change the status from PENDING TO CLOSE.
Not sure if they is a way of setting the constants in the add views and insert in the table.
Obviously i am learning and can someone please guide me on the steps i must take.
Here is my code for the add it_query for the users
<?php echo $this->Form->create('ItQuery'); ?>
<fieldset>
<legend><?php echo __('Add It Query'); ?></legend>
<?php
echo $this->Form->hidden('hr_employee_id',array('value'=>$loggedInId));
echo $this->Form->input('it_query_type_id');
echo $this->Form->input('comment');
echo $this->Form->hidden('status_type', array('value'=>OPEN));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
Here is the code for my comments for IT Department
<?php echo $this->Form->create('ItQueryComment'); ?>
<?php echo __('Add It Query Comment'); ?>
<?php
echo $this->Form->input('it_query_id');
echo $this->Form->input('comment');
echo $this->Form->input('close_query');
?>
<?php echo $this->Form->end(__('Submit')); ?>
Here is the code for my ItQueryComments
public function add() {
if ($this->request->is('post')) {
$this->ItQueryComment->create();
if ($this->ItQueryComment->save($this->request->data)) {
$this->Session->setFlash(__('The it query comment has been saved'));
$this->redirect(array('controller' => 'it_queries','action' => 'view', $this->request->data['ItQueryComment']['it_query_id']));
} else {
$this->Session->setFlash(__('The it query comment could not be saved. Please, try again.'));
}
}
$itQueries = $this->ItQueryComment->ItQuery->find('list');
$hrEmployees = $this->ItQueryComment->HrEmployee->find('list');
$this->set(compact('itQueries', 'hrEmployees'));
}

While saving a comment for a particular Itquery , in your add function of Comments Controller after adding comment to database get its id by using $this->getLastInsertID();
and call function updateAll for Itquery Model and change status of that particular Query to "Pending".

Related

Trying to add data to database but getting "Error: Call to a member function save() on boolean"

I'm new to cakephp and I'm trying to add data to my coupon table with the following code.
In my UsersController I have:
public function addCoupon() {
if ($this->request->is('post')) {
if ($this->Coupon->save($this->request->data)) {
$this->Session->setFlash(__('The data has been saved'));
} else {
$this->Session->setFlash(__('The data could not be saved. Please, try again.'));
}
}
}
In my add_coupon.cpt I have:
<?php
echo $this->Form->create('Coupon',['url' => ['action' => 'addCoupon']]);
echo $this->Form->input('coupon_code');
echo $this->Form->input('expiration_date');
echo $this->Form->input('discount_amount');
echo $this->Form->input('usage_limit');
echo $this->Form->input('domain_limit');
echo $this->Form->input('description');
echo $this->Form->input('type');
echo $this->Form->button('Submit');
echo $this->Form->end();
?>
What is wrong with my code?
If your model is, in fact, named Coupon (singular) rather than Coupons (plural), try adding the following line of code anywhere before $this->Coupon->save(this->request->data):
$this->loadModel('Coupon');
But if it's actually named Coupons as convention would prescribe, change $this->Coupon->save(this->request->data) to $this->Coupons->save(this->request->data)
and add this line instead:
$this->loadModel('Coupons');
By default, the Users controller will only load the Users model. If you use any other models, you need to load them manually. Alternatively, you could move all of this to the Coupons controller, where it probably belongs anyways.

Cake Bake issue with views using cakephp3.0

I am new to cakephp framework and i am using cakephp3.0 now i used baking concept instead of using scaffolding. After baking it automatically generated all pages based on my tables in database and it generated code in models,controllers and views.Now my question is "if it is possible to change the code in views to change field types (from text box to radio button) according to my requirements."
Please help me.
Thanks in advance
Yes, after you bake your project you can change the fields types. Navigate to the folder where all your views are located, for example: app\View\MyViewName. Baking is a great tool to get something up and running fast. If you have a fairly structured website mostly used for data entry this is a great tool. I used it for simple data entry websites and it has saved me so much typing! Just add some data validation/constraints in the model and you're good to go!
A freshly baked view will look a little something like this:
<div class="MyForm Form">
<?php echo $this->Form->create('MyForm'); ?>
<fieldset>
<legend><?php echo __('Add My Form'); ?></legend>
<?php
echo $this->Form->input('field1');
echo $this->Form->input('field2');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List My Forms'), array('action' => 'index')); ?></li>
</ul>
</div>
Change the input methods to look something like this:
$options = array('Y' => 'Yes', 'N' => 'No');
echo $this->Form->radio('myFields', $options);

Saving User and Detail rows with Cake 2.4.5

I am very new to cake and I was wondering how i would be able to create a new user and also be able to create the contact details
database is like so
Users
user_id
contact_detail_id
password
role_id
username
Contact_Details
contact_detail_id
address1
address2
and many other fields
In the User model I have assigned public $hasOne = array('ContactDetail');
Since the user will have one contactdetail
Now what I need to know is how would this would be saved in the add method as many said to use the saveassociated although this doesn't seem to be working.
The add view is like so and from my understanding this is correct
<?php echo $this->Form->create('User', array('action' => 'add')); ?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('User.username');
echo $this->Form->input('User.password');
echo $this->Form->input('roles');
echo $this->Form->input('ContactDetail.name');
echo $this->Form->input('ContactDetail.surname');
echo $this->Form->input('ContactDetail.address1');
echo $this->Form->input('ContactDetail.address2');
echo $this->Form->input('ContactDetail.country');
echo $this->Form->input('ContactDetail.email');
echo $this->Form->input('ContactDetail.fax');
?>
<label>Are you interested in buying property in Malta?</label>
<?php
$interest_buy = array('0'=>'no','1' => 'yes');
echo $this->Form->input('ContactDetail.interest_buy_property',array('type'=>'radio','options'=>$interest_buy,'value'=>'0','legend'=>FALSE));
?>
<label>Are you interested in renting property in Malta?</label>
<?php
$interest_rent = array('0'=>'no','1' => 'yes');
echo $this->Form->input('ContactDetail.interest_rent_property',array('type'=>'radio','options'=>$interest_rent,'value'=>'0','legend'=>FALSE));
echo $this->Form->input('ContactDetail.mobile');
echo $this->Form->input('ContactDetail.phone');
echo $this->Form->input('ContactDetail.postcode');
echo $this->Form->input('ContactDetail.town');
echo $this->Form->input('ContactDetail.newsletter',array('type'=>'checkbox','label'=>'Would you like to register for the newsletter?' ,'checked'=>'1','legend'=>FALSE,));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
Please see Arun explanation about what to put in your model first and then, you can use $this->User->saveAll(); or $this->User->saveAssociated(); to save both tables at the same time.
If you want to save ContactDetail first and then save User, You can do this second option, but it is not recomended.
$this->User->ContactDetail->create();
$this->User->ContactDetail->save($this->request->data);`
this->request->data['User']['contact_detail_id'] = $this->User->ContactDetail->id`;
$this->User->Create();
$this->User->save($this->request->data);`
But I prefer the first option, to try the first option again,
Note: Make sure ContactDetail and User relationship are there in the model like this in User Model
public $hasOne= array(
'ContactDetail' => array(
'className' => 'ContactDetail',
'foreignKey' => 'contact_detail_id'
)
);
Hope it helps.
In the first look into your database, you should use the following database structure, this will help you in a more directions:
User Model
id
username
password
role_id
ContactDetail Model
id
user_id
address1
address2
Define hasMany model relationship in the User Model with ContactDetail like:
$hasMany => array('ContactDetail');
Now populate an array of Contact details with (id => address1) pair using:
$this->ContactDetail->find('list', array('conditions' => array('user_id' => $user_id)),
'fields' => array('ContactDetail.id', 'ContactDetail.address1')));
Now to save an associated details, use SaveAssociated Method.

cakephp select field of relationed elements for show in a view

I have this in cakephp, I try choose the field to be displayed in the combobox when creating related data in cake php.
<?php echo $this->Form->create('Round'); ?>
<fieldset>
<legend><?php echo __('Add Round'); ?></legend>
<?php
echo $this->Form->input('description');
echo $this->Form->input('text_marked');
echo $this->Form->input('project_id');
echo $this->Form->input('User');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
I try select one project with project_title, in the table project, not by 'project_id'. Title are not unique but is more descriptive than id. Can I do this in cake php?
The answer was easy,Thus we choose the field to display in the other views that are associated. it was put:
public $displayField = 'titte'
in the Round model.

How do i use batch insert in yii

Please i need a big help here. AM developing a yii application where i have to loop through my form and do a batch insert. I found bacth update in yii but ive not been able to see how to do batch insert and validation. Please help.
Here is my View:
<?php for($i=0;$i< $this->getDisplayArchModel();$i++) {?>
<FIELDSET class="radios">
<div class="row">
<?php echo $form->labelEx($model,'competency_type'); ?>
<?php echo $form->textField($model,'competency_type'); ?>
<?php echo $form->error($model,'competency_type'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'definition'); ?>
<?php echo $form->textArea($model,'definition'); ?>
<?php echo $form->error($model,'definition'); ?>
</div>
</fieldset>
<?php } ?>
Controller ::
public function actionDisplayArchModel()
{
$validateCat = $this->getDisplayArchModel();
if($validateCat == NULL)
$this->redirect(array('architecture'));
$model = new CompetencyType;
if(isset($_POST['CompetencyType']))
{
$model->attributes = $_POST['CompetencyType'];
if($model->validate()){
foreach($_POST['CompetencyType'] as $value)
{
echo $value;
}
}
}
$this->render('displayArchModel' ,array('model'=>$model));
}
Sometimes we want to collect user input in a batch mode. That is, the user can enter the information for multiple model instances and submit them all at once. We call this tabular input because the input fields are often presented in an HTML table.
To work with tabular input, we first need to create or populate an array of model instances, depending on whether we are inserting or updating the data. We then retrieve the user input data from the $_POST variable and assign it to each model. A slight difference from single model input is that we retrieve the input data using $_POST['ModelClass'][$i] instead of $_POST['ModelClass'].
more on collecting Tabular Input

Resources