how to save data in one model from another controller in cakephp? - cakephp

I have two controller name as ProductsController and Categories Controller and have there models.I have loaded Category model in ProductsController using
public function beforeFilter(){
$this->loadModel('Category');
}
In ProductsController When I use
$data = $this->Category->find('all', array('conditions'=>array('product_id'=>$id)));
it will return Value so that I can retrieve data from Category model .But I try to Save data in Category model from productsController using
$this->Category->save($this->request->data);
It doesnot work.When I debug to to see what it return,I get debug result as
\app\controller\ProductsController.php (line 74)
false
How to solve this problem any help?

Try to print data in $this->request->data before save and also print sql log
like
echo "<pre>";
print_r($this->request->data);
$this->Category->save($this->request->data);
$log=$this->Category->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;
and make sure you have set debug to 2 in core.php

Related

cakephp getLastInsertId not working

Here I have used cakephp js helper to send data,After insert One data I need this last id for farther work.Here I have tried bellow code in Addcontroller
if ($this->Patient->save($this->request->data)) {
$lastid=$this->Patient->getLastInsertId();
$patient=$this->Patient->find('all',array(
'conditions'=>array('Patient.id'=>$lastid ),
'recursive' => -1
));
$this->set('patient', $patient);
}
In add.ctp I have tried bellow code but I haven't get last id here.
<?php foreach ($patient as $patient): ?>
<?php echo h($patient['Patient']['id']); ?>
<?php endforeach; ?>
Method getLastInsertId() return id of just saved records.
If you need this id in your view just after save, you must first set that variable in your controller like $this->set(compact('lastid','patient'); and then use in view <?php echo $lastid; ?>
use
if ($this->Patient->save($this->request->data)) {
$id = $this->Patient->id;
$patient=$this->Patient->find('all',array('conditions'=>array('Patient.id'=>$id),'recursive' => -1));
$this->set->('patient', $patient);
//If you are saving the record with ajax which it looks like you
//might be from your question you will need the following instead
//of $this->set->('patient', $patient); try:
return json_encode($patient);
You will then also need to update your js ajax call, you will have a json array to decode so parse it with jquery and append it back into your view.
Cake will always give you the id of record you have just saved, by simply adding $id = $this->MyModel->id; You can use the id to query for the record.
Try below code:
In controller:
$lastid=$this->Patient->getLastInsertId();
$this->set(compact('lastid','patient');
Then use $lastid in View file.
In controller:
$lastid=$this->Patient->getLastInsertId();
$patient['Patient']['last_id'] = $lastid;
then use $patient['Patient']['last_id'] in your view file.

how to reach database fields from default.ctp in Cakephp

I'm unable to show a db field value in default.ctp , like unreaded messages or username ,
I keep getting Undefined variable: user, or message
how to reach them from default.ctp ?
Since your default layout is application wide, you will need to process data in the AppController.php in the beforeFilter method, so something like this
In AppController.php :
public function beforeFilter() {
//for example, you want to read messages
//import the Model
$this->loadModel('Message');
$all_messages = $this->Message->find('all'); //or whatever you need to do to get the data
$this->set('all_messages', $all_messages);
}
Then in your default.ctp,call the variable: $all_messages

How to use find('all') in Views - CakePHP

I searched a lot but I couldn't find on How to use the find('all') in Views as used in Rails, but here I'm getting the error "Undefined property: View::$Menu [APP\Lib\Cake\View\View.php, line 804]"
'Menu' is the model which I'm using to fetch data from the menus table.
I'm using the below code in views:
$this->set('test',$this->Menu->find('all'));
print_r($test);
Inside your Menu model create a method, something like getMenu(). In this method do your find() and get the results you want. Modify the results as you need and like to within the getMenu() method and return the data.
If you need that menu on every page in AppController::beforeFilter() or beforeRender() simply do
$this->set('menu', ClassRegistry::init('Menu')->getMenu());
If you do not need it everywhere you might go better with using requestAction getting the data using this method from the Menus controller that will call getMenu() from the model and return the data. Setting it where you need it would be still better, if you use requestAction you also want to cache it very likely.
TRY TO NOT RETRIEVE DATA WITHIN VIEW FILE. VIOLATION OF MVC RULE
try this in view file:
$menu = ClassRegistry::init('Menu');
pr($menu->find('all'));
In AppHelper ,
Make a below function
function getMenu()
{
App::import('Model', 'Menu');
$this->Menu= &new Menu();
$test = array();
$test = $this->Menu->find('all');
return $test;
}
Use above function in view like :
<?php
$menu = $html->getMenu();
print_r($menu);
?>
Cakephp not allow this .
First create the reference(object) of your model using ClassRegistry::init('Model');
And then call find function from using object
$obj = ClassRegistry::init('Menu');
$test = $obj->find('all');
echo ""; print_r($test); `
This will work.

CakePHP $this->Auth->user('id') returns incorrect user in controller

I've got a really bizarre bug occurring in a CakePHP app I've been working on.
I've got the following method in my users_controller.php that reads the currently signed in user and sends the data to a view and sets the title_for_layout to the user's name:-
function account() {
$this->User->id = $this->Auth->user('id');
$this->set('User', $this->User->read());
$this->set('title_for_layout', 'Welcome '.$this->User->Contact->field('name'));
}
In my view I've got (among other things):-
<?php echo $User['Contact']['name'] ?>
Everything in the view looks fine. It is outputting the fields for the correct user (the one I am currently signed in as). However the title_for_layout is using a completely different user's details. So $this->User->Contact->field('name') is not the same as $User['Contact']['name']!
I can't spot what is going wrong here so hoping someone out there can point out my mistake.
Model read() simply does a find('first'), sets the result as the Model $data, and returns it. It doesn't set $ids of associated models.
So in your case, $this->User->id is set, but $this->User->Contact->id isn't.

Cakephp: how to access to model "variables" (mapped from db) from the controller after a set?

Ok i have this controller:
class ExampleController extends AppController {
var $name = 'Example';
public function test_me () {
$this->Example->Create();
$this->Example->set( 'variable_from_db_1' => 'random_value_1',
'variable_from_db_2' => 'random_value_2' );
//here, how can i access to variable_from_db_1 and 2 in $this->Example?
//???? i've tried $this->data and $this->Example->data but nothing to do
}
}
Do you have some hints for me?
you can explore the data with:
debug( $this->Example );
the data is it's own array:
$this->Example->data['variable_from_db_1'];
I don't think you can do that.
You can assign your model data to a $this->data array like this:
$this->data['variable_from_db_1'] = $value;
$this->set('variable_from_db_1', $value);
So know you can access $this->data within the controller.
I think if you want to save data to your actual Model, you might have to implement the getter / setter method in your model...
In the related view you can access it like this:
echo $variable_from_db_1.'<br />';
echo $variable_from_db_2.'<br />';
In the controller call
debug($this->data);
I think you cannot do this in controller, normally set calls are moved to the end area of actions and all the operations on the such variables must be performed before 'set'ing it for later access in views.

Resources